class FeedTorrents::Feed::Download

Public Class Methods

new(title, link, timeout, directory) click to toggle source
# File lib/feed_torrents/feed/download.rb, line 9
def initialize(title, link, timeout, directory)
  @title, @link, @timeout, @directory = title, link, timeout, directory
end

Public Instance Methods

process() click to toggle source
# File lib/feed_torrents/feed/download.rb, line 13
def process
  if FeedTorrents.configuration.filter_testing?
    puts "Would have downloaded torrent for '#{@title}' (#{torrent_link})".bold.cyan
  else
    info "Downloading torrent for '#{@title}' (#{torrent_link})"
    http = EM::HttpRequest.new(torrent_link, inactivity_timeout: @timeout).get
    fh = File.new(file,'wb')

    http.stream { |chunk| fh.write chunk }

    http.errback do
      error "failure retrieving torrent for '#{@title}' (#{torrent_link})"
      error "error: #{http.error}"
    end

    http.callback do
      info "Downloading torrent for #{@title} completed"
      fh.close
      FeedTorrents.store.persist(@link)

      FeedTorrents::Mail.new.send_email(@title, @title)
    end
  end
end

Private Instance Methods

file() click to toggle source
# File lib/feed_torrents/feed/download.rb, line 56
def file
  dir = Pathname.new(@directory).expand_path
  FileUtils.mkdir_p(dir) unless dir.exist?
  dir.join("#{@title}.torrent")
end
is_magnet?() click to toggle source
# File lib/feed_torrents/feed/download.rb, line 52
def is_magnet?
  @link =~ /^magnet:\?/
end