Class: Meiriyigua::PostClient

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
lib/meiriyigua/post_client.rb

Constant Summary collapse

ZHS =
0x4e00..0x9fff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePostClient

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 
  check_category
end

Instance Attribute Details

#category_idsObject (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_categoryObject



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"
  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

#loginObject



49
50
51
52
53
54
55
56
# File 'lib/meiriyigua/post_client.rb', line 49

def 
  page = @agent.get(Meiriyigua::Config.)
   = page.form_with(name: 'login')
  .field_with(name: 'ad_name').value = Meiriyigua::Config.username
  .field_with(name: 'ad_pwd').value = Meiriyigua::Config.password
   = @agent.submit()
  .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} #{$!.message}\n#{$!.backtrace.join("\n")}"
    false
end

#runObject



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