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

twitter_search_agent.rb 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. module Agents
  2. class TwitterSearchAgent < Agent
  3. include TwitterConcern
  4. cannot_receive_events!
  5. description <<-MD
  6. The Twitter Search Agent emits the results of a specified search.
  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 provide the desired `search`.
  10. Set `result_type` (String) — Specifies what type of search results you would prefer to receive. Options are "mixed", "recent", and "popular". (default: `mixed`)
  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](https://dev.twitter.com/rest/reference/get/search/tweets). Should look something like:
  16. {
  17. ... every Tweet field, including ...
  18. "text": "something",
  19. "user": {
  20. "name": "Mr. Someone",
  21. "screen_name": "Someone",
  22. "location": "Vancouver BC Canada",
  23. "description": "...",
  24. "followers_count": 486,
  25. "friends_count": 1983,
  26. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  27. "time_zone": "Pacific Time (US & Canada)",
  28. "statuses_count": 3807,
  29. "lang": "en"
  30. },
  31. "retweet_count": 0,
  32. "entities": ...
  33. "lang": "en"
  34. }
  35. MD
  36. default_schedule "every_1h"
  37. def working?
  38. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  39. end
  40. def default_options
  41. {
  42. 'search' => 'freebandnames',
  43. 'expected_update_period_in_days' => '2'
  44. }
  45. end
  46. def validate_options
  47. errors.add(:base, "search is required") unless options['search'].present?
  48. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  49. if options[:starting_at].present?
  50. Time.parse(options[:starting_at]) rescue errors.add(:base, "Error parsing starting_at")
  51. end
  52. end
  53. def starting_at
  54. if interpolated[:starting_at].present?
  55. Time.parse(interpolated[:starting_at]) rescue created_at
  56. else
  57. created_at
  58. end
  59. end
  60. def check
  61. since_id = memory['since_id'] || nil
  62. opts = {:include_entities => true}
  63. opts.merge! result_type: interpolated[:result_type] if interpolated[:result_type].present?
  64. opts.merge! :since_id => since_id unless since_id.nil?
  65. # http://www.rubydoc.info/gems/twitter/Twitter/REST/Search
  66. tweets = twitter.search(interpolated['search'], opts).take(100)
  67. tweets.each do |tweet|
  68. if tweet.created_at >= starting_at
  69. memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
  70. create_event :payload => tweet.attrs
  71. end
  72. end
  73. save!
  74. end
  75. end
  76. end