twitter_user_agent.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. Twitter credentials must be supplied as either [credentials](/user_credentials) called
  9. `twitter_consumer_key`, `twitter_consumer_secret`, `twitter_oauth_token`, and `twitter_oauth_token_secret`,
  10. or as options to this Agent called `consumer_key`, `consumer_secret`, `oauth_token`, and `oauth_token_secret`.
  11. To get oAuth credentials for Twitter, [follow these instructions](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token).
  12. You must also provide the `username` of the Twitter user to monitor.
  13. Set `include_retweets` to `false` to not include retweets (default: `true`)
  14. 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.
  15. Set `starting_at` to the date/time (eg. `Mon Jun 02 00:38:12 +0000 2014`) you want to start receiving tweets from (default: agent's `created_at`)
  16. MD
  17. event_description <<-MD
  18. Events are the raw JSON provided by the [Twitter API](https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline). Should look something like:
  19. {
  20. ... every Tweet field, including ...
  21. "text": "something",
  22. "user": {
  23. "name": "Mr. Someone",
  24. "screen_name": "Someone",
  25. "location": "Vancouver BC Canada",
  26. "description": "...",
  27. "followers_count": 486,
  28. "friends_count": 1983,
  29. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  30. "time_zone": "Pacific Time (US & Canada)",
  31. "statuses_count": 3807,
  32. "lang": "en"
  33. },
  34. "retweet_count": 0,
  35. "entities": ...
  36. "lang": "en"
  37. }
  38. MD
  39. default_schedule "every_1h"
  40. def working?
  41. event_created_within?(interpolated_options['expected_update_period_in_days']) && !recent_error_logs?
  42. end
  43. def default_options
  44. {
  45. 'username' => 'tectonic',
  46. 'include_retweets' => 'true',
  47. 'expected_update_period_in_days' => '2'
  48. }
  49. end
  50. def validate_options
  51. errors.add(:base, "username is required") unless options['username'].present?
  52. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  53. if options[:include_retweets].present? && !%w[true false].include?(options[:include_retweets])
  54. errors.add(:base, "include_retweets must be a boolean value string (true/false)")
  55. end
  56. if options[:starting_at].present?
  57. Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
  58. end
  59. end
  60. def starting_at
  61. if interpolated_options[:starting_at].present?
  62. Time.parse(interpolated_options[:starting_at]) rescue created_at
  63. else
  64. created_at
  65. end
  66. end
  67. def include_retweets?
  68. interpolated_options[:include_retweets] != "false"
  69. end
  70. def check
  71. since_id = memory['since_id'] || nil
  72. opts = {:count => 200, :include_rts => include_retweets?, :exclude_replies => false, :include_entities => true, :contributor_details => true}
  73. opts.merge! :since_id => since_id unless since_id.nil?
  74. # http://rdoc.info/gems/twitter/Twitter/REST/Timelines#user_timeline-instance_method
  75. tweets = twitter.user_timeline(interpolated_options['username'], opts)
  76. tweets.each do |tweet|
  77. if tweet.created_at >= starting_at
  78. memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
  79. create_event :payload => tweet.attrs
  80. end
  81. end
  82. save!
  83. end
  84. end
  85. end