class TransmissionRSS::Aggregator
Class for aggregating torrent files through RSS feeds.
Attributes
seen[R]
Public Class Methods
new(feeds = [], options = {})
click to toggle source
# File lib/transmission-rss/aggregator.rb, line 18 def initialize(feeds = [], options = {}) reinitialize!(feeds, options) end
Public Instance Methods
reinitialize!(feeds = [], options = {})
click to toggle source
# File lib/transmission-rss/aggregator.rb, line 22 def reinitialize!(feeds = [], options = {}) seen_file = options[:seen_file] # Prepare Array of feeds URLs. @feeds = feeds.map { |config| TransmissionRSS::Feed.new(config) } # Nothing seen, yet. @seen = SeenFile.new(seen_file) # Initialize log instance. @log = Log.instance # Log number of +@seen+ URIs. @log.debug(@seen.size.to_s + ' uris from seenfile') end
run(interval = 600)
click to toggle source
Get file enclosures from all feeds items and call on_new_item callback with torrent file URL as argument.
# File lib/transmission-rss/aggregator.rb, line 40 def run(interval = 600) @log.debug('aggregator start') loop do @feeds.each do |feed| @log.debug('aggregate ' + feed.url) begin content = fetch(feed) rescue StandardError => e @log.debug("retrieval error (#{e.class}: #{e.message})") next end # gzip HTTP Content-Encoding is not automatically decompressed in # Ruby 1.9.3. content = decompress(content) if RUBY_VERSION == '1.9.3' begin items = parse(content) rescue StandardError => e @log.debug("parse error (#{e.class}: #{e.message})") next end items.each do |item| result = process_link(feed, item) next if result.nil? end end if interval == -1 @log.debug('single run mode, exiting') break end sleep(interval) end end
Private Instance Methods
decompress(string)
click to toggle source
# File lib/transmission-rss/aggregator.rb, line 100 def decompress(string) Zlib::GzipReader.new(StringIO.new(string)).read rescue Zlib::GzipFile::Error, Zlib::Error string end
fetch(feed)
click to toggle source
# File lib/transmission-rss/aggregator.rb, line 81 def fetch(feed) options = { allow_redirections: :safe, 'User-Agent' => 'transmission-rss' } unless feed.validate_cert @log.debug('aggregate certificate validation: false') options[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE end # open for URIs is obsolete, URI.open does not work in 2.4 URI.send(:open, feed.url, options).read end
parse(content)
click to toggle source
# File lib/transmission-rss/aggregator.rb, line 96 def parse(content) RSS::Parser.parse(content, false).items end
process_link(feed, item)
click to toggle source
# File lib/transmission-rss/aggregator.rb, line 106 def process_link(feed, item) link = item.enclosure.url rescue item.link # Item contains no link. return if link.nil? # Link is not a String directly. link = link.href if link.class != String # The link is not in +@seen+ Array. unless @seen.include?(link) # Skip if filter defined and not matching. unless feed.matches_regexp?(item.title) @seen.add(link) return end @log.debug('on_new_item event ' + link) download_path = feed.download_path(item.title) begin on_new_item(link, feed, download_path) rescue Client::Unauthorized, Errno::ECONNREFUSED, Timeout::Error @log.debug('not added to seen file ' + link) else @seen.add(link) end end return link end