twitter_favorites.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module Agents
  2. class TwitterFavorites < Agent
  3. include TwitterConcern
  4. can_dry_run!
  5. cannot_receive_events!
  6. description <<~MD
  7. The Twitter Favorites List Agent follows the favorites list of a specified Twitter user.
  8. #{twitter_dependencies_missing if dependencies_missing?}
  9. To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.
  10. 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.
  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 v1.1](https://dev.twitter.com/docs/api/1.1/get/favorites/list) with slight modifications. They should look something like this:
  16. #{tweet_event_description('full_text')}
  17. MD
  18. default_schedule "every_1h"
  19. def working?
  20. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  21. end
  22. def default_options
  23. {
  24. 'username' => 'tectonic',
  25. 'number' => '10',
  26. 'history' => '100',
  27. 'expected_update_period_in_days' => '2'
  28. }
  29. end
  30. def validate_options
  31. errors.add(:base, "username is required") unless options[:username].present?
  32. errors.add(:base, "number is required") unless options[:number].present?
  33. errors.add(:base, "history is required") unless options[:history].present?
  34. errors.add(
  35. :base,
  36. "expected_update_period_in_days is required"
  37. ) unless options[:expected_update_period_in_days].present?
  38. if options[:starting_at].present?
  39. begin
  40. Time.parse(options[:starting_at])
  41. rescue StandardError
  42. errors.add(:base, "Error parsing starting_at")
  43. end
  44. end
  45. end
  46. def starting_at
  47. if interpolated[:starting_at].present?
  48. begin
  49. Time.parse(interpolated[:starting_at])
  50. rescue StandardError
  51. end
  52. end || created_at || Time.now # for dry-running
  53. end
  54. def check
  55. opts = { count: interpolated[:number], tweet_mode: 'extended' }
  56. tweets = twitter.favorites(interpolated[:username], opts)
  57. memory[:last_seen] ||= []
  58. tweets.sort_by(&:id).each do |tweet|
  59. next if memory[:last_seen].include?(tweet.id) || tweet.created_at < starting_at
  60. memory[:last_seen].push(tweet.id)
  61. memory[:last_seen].shift if memory[:last_seen].length > interpolated[:history].to_i
  62. create_event(payload: format_tweet(tweet))
  63. end
  64. end
  65. end
  66. end