Sha256: bb20cdf42cb2db85bf123222f03452b653ed6a6b9e108555223db36b23835a82

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

module DownloadTV

	class TorrentAPI < LinkGrabber

		attr_accessor :token
		attr_reader :wait

		def initialize
			super("https://torrentapi.org/pubapi_v2.php?mode=search&search_string=%s&token=%s")
			@token = get_token
			@wait = 2.1
			
		end

		##
		# Connects to Torrentapi.org and requests a token.
		# Returns said token.
		def get_token
			page = @agent.get("https://torrentapi.org/pubapi_v2.php?get_token=get_token").content
			
			obj = JSON.parse(page)

			@token = obj['token']

		end

		def get_links(s)

			# Format the url
			search = @url % [s, @token]

			page = @agent.get(search).content
			obj = JSON.parse(page)

			if obj["error_code"]==4 # Token expired
				get_token
				search = @url % [s, @token]
				page = @agent.get(search).content
				obj = JSON.parse(page)
			end

			while obj["error_code"]==5 # Violate 1req/2s limit
				# puts "Torrentapi request limit hit. Wait a few seconds..."
				sleep(@wait) 
				page = @agent.get(search).content
				obj = JSON.parse(page)

			end

			raise NoTorrentsError if obj["error"]

			names = obj["torrent_results"].collect {|i| i["filename"]}
			links = obj["torrent_results"].collect {|i| i["download"]}

			names.zip(links)

		end

	end

end

# Tokens automaticly expire in 15 minutes.
# The api has a 1req/2s limit.
# http://torrentapi.org/apidocs_v2.txt

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
download_tv-2.2.0 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.1.1 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.1.0 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.0.6 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.0.5 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.0.4 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.0.3 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.0.0 lib/download_tv/grabbers/torrentapi.rb
download_tv-1.0.0 lib/download_tv/grabbers/torrentapi.rb