twitter_favorites.rb 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. module Agents
  2. class TwitterFavorites < Agent
  3. include TwitterConcern
  4. cannot_receive_events!
  5. description <<-MD
  6. The Twitter Favorites List Agent follows the favorites list 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, `number` of latest tweets to monitor and `history' as number of tweets that will be held in memory.
  10. 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.
  11. 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`)
  12. MD
  13. event_description <<-MD
  14. Events are the raw JSON provided by the [Twitter API](https://dev.twitter.com/docs/api/1.1/get/favorites/list). Should look something like:
  15. {
  16. ... every Tweet field, including ...
  17. "text": "something",
  18. "user": {
  19. "name": "Mr. Someone",
  20. "screen_name": "Someone",
  21. "location": "Vancouver BC Canada",
  22. "description": "...",
  23. "followers_count": 486,
  24. "friends_count": 1983,
  25. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  26. "time_zone": "Pacific Time (US & Canada)",
  27. "statuses_count": 3807,
  28. "lang": "en"
  29. },
  30. "retweet_count": 0,
  31. "entities": ...
  32. "lang": "en"
  33. }
  34. MD
  35. default_schedule "every_1h"
  36. def working?
  37. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  38. end
  39. def default_options
  40. {
  41. 'username' => 'tectonic',
  42. 'number' => '10',
  43. 'history' => '100',
  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, "number is required") unless options['number'].present?
  50. errors.add(:base, "history is required") unless options['history'].present?
  51. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  52. if options[:starting_at].present?
  53. Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
  54. end
  55. end
  56. def starting_at
  57. if interpolated[:starting_at].present?
  58. Time.parse(interpolated[:starting_at]) rescue created_at
  59. else
  60. created_at
  61. end
  62. end
  63. def check
  64. opts = {:count => interpolated['number'], tweet_mode: 'extended'}
  65. tweets = twitter.favorites(interpolated['username'], opts)
  66. memory[:last_seen] ||= []
  67. tweets.each do |tweet|
  68. unless memory[:last_seen].include?(tweet.id) || tweet.created_at < starting_at
  69. memory[:last_seen].push(tweet.id)
  70. memory[:last_seen].shift if memory[:last_seen].length > interpolated['history'].to_i
  71. create_event payload: tweet.attrs
  72. end
  73. end
  74. end
  75. end
  76. end