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

twitter_user_agent.rb 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. require "twitter"
  2. module Agents
  3. class TwitterUserAgent < Agent
  4. cannot_receive_events!
  5. description <<-MD
  6. The TwitterUserAgent follows the timeline of a specified Twitter user.
  7. You [must set up a Twitter app](https://github.com/cantino/huginn/wiki/Getting-a-twitter-oauth-token) and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`, (Also shown as "Access token" on the Twitter developer's site.) along with the `username` of the Twitter user to monitor.
  8. 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.
  9. MD
  10. event_description <<-MD
  11. Events are the raw JSON provided by the Twitter API. Should look something like:
  12. {
  13. ... every Tweet field, including ...
  14. "text": "something",
  15. "user": {
  16. "name": "Mr. Someone",
  17. "screen_name": "Someone",
  18. "location": "Vancouver BC Canada",
  19. "description": "...",
  20. "followers_count": 486,
  21. "friends_count": 1983,
  22. "created_at": "Mon Aug 29 23:38:14 +0000 2011",
  23. "time_zone": "Pacific Time (US & Canada)",
  24. "statuses_count": 3807,
  25. "lang": "en"
  26. },
  27. "retweet_count": 0,
  28. "entities": ...
  29. "lang": "en"
  30. }
  31. MD
  32. default_schedule "every_1h"
  33. def validate_options
  34. unless options[:username].present? && options[:expected_update_period_in_days].present? && options[:consumer_key].present? && options[:consumer_secret].present? && options[:oauth_token].present? && options[:oauth_token_secret].present?
  35. errors.add(:base, "expected_update_period_in_days, username, consumer_key, consumer_secret, oauth_token and oauth_token_secret are required")
  36. end
  37. end
  38. def working?
  39. event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs?
  40. end
  41. def default_options
  42. {
  43. :username => "tectonic",
  44. :expected_update_period_in_days => "2",
  45. :consumer_key => "---",
  46. :consumer_secret => "---",
  47. :oauth_token => "---",
  48. :oauth_token_secret => "---"
  49. }
  50. end
  51. def check
  52. Twitter.configure do |config|
  53. config.consumer_key = options[:consumer_key]
  54. config.consumer_secret = options[:consumer_secret]
  55. config.oauth_token = options[:oauth_token]
  56. config.oauth_token_secret = options[:oauth_token_secret]
  57. end
  58. since_id = memory[:since_id] || nil
  59. opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
  60. opts.merge! :since_id => since_id unless since_id.nil?
  61. tweets = Twitter.user_timeline(options[:username], opts)
  62. tweets.each do |tweet|
  63. memory[:since_id] = tweet.id if !memory[:since_id] || (tweet.id > memory[:since_id])
  64. create_event :payload => tweet.attrs
  65. end
  66. save!
  67. end
  68. end
  69. end