weibo_user_agent.rb 3.8 KB

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