Sha256: f0d102b09a2312c75eb5f1bffe8a0db02fbc409046cf734d31803afe13c50b70

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

require 'pathname'
require 'fileutils'

module FeedTorrents
  module Feed
    class Download
      include LogFunctions

      def initialize(title, link, timeout, directory)
        @title, @link, @timeout, @directory = title, link, timeout, directory
      end

      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)
          end
        end
      end

      private

      def torrent_link
        @torrent_link ||= determine_link
      end

      def determine_link
        if is_magnet?
          MagnetURI.new(@link).reflektor_link
        else
          @link
        end
      end

      def is_magnet?
        @link =~ /^magnet:\?/
      end

      def file
        dir = Pathname.new(@directory).expand_path
        FileUtils.mkdir_p(dir) unless dir.exist?
        dir.join("#{@title}.torrent")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
feed_torrents-0.1.5 lib/feed_torrents/feed/download.rb
feed_torrents-0.1.4 lib/feed_torrents/feed/download.rb