class FeedTorrents::Feed::List

Attributes

name[R]

Public Class Methods

new(name, options) click to toggle source
# File lib/feed_torrents/feed/list.rb, line 11
def initialize(name, options)
  @name = name
  @options = options

  info "#{name} (enabled: #{enabled?} interval: #{interval} link: #{url})"
end

Public Instance Methods

download_new_items() click to toggle source
# File lib/feed_torrents/feed/list.rb, line 26
def download_new_items
  info "feed #{name}"

  case url
    when /^file:\/\/(.*)$/
      process_raw(File.read($1))
    when /^https?:\/\//
      http = EM::HttpRequest.new(url, inactivity_timeout: timeout).get

      http.errback do
        error "failure retrieving list #{name}"
        error "error: #{http.error}"
      end

      http.callback do
        process_raw http.response
      end
  end
end
enabled?() click to toggle source
# File lib/feed_torrents/feed/list.rb, line 22
def enabled?
  @options[:enabled]
end
interval() click to toggle source
# File lib/feed_torrents/feed/list.rb, line 18
def interval
  @options[:interval].to_i >= 1 ? @options[:interval].to_i : 3600
end

Private Instance Methods

already_processed?(item) click to toggle source
# File lib/feed_torrents/feed/list.rb, line 94
def already_processed?(item)
  FeedTorrents.store.present?(item)
end
matches_filter?(title) click to toggle source
# File lib/feed_torrents/feed/list.rb, line 83
def matches_filter?(title)
  regex_filters.each do |filter|
    if title =~ filter
      puts "Filter #{filter.inspect} matches: #{title}".bold.red if FeedTorrents.configuration.filter_testing?
      return true
    end
  end

  false
end
process(title, link) click to toggle source
# File lib/feed_torrents/feed/list.rb, line 73
def process(title, link)
  info "Processing: #{title}"

  Download.new(title, link, timeout, @options[:directory]).process
end
process_raw(contents) click to toggle source
# File lib/feed_torrents/feed/list.rb, line 56
def process_raw(contents)
  rss = SimpleRSS.parse contents

  rss.items.each do |item|
    link = CGI.unescapeHTML(item.link)
    title = CGI.unescapeHTML(item.title)
    title = title.force_encoding('UTF-8')


    if matches_filter?(title)
      process(title, link) if FeedTorrents.configuration.filter_testing? || !already_processed?(link)
    end
  end
rescue Exception => e
  error "list #{@name} caused exception #{e.class}: #{e.message}"
end
regex_filters() click to toggle source
# File lib/feed_torrents/feed/list.rb, line 79
def regex_filters
  @options[:regex_filters].map { |regex| /#{regex}/ }
end
timeout() click to toggle source
# File lib/feed_torrents/feed/list.rb, line 52
def timeout
  @options[:timeout].to_i || 30
end
url() click to toggle source
# File lib/feed_torrents/feed/list.rb, line 48
def url
  @options[:url]
end