weibo_user_agent.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. module Agents
  2. class WeiboUserAgent < Agent
  3. include WeiboConcern
  4. cannot_receive_events!
  5. description <<~MD
  6. The Weibo User Agent follows the timeline of a specified Weibo user. It uses this endpoint: http://open.weibo.com/wiki/2/statuses/user_timeline/en
  7. #{'## Include `weibo_2` in your Gemfile to use this Agent!' if dependencies_missing?}
  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 Weibo 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. errors.add(:base, "expected_update_period_in_days and uid are required")
  65. end
  66. end
  67. def working?
  68. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  69. end
  70. def default_options
  71. {
  72. 'uid' => "",
  73. 'access_token' => "---",
  74. 'app_key' => "---",
  75. 'app_secret' => "---",
  76. 'expected_update_period_in_days' => "2"
  77. }
  78. end
  79. def check
  80. since_id = memory['since_id'] || nil
  81. opts = { uid: interpolated['uid'].to_i }
  82. opts.merge! since_id: since_id unless since_id.nil?
  83. # http://open.weibo.com/wiki/2/statuses/user_timeline/en
  84. resp = weibo_client.statuses.user_timeline opts
  85. if resp[:statuses]
  86. resp[:statuses].each do |status|
  87. memory['since_id'] = status.id if !memory['since_id'] || (status.id > memory['since_id'])
  88. create_event payload: status.as_json
  89. end
  90. end
  91. save!
  92. end
  93. end
  94. end