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.



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

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

Instance Attribute Details

#seenObject (readonly)

Returns the value of attribute seen.



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

def seen
  @seen
end

Instance Method Details

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object

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



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
72
73
74
75
76
77
# 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