class Meiriyigua::PostClient
Constants
- ZHS
Attributes
category_ids[R]
Public Class Methods
new()
click to toggle source
# File lib/meiriyigua/post_client.rb, line 13 def initialize @agent = CrawlClient.create_agent @category_ids = {} raise "登录失败,请检测用户与密码是否正确" unless login check_category end
Public Instance Methods
check_category()
click to toggle source
# File lib/meiriyigua/post_client.rb, line 57 def check_category page = @agent.get "http://#{Meiriyigua::Config.site_host}/admin/admin.php/News/add" options = page.search('select#news_type > option').collect{|o| [o.text.strip, o['value'].to_i]} options = Hash[options] Meiriyigua::Config.categories.each do |host, name| unless options.keys.include?(name) @category_ids[host] = options[name] @agent.post("http://#{Meiriyigua::Config.site_host}/admin/admin.php/NewsType/add", news_type: name) end end load_category_ids end
login()
click to toggle source
# File lib/meiriyigua/post_client.rb, line 48 def login page = @agent.get(Meiriyigua::Config.login_url) login_form = page.form_with(name: 'login') login_form.field_with(name: 'ad_name').value = Meiriyigua::Config.username login_form.field_with(name: 'ad_pwd').value = Meiriyigua::Config.password login_results = @agent.submit(login_form) login_results.search('a[href="/admin/admin.php/Index/index"]').size > 0 end
post_news(page_record, category_id)
click to toggle source
# File lib/meiriyigua/post_client.rb, line 70 def post_news(page_record, category_id) post_params = {} post_params['news_tit'] = page_record.title post_params['author'] = random_zh post_params['click'] = rand(600..13000) post_params['biaoqian'] = page_record.category post_params['download_url'] = page_record.downloads.split('#!#')[0..1].join('|') post_params['news_type'] = category_id post_params['jianjie'] = page_record.content.to_s[0..16] post_params['editorValue'] = "<pre>#{page_record.content} \r\n\r\n #{page_record.baidu_intro}</pre>" result_page = @agent.post("http://#{Meiriyigua::Config.site_host}/admin/admin.php/News/insert", post_params) !result_page.search('script').text.index('/admin/admin.php/News/index').nil? rescue puts "发布出错了 #{$!.class} #{$!.message}\n#{$!.backtrace.join("\n")}" false end
run()
click to toggle source
# File lib/meiriyigua/post_client.rb, line 21 def run today_count = UrlRecord.count(:publish_at.gte => Date.today) if today_count >= Meiriyigua::Config.day_num return end UrlRecord.all(:baidu_at.not => nil, :publish_at => nil).each do |url_record| today_count += 1 if today_count >= Meiriyigua::Config.day_num return end page_record = url_record.page_record if post_news(page_record, @category_ids[URI(url_record.url).host]) url_record.publish_at = Time.now url_record.save print "发布 #{url_record.url} " puts "成功" else print "发布 #{url_record.url} " puts "失败" end sleep Meiriyigua::Config.post_time.to_i end end
Private Instance Methods
load_category_ids()
click to toggle source
# File lib/meiriyigua/post_client.rb, line 96 def load_category_ids page = @agent.get "http://#{Meiriyigua::Config.site_host}/admin/admin.php/News/add" options = page.search('select#news_type > option').collect{|o| [o.text.strip, o['value'].to_i]} options = Hash[options] Meiriyigua::Config.categories.each do |host, name| @category_ids[host] = options[name] end end
random_zh()
click to toggle source
# File lib/meiriyigua/post_client.rb, line 88 def random_zh s = "" rand(2..4).times do s << rand(ZHS) end s end