Class: Meiriyigua::PostClient
- Inherits:
-
Object
- Object
- Meiriyigua::PostClient
- Includes:
- Models
- Defined in:
- lib/meiriyigua/post_client.rb
Constant Summary collapse
- ZHS =
0x4e00..0x9fff
Instance Attribute Summary collapse
-
#category_ids ⇒ Object
readonly
Returns the value of attribute category_ids.
Instance Method Summary collapse
- #check_category ⇒ Object
-
#initialize ⇒ PostClient
constructor
A new instance of PostClient.
- #login ⇒ Object
- #post_news(page_record, category_id) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ PostClient
Returns a new instance of PostClient.
14 15 16 17 18 19 20 |
# File 'lib/meiriyigua/post_client.rb', line 14 def initialize @agent = CrawlClient.create_agent @category_ids = {} raise "登录失败,请检测用户与密码是否正确" unless login check_category end |
Instance Attribute Details
#category_ids ⇒ Object (readonly)
Returns the value of attribute category_ids.
12 13 14 |
# File 'lib/meiriyigua/post_client.rb', line 12 def category_ids @category_ids end |
Instance Method Details
#check_category ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/meiriyigua/post_client.rb', line 58 def check_category page = @agent.get "http://#{Meiriyigua::Config.site_host}/admin/admin.php/News/add" = page.search('select#news_type > option').collect{|o| [o.text.strip, o['value'].to_i]} = Hash[] Meiriyigua::Config.categories.each do |host, name| unless .keys.include?(name) @category_ids[host] = [name] @agent.post("http://#{Meiriyigua::Config.site_host}/admin/admin.php/NewsType/add", news_type: name) end end load_category_ids end |
#login ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/meiriyigua/post_client.rb', line 49 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) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/meiriyigua/post_client.rb', line 71 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} #{$!.}\n#{$!.backtrace.join("\n")}" false end |
#run ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/meiriyigua/post_client.rb', line 22 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 |