Nenhuma Descrição http://j1x-huginn.herokuapp.com

twitter_user_agent.rb 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require "twitter"
  2. module Agents
  3. class TwitterUserAgent < Agent
  4. include TwitterConcern
  5. cannot_receive_events!
  6. description <<-MD
  7. The TwitterUserAgent follows the timeline of a specified Twitter user.
  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 to monitor.
  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. MD
  12. event_description <<-MD
  13. Events are the raw JSON provided by the Twitter API. Should look something like:
  14. {
  15. ... every Tweet field, including ...
  16. "text": "something",
  17. "user": {
  18. "name": "Mr. Someone",
  19. "screen_name": "Someone",
  20. "location": "Vancouver BC Canada",
  21. "description": "...",
  22. "followers_count": 486,
  23. "friends_count": 1983,
  24. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  25. "time_zone": "Pacific Time (US & Canada)",
  26. "statuses_count": 3807,
  27. "lang": "en"
  28. },
  29. "retweet_count": 0,
  30. "entities": ...
  31. "lang": "en"
  32. }
  33. MD
  34. default_schedule "every_1h"
  35. def validate_options
  36. unless options['username'].present? &&
  37. options['expected_update_period_in_days'].present?
  38. errors.add(:base, "username and expected_update_period_in_days are required")
  39. end
  40. end
  41. def working?
  42. event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
  43. end
  44. def default_options
  45. {
  46. 'username' => "tectonic",
  47. 'expected_update_period_in_days' => "2"
  48. }
  49. end
  50. def check
  51. since_id = memory['since_id'] || nil
  52. opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
  53. opts.merge! :since_id => since_id unless since_id.nil?
  54. tweets = twitter.user_timeline(options['username'], opts)
  55. tweets.each do |tweet|
  56. memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
  57. create_event :payload => tweet.attrs
  58. end
  59. save!
  60. end
  61. end
  62. end