Nessuna descrizione http://j1x-huginn.herokuapp.com

rss_agent.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * `disable_ssl_verification` - Set to `true` to disable ssl verification.
  20. * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
  21. MD
  22. end
  23. def default_options
  24. {
  25. 'expected_update_period_in_days' => "5",
  26. 'clean' => 'false',
  27. 'url' => "https://github.com/cantino/huginn/commits/master.atom"
  28. }
  29. end
  30. event_description <<-MD
  31. Events look like:
  32. {
  33. "id": "829f845279611d7925146725317b868d",
  34. "date_published": "2014-09-11 01:30:00 -0700",
  35. "last_updated": "Thu, 11 Sep 2014 01:30:00 -0700",
  36. "url": "http://example.com/...",
  37. "urls": [ "http://example.com/..." ],
  38. "description": "Some description",
  39. "content": "Some content",
  40. "title": "Some title",
  41. "authors": [ ... ],
  42. "categories": [ ... ]
  43. }
  44. MD
  45. def working?
  46. event_created_within?((interpolated['expected_update_period_in_days'].presence || 10).to_i) && !recent_error_logs?
  47. end
  48. def validate_options
  49. errors.add(:base, "url is required") unless options['url'].present?
  50. unless options['expected_update_period_in_days'].present? && options['expected_update_period_in_days'].to_i > 0
  51. 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")
  52. end
  53. validate_web_request_options!
  54. end
  55. def check
  56. response = faraday.get(interpolated['url'])
  57. if response.success?
  58. feed = FeedNormalizer::FeedNormalizer.parse(response.body)
  59. feed.clean! if interpolated['clean'] == 'true'
  60. created_event_count = 0
  61. feed.entries.each do |entry|
  62. entry_id = get_entry_id(entry)
  63. if check_and_track(entry_id)
  64. created_event_count += 1
  65. create_event(payload: {
  66. id: entry_id,
  67. date_published: entry.date_published,
  68. last_updated: entry.last_updated,
  69. url: entry.url,
  70. urls: entry.urls,
  71. description: entry.description,
  72. content: entry.content,
  73. title: entry.title,
  74. authors: entry.authors,
  75. categories: entry.categories
  76. })
  77. end
  78. end
  79. log "Fetched #{interpolated['url']} and created #{created_event_count} event(s)."
  80. else
  81. error "Failed to fetch #{interpolated['url']}: #{response.inspect}"
  82. end
  83. end
  84. protected
  85. def get_entry_id(entry)
  86. entry.id.presence || Digest::MD5.hexdigest(entry.content)
  87. end
  88. def check_and_track(entry_id)
  89. memory['seen_ids'] ||= []
  90. if memory['seen_ids'].include?(entry_id)
  91. false
  92. else
  93. memory['seen_ids'].unshift entry_id
  94. memory['seen_ids'].pop if memory['seen_ids'].length > 500
  95. true
  96. end
  97. end
  98. end
  99. end