twitter_user_agent.rb 4.1 KB

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