Нет описания http://j1x-huginn.herokuapp.com

twitter_stream_agent.rb 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. module Agents
  2. class TwitterStreamAgent < Agent
  3. include TwitterConcern
  4. cannot_receive_events!
  5. description <<-MD
  6. The TwitterStreamAgent follows the Twitter stream in real time, watching for certain keywords, or filters, that you provide.
  7. You must provide an oAuth `consumer_key`, `consumer_secret`, `oauth_token`, and `oauth_token_secret`, as well as an array of `filters`. Multiple words in a filter
  8. 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 get oAuth credentials for Twitter, [follow these instructions](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token).
  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(options[:expected_update_period_in_days]) && !recent_error_logs?
  53. end
  54. def default_options
  55. {
  56. :consumer_key => "---",
  57. :consumer_secret => "---",
  58. :oauth_token => "---",
  59. :oauth_token_secret => "---",
  60. :filters => %w[keyword1 keyword2],
  61. :expected_update_period_in_days => "2",
  62. :generate => "events"
  63. }
  64. end
  65. def process_tweet(filter, status)
  66. filter = lookup_filter(filter)
  67. if filter
  68. if options[:generate] == "counts"
  69. # Avoid memory pollution by reloading the Agent.
  70. agent = Agent.find(id)
  71. agent.memory[:filter_counts] ||= {}
  72. agent.memory[:filter_counts][filter.to_sym] ||= 0
  73. agent.memory[:filter_counts][filter.to_sym] += 1
  74. remove_unused_keys!(agent, :filter_counts)
  75. agent.save!
  76. else
  77. create_event :payload => status.merge(:filter => filter.to_s)
  78. end
  79. end
  80. end
  81. def check
  82. if options[:generate] == "counts" && memory[:filter_counts] && memory[:filter_counts].length > 0
  83. memory[:filter_counts].each do |filter, count|
  84. create_event :payload => { :filter => filter.to_s, :count => count, :time => Time.now.to_i }
  85. end
  86. end
  87. memory[:filter_counts] = {}
  88. end
  89. protected
  90. def lookup_filter(filter)
  91. options[:filters].each do |known_filter|
  92. if known_filter == filter
  93. return filter
  94. elsif known_filter.is_a?(Array)
  95. if known_filter.include?(filter)
  96. return known_filter.first
  97. end
  98. end
  99. end
  100. end
  101. def remove_unused_keys!(agent, base)
  102. if agent.memory[base]
  103. (agent.memory[base].keys - agent.options[:filters].map {|f| f.is_a?(Array) ? f.first.to_sym : f.to_sym }).each do |removed_key|
  104. agent.memory[base].delete(removed_key)
  105. end
  106. end
  107. end
  108. end
  109. end