rss_agent.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. This Agent is fairly simple, using [feed-normalizer](https://github.com/aasmith/feed-normalizer) as a base. For complex feeds
  12. with additional field types, we recommend using a WebsiteAgent. See [this example](https://github.com/cantino/huginn/wiki/Agent-configuration-examples#itunes-trailers).
  13. If you want to *output* an RSS feed, use the DataOutputAgent.
  14. Options:
  15. * `url` - The URL of the RSS feed.
  16. * `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.
  17. * `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.
  18. * `headers` - When present, it should be a hash of headers to send with the request.
  19. * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
  20. * `disable_ssl_verification` - Set to `true` to disable ssl verification.
  21. * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
  22. MD
  23. end
  24. def default_options
  25. {
  26. 'expected_update_period_in_days' => "5",
  27. 'clean' => 'false',
  28. 'url' => "https://github.com/cantino/huginn/commits/master.atom"
  29. }
  30. end
  31. event_description <<-MD
  32. Events look like:
  33. {
  34. "id": "829f845279611d7925146725317b868d",
  35. "date_published": "2014-09-11 01:30:00 -0700",
  36. "last_updated": "Thu, 11 Sep 2014 01:30:00 -0700",
  37. "url": "http://example.com/...",
  38. "urls": [ "http://example.com/..." ],
  39. "description": "Some description",
  40. "content": "Some content",
  41. "title": "Some title",
  42. "authors": [ ... ],
  43. "categories": [ ... ]
  44. }
  45. MD
  46. def working?
  47. event_created_within?((interpolated['expected_update_period_in_days'].presence || 10).to_i) && !recent_error_logs?
  48. end
  49. def validate_options
  50. errors.add(:base, "url is required") unless options['url'].present?
  51. unless options['expected_update_period_in_days'].present? && options['expected_update_period_in_days'].to_i > 0
  52. 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")
  53. end
  54. validate_web_request_options!
  55. end
  56. def check
  57. response = faraday.get(interpolated['url'])
  58. if response.success?
  59. feed = FeedNormalizer::FeedNormalizer.parse(response.body)
  60. feed.clean! if interpolated['clean'] == 'true'
  61. created_event_count = 0
  62. feed.entries.each do |entry|
  63. entry_id = get_entry_id(entry)
  64. if check_and_track(entry_id)
  65. created_event_count += 1
  66. create_event(payload: {
  67. id: entry_id,
  68. date_published: entry.date_published,
  69. last_updated: entry.last_updated,
  70. url: entry.url,
  71. urls: entry.urls,
  72. description: entry.description,
  73. content: entry.content,
  74. title: entry.title,
  75. authors: entry.authors,
  76. categories: entry.categories
  77. })
  78. end
  79. end
  80. log "Fetched #{interpolated['url']} and created #{created_event_count} event(s)."
  81. else
  82. error "Failed to fetch #{interpolated['url']}: #{response.inspect}"
  83. end
  84. end
  85. protected
  86. def get_entry_id(entry)
  87. entry.id.presence || Digest::MD5.hexdigest(entry.content)
  88. end
  89. def check_and_track(entry_id)
  90. memory['seen_ids'] ||= []
  91. if memory['seen_ids'].include?(entry_id)
  92. false
  93. else
  94. memory['seen_ids'].unshift entry_id
  95. memory['seen_ids'].pop if memory['seen_ids'].length > 500
  96. true
  97. end
  98. end
  99. end
  100. end