Class: TransmissionRSS::Aggregator

Inherits:
Object
  • Object
show all
Extended by:
Callback
Defined in:
lib/transmission-rss/aggregator.rb

Overview

Class for aggregating torrent files through RSS feeds.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callback

callback

Constructor Details

#initialize(feeds = [], options = {}) ⇒ Aggregator

Returns a new instance of Aggregator.



17
18
19
# File 'lib/transmission-rss/aggregator.rb', line 17

def initialize(feeds = [], options = {})
  reinitialize!(feeds, options)
end

Instance Attribute Details

#seenObject (readonly)

Returns the value of attribute seen.



15
16
17
# File 'lib/transmission-rss/aggregator.rb', line 15

def seen
  @seen
end

Instance Method Details

#reinitialize!(feeds = [], options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/transmission-rss/aggregator.rb', line 21

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(nil, 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) ⇒ Object

Get file enclosures from all feeds items and call on_new_item callback with torrent file URL as argument.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/transmission-rss/aggregator.rb', line 39

def run(interval = 600)
  @log.debug('aggregator start')

  loop do
    @feeds.each do |feed|
      @log.debug('aggregate ' + feed.url)

      begin
        content = open(feed.url, allow_redirections: :safe).read
      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 = RSS::Parser.parse(content, false).items
      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

    sleep(interval)
  end
end