1
0

twitter_stream_agent.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module Agents
  2. class TwitterStreamAgent < Agent
  3. include TwitterConcern
  4. cannot_receive_events!
  5. description <<-MD
  6. #{twitter_dependencies_missing if dependencies_missing?}
  7. The TwitterStreamAgent follows the Twitter stream in real time, watching for certain keywords, or filters, that you provide.
  8. To follow the Twitter stream, provide an array of `filters`. Multiple words in a filter must all show up in a tweet, but are independent of order.
  9. If you provide an array instead of a filter, the first entry will be considered primary and any additional values will be treated as aliases.
  10. To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.
  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. `generate` should be either `events` or `counts`. If set to `counts`, it will output event summaries whenever the Agent is scheduled.
  13. MD
  14. event_description <<-MD
  15. When in `counts` mode, TwitterStreamAgent events look like:
  16. {
  17. "filter": "hello world",
  18. "count": 25,
  19. "time": 3456785456
  20. }
  21. When in `events` mode, TwitterStreamAgent events look like:
  22. {
  23. "filter": "selectorgadget",
  24. ... every Tweet field, including ...
  25. "text": "something",
  26. "user": {
  27. "name": "Mr. Someone",
  28. "screen_name": "Someone",
  29. "location": "Vancouver BC Canada",
  30. "description": "...",
  31. "followers_count": 486,
  32. "friends_count": 1983,
  33. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  34. "time_zone": "Pacific Time (US & Canada)",
  35. "statuses_count": 3807,
  36. "lang": "en"
  37. },
  38. "retweet_count": 0,
  39. "entities": ...
  40. "lang": "en"
  41. }
  42. MD
  43. default_schedule "11pm"
  44. def validate_options
  45. unless options['filters'].present? &&
  46. options['expected_update_period_in_days'].present? &&
  47. options['generate'].present?
  48. errors.add(:base, "expected_update_period_in_days, generate, and filters are required fields")
  49. end
  50. end
  51. def working?
  52. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  53. end
  54. def default_options
  55. {
  56. 'filters' => %w[keyword1 keyword2],
  57. 'expected_update_period_in_days' => "2",
  58. 'generate' => "events"
  59. }
  60. end
  61. def process_tweet(filter, status)
  62. filter = lookup_filter(filter)
  63. if filter
  64. if interpolated['generate'] == "counts"
  65. # Avoid memory pollution by reloading the Agent.
  66. agent = Agent.find(id)
  67. agent.memory['filter_counts'] ||= {}
  68. agent.memory['filter_counts'][filter] ||= 0
  69. agent.memory['filter_counts'][filter] += 1
  70. remove_unused_keys!(agent, 'filter_counts')
  71. agent.save!
  72. else
  73. create_event :payload => status.merge('filter' => filter)
  74. end
  75. end
  76. end
  77. def check
  78. if interpolated['generate'] == "counts" && memory['filter_counts'] && memory['filter_counts'].length > 0
  79. memory['filter_counts'].each do |filter, count|
  80. create_event :payload => { 'filter' => filter, 'count' => count, 'time' => Time.now.to_i }
  81. end
  82. end
  83. memory['filter_counts'] = {}
  84. end
  85. protected
  86. def lookup_filter(filter)
  87. interpolated['filters'].each do |known_filter|
  88. if known_filter == filter
  89. return filter
  90. elsif known_filter.is_a?(Array)
  91. if known_filter.include?(filter)
  92. return known_filter.first
  93. end
  94. end
  95. end
  96. end
  97. def remove_unused_keys!(agent, base)
  98. if agent.memory[base]
  99. (agent.memory[base].keys - agent.interpolated['filters'].map {|f| f.is_a?(Array) ? f.first.to_s : f.to_s }).each do |removed_key|
  100. agent.memory[base].delete(removed_key)
  101. end
  102. end
  103. end
  104. end
  105. end