Sha256: a2ecff3d600d9ec02a7a821c66bb86d1ba3843d81931543bc777af72effe5817

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module DownloadTV

	class Torrent

		attr_reader :g_names, :g_instances, :n_grabbers

		def grabbers
			["Eztv", "KAT", "ThePirateBay", "TorrentAPI"]
		end

		def initialize(default_grabber=nil)
			@g_names = grabbers
			@g_instances = Array.new
			@n_grabbers = @g_names.size # Initial size
			@tries = @n_grabbers - 1

			# Silently ignores bad names
			@g_names.rotate! @g_names.find_index(default_grabber).to_i+1

			change_grabbers
			
		end

		
		def change_grabbers
			if !@g_names.empty?
				# Instantiates the last element from g_names, popping it
				newt = (DownloadTV.const_get @g_names.pop).new
				newt.test_connection

				@g_instances.unshift newt

			else
				# Rotates the instantiated grabbers
				@g_instances.rotate!

			end

		rescue Mechanize::ResponseCodeError, Net::HTTP::Persistent::Error

			warn "Problem accessing #{newt.class.name}"
			# We won't be using this grabber
			@n_grabbers = @n_grabbers-1
			@tries = @n_grabbers - 1

			change_grabbers

		rescue SocketError, Errno::ECONNRESET, Net::OpenTimeout
			warn "Connection error."
			exit 1
			
		end


		def get_links(show)
			links = @g_instances.first.get_links(show)

			# Reset the counter
			@tries = @n_grabbers - 1
			
			links

		rescue NoTorrentsError
			puts "No torrents found for #{show} using #{@g_instances.first.class.name}"

			# Use next grabber
			if @tries > 0
				@tries-=1
				change_grabbers
				retry

			else # Reset the counter
				@tries = @n_grabbers - 1
				# Handle show not found here!!
				return []
			
			end
			
		end

		
	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
download_tv-2.2.1 lib/download_tv/torrent.rb