twitter_user_agent.rb 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. require "twitter"
  2. module Agents
  3. class TwitterUserAgent < Agent
  4. include TwitterConcern
  5. cannot_receive_events!
  6. description <<-MD
  7. The TwitterUserAgent follows the timeline of a specified Twitter user.
  8. You [must set up a Twitter app](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token) and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`, (Also shown as "Access token" on the Twitter developer's site.) along with the `username` of the Twitter user to monitor.
  9. 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.
  10. MD
  11. event_description <<-MD
  12. Events are the raw JSON provided by the Twitter API. Should look something like:
  13. {
  14. ... every Tweet field, including ...
  15. "text": "something",
  16. "user": {
  17. "name": "Mr. Someone",
  18. "screen_name": "Someone",
  19. "location": "Vancouver BC Canada",
  20. "description": "...",
  21. "followers_count": 486,
  22. "friends_count": 1983,
  23. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  24. "time_zone": "Pacific Time (US & Canada)",
  25. "statuses_count": 3807,
  26. "lang": "en"
  27. },
  28. "retweet_count": 0,
  29. "entities": ...
  30. "lang": "en"
  31. }
  32. MD
  33. default_schedule "every_1h"
  34. def validate_options
  35. unless options['username'].present? &&
  36. options['expected_update_period_in_days'].present?
  37. errors.add(:base, "username and expected_update_period_in_days are required")
  38. end
  39. end
  40. def working?
  41. event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
  42. end
  43. def default_options
  44. {
  45. 'username' => "tectonic",
  46. 'expected_update_period_in_days' => "2",
  47. 'consumer_key' => "---",
  48. 'consumer_secret' => "---",
  49. 'oauth_token' => "---",
  50. 'oauth_token_secret' => "---"
  51. }
  52. end
  53. def check
  54. since_id = memory['since_id'] || nil
  55. opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
  56. opts.merge! :since_id => since_id unless since_id.nil?
  57. tweets = Twitter.user_timeline(options['username'], opts)
  58. tweets.each do |tweet|
  59. memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
  60. create_event :payload => tweet.attrs
  61. end
  62. save!
  63. end
  64. end
  65. end