Aucune description http://j1x-huginn.herokuapp.com

rss_agent.rb 4.8KB

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