weibo_user_agent.rb 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # encoding: utf-8
  2. require "weibo_2"
  3. module Agents
  4. class WeiboUserAgent < Agent
  5. cannot_receive_events!
  6. description <<-MD
  7. The WeiboUserAgent follows the timeline of a specified Weibo user. It uses this endpoint: http://open.weibo.com/wiki/2/statuses/user_timeline/en
  8. You must first set up a Weibo app and generate an `acess_token` to authenticate with. Provide that, along with the `app_key` and `app_secret` for your Weibo app in the options.
  9. Specify the `uid` of the Weibo user whose timeline you want to watch.
  10. Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
  11. MD
  12. event_description <<-MD
  13. Events are the raw JSON provided by the Twitter API. Should look something like:
  14. {
  15. "created_at": "Tue May 31 17:46:55 +0800 2011",
  16. "id": 11488058246,
  17. "text": "求关注。",
  18. "source": "<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>",
  19. "favorited": false,
  20. "truncated": false,
  21. "in_reply_to_status_id": "",
  22. "in_reply_to_user_id": "",
  23. "in_reply_to_screen_name": "",
  24. "geo": null,
  25. "mid": "5612814510546515491",
  26. "reposts_count": 8,
  27. "comments_count": 9,
  28. "annotations": [],
  29. "user": {
  30. "id": 1404376560,
  31. "screen_name": "zaku",
  32. "name": "zaku",
  33. "province": "11",
  34. "city": "5",
  35. "location": "北京 朝阳区",
  36. "description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。",
  37. "url": "http://blog.sina.com.cn/zaku",
  38. "profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1",
  39. "domain": "zaku",
  40. "gender": "m",
  41. "followers_count": 1204,
  42. "friends_count": 447,
  43. "statuses_count": 2908,
  44. "favourites_count": 0,
  45. "created_at": "Fri Aug 28 00:00:00 +0800 2009",
  46. "following": false,
  47. "allow_all_act_msg": false,
  48. "remark": "",
  49. "geo_enabled": true,
  50. "verified": false,
  51. "allow_all_comment": true,
  52. "avatar_large": "http://tp1.sinaimg.cn/1404376560/180/0/1",
  53. "verified_reason": "",
  54. "follow_me": false,
  55. "online_status": 0,
  56. "bi_followers_count": 215
  57. }
  58. }
  59. MD
  60. default_schedule "every_1h"
  61. def validate_options
  62. unless options[:uid].present? &&
  63. options[:expected_update_period_in_days].present? &&
  64. options[:app_key].present? &&
  65. options[:app_secret].present? &&
  66. options[:access_token].present?
  67. errors.add(:base, "expected_update_period_in_days, uid, app_key, app_secret and access_token are required")
  68. end
  69. end
  70. def working?
  71. (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present?
  72. end
  73. def default_options
  74. {
  75. :uid => "",
  76. :access_token => "---",
  77. :app_key => "---",
  78. :app_secret => "---",
  79. :expected_update_period_in_days => "2"
  80. }
  81. end
  82. def check
  83. WeiboOAuth2::Config.api_key = options[:app_key] # WEIBO_APP_KEY
  84. WeiboOAuth2::Config.api_secret = options[:app_secret] # WEIBO_APP_SECRET
  85. client = WeiboOAuth2::Client.new
  86. client.get_token_from_hash :access_token => options[:access_token]
  87. since_id = memory[:since_id] || nil
  88. opts = {:uid => options[:uid].to_i}
  89. opts.merge! :since_id => since_id unless since_id.nil?
  90. # http://open.weibo.com/wiki/2/statuses/user_timeline/en
  91. resp = client.statuses.user_timeline opts
  92. if resp[:statuses]
  93. resp[:statuses].each do |status|
  94. memory[:since_id] = status.id if !memory[:since_id] || (status.id > memory[:since_id])
  95. create_event :payload => status.as_json
  96. end
  97. end
  98. save!
  99. end
  100. end
  101. end