rss_agent.rb 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. require 'rss'
  2. require 'feed-normalizer'
  3. module Agents
  4. class RssAgent < Agent
  5. include WebRequestConcern
  6. cannot_receive_events!
  7. default_schedule "every_1d"
  8. description do
  9. <<-MD
  10. This Agent consumes RSS feeds and emits events when they change.
  11. (If you want to *output* an RSS feed, use the DataOutputAgent. Also, you can technically parse RSS and XML feeds
  12. with the WebsiteAgent as well. See [this example](https://github.com/cantino/huginn/wiki/Agent-configuration-examples#itunes-trailers).)
  13. Options:
  14. * `url` - The URL of the RSS feed.
  15. * `clean` - Attempt to use [feed-normalizer](https://github.com/aasmith/feed-normalizer)'s' `clean!` method to cleanup HTML in the feed. Set to `true` to use.
  16. * `expected_update_period_in_days` - How often you expect this RSS feed to change. If more than this amount of time passes without an update, the Agent will mark itself as not working.
  17. * `headers` - When present, it should be a hash of headers to send with the request.
  18. * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
  19. * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
  20. MD
  21. end
  22. def default_options
  23. {
  24. 'expected_update_period_in_days' => "5",
  25. 'clean' => 'false',
  26. 'url' => "https://github.com/cantino/huginn/commits/master.atom"
  27. }
  28. end
  29. event_description <<-MD
  30. Events look like:
  31. {
  32. "id": "829f845279611d7925146725317b868d",
  33. "date_published": "2014-09-11 01:30:00 -0700",
  34. "last_updated": "Thu, 11 Sep 2014 01:30:00 -0700",
  35. "url": "http://example.com/...",
  36. "urls": [ "http://example.com/..." ],
  37. "description": "Some description",
  38. "content": "Some content",
  39. "title": "Some title",
  40. "authors": [ ... ],
  41. "categories": [ ... ]
  42. }
  43. MD
  44. def working?
  45. event_created_within?((interpolated['expected_update_period_in_days'].presence || 10).to_i) && !recent_error_logs?
  46. end
  47. def validate_options
  48. errors.add(:base, "url is required") unless options['url'].present?
  49. unless options['expected_update_period_in_days'].present? && options['expected_update_period_in_days'].to_i > 0
  50. errors.add(:base, "Please provide 'expected_update_period_in_days' to indicate how many days can pass without an update before this Agent is considered to not be working")
  51. end
  52. validate_web_request_options!
  53. end
  54. def check
  55. response = faraday.get(interpolated['url'])
  56. if response.success?
  57. feed = FeedNormalizer::FeedNormalizer.parse(response.body)
  58. feed.clean! if interpolated['clean'] == 'true'
  59. created_event_count = 0
  60. feed.entries.each do |entry|
  61. entry_id = get_entry_id(entry)
  62. if check_and_track(entry_id)
  63. created_event_count += 1
  64. create_event(payload: {
  65. id: entry_id,
  66. date_published: entry.date_published,
  67. last_updated: entry.last_updated,
  68. url: entry.url,
  69. urls: entry.urls,
  70. description: entry.description,
  71. content: entry.content,
  72. title: entry.title,
  73. authors: entry.authors,
  74. categories: entry.categories
  75. })
  76. end
  77. end
  78. log "Fetched #{interpolated['url']} and created #{created_event_count} event(s)."
  79. else
  80. error "Failed to fetch #{interpolated['url']}: #{response.inspect}"
  81. end
  82. end
  83. protected
  84. def get_entry_id(entry)
  85. entry.id.presence || Digest::MD5.hexdigest(entry.content)
  86. end
  87. def check_and_track(entry_id)
  88. memory['seen_ids'] ||= []
  89. if memory['seen_ids'].include?(entry_id)
  90. false
  91. else
  92. memory['seen_ids'].unshift entry_id
  93. memory['seen_ids'].pop if memory['seen_ids'].length > 500
  94. true
  95. end
  96. end
  97. end
  98. end