1
0

twitter_user_agent.rb 3.5 KB

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