twitter_user_agent.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. module Agents
  2. class TwitterUserAgent < Agent
  3. include TwitterConcern
  4. cannot_receive_events!
  5. description <<-MD
  6. The Twitter User Agent follows the timeline of a specified Twitter user.
  7. #{twitter_dependencies_missing if dependencies_missing?}
  8. To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.
  9. You must also provide the `username` of the Twitter user to monitor.
  10. Set `include_retweets` to `false` to not include retweets (default: `true`)
  11. Set `exclude_replies` to `true` to exclude replies (default: `false`)
  12. 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.
  13. 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`)
  14. MD
  15. event_description <<-MD
  16. 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:
  17. {
  18. ... every Tweet field, including ...
  19. "text": "something",
  20. "user": {
  21. "name": "Mr. Someone",
  22. "screen_name": "Someone",
  23. "location": "Vancouver BC Canada",
  24. "description": "...",
  25. "followers_count": 486,
  26. "friends_count": 1983,
  27. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  28. "time_zone": "Pacific Time (US & Canada)",
  29. "statuses_count": 3807,
  30. "lang": "en"
  31. },
  32. "retweet_count": 0,
  33. "entities": ...
  34. "lang": "en"
  35. }
  36. MD
  37. default_schedule "every_1h"
  38. def working?
  39. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  40. end
  41. def default_options
  42. {
  43. 'username' => 'tectonic',
  44. 'include_retweets' => 'true',
  45. 'exclude_replies' => 'false',
  46. 'expected_update_period_in_days' => '2'
  47. }
  48. end
  49. def validate_options
  50. errors.add(:base, "username is required") unless options['username'].present?
  51. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  52. if options[:include_retweets].present? && !%w[true false].include?(options[:include_retweets])
  53. errors.add(:base, "include_retweets must be a boolean value string (true/false)")
  54. end
  55. if options[:starting_at].present?
  56. Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
  57. end
  58. end
  59. def starting_at
  60. if interpolated[:starting_at].present?
  61. Time.parse(interpolated[:starting_at]) rescue created_at
  62. else
  63. created_at
  64. end
  65. end
  66. def include_retweets?
  67. interpolated[:include_retweets] != "false"
  68. end
  69. def exclude_replies?
  70. boolify(interpolated[:exclude_replies]) || false
  71. end
  72. def check
  73. since_id = memory['since_id'] || nil
  74. opts = {:count => 200, :include_rts => include_retweets?, :exclude_replies => exclude_replies?, :include_entities => true, :contributor_details => true}
  75. opts.merge! :since_id => since_id unless since_id.nil?
  76. # http://rdoc.info/gems/twitter/Twitter/REST/Timelines#user_timeline-instance_method
  77. tweets = twitter.user_timeline(interpolated['username'], opts)
  78. tweets.each do |tweet|
  79. if tweet.created_at >= starting_at
  80. memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
  81. create_event :payload => tweet.attrs
  82. end
  83. end
  84. save!
  85. end
  86. end
  87. end