class DownloadTV::TorrentAPI
TorrentAPI.org grabber Interfaces with torrentapi.org/apidocs_v2.txt
Constants
- TOKEN_EXPIRED_ERROR
- TOO_MANY_REQUESTS_ERROR
Public Class Methods
new()
click to toggle source
Calls superclass method
# File lib/download_tv/grabbers/torrentapi.rb, line 11 def initialize super('https://torrentapi.org/pubapi_v2.php?'\ 'mode=search&search_string=%s&token=%s&'\ 'app_id=DownloadTV&sort=seeders') @wait = 0.5 @token = nil end
Public Instance Methods
get_links(show)
click to toggle source
# File lib/download_tv/grabbers/torrentapi.rb, line 53 def get_links(show) renew_token if @token.nil? search = format(@url, show, @token) obj = request_and_parse(search) if obj['error_code'] == TOKEN_EXPIRED_ERROR renew_token search = format(@url, show, @token) obj = request_and_parse(search) end while obj['error_code'] == TOO_MANY_REQUESTS_ERROR sleep(@wait) obj = request_and_parse(search) 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) rescue Mechanize::ResponseCodeError => e if e.response_code == '429' sleep(@wait) retry end end
online?()
click to toggle source
Specific implementation for TorrentAPI
(requires token)
# File lib/download_tv/grabbers/torrentapi.rb, line 21 def online? renew_token true rescue Mechanize::ResponseCodeError => e if e.response_code == '429' sleep(@wait) retry end false rescue Net::HTTP::Persistent::Error false end
renew_token()
click to toggle source
Connects to Torrentapi.org and requests a token, returning it Tokens automatically expire every 15 minutes
# File lib/download_tv/grabbers/torrentapi.rb, line 45 def renew_token obj = request_and_parse('https://torrentapi.org/pubapi_v2'\ '.php?get_token=get_token&app_id='\ 'DownloadTV') @token = obj['token'] end
request_and_parse(url)
click to toggle source
Makes a get request tp the given url. Returns the JSON response parsed into a hash
# File lib/download_tv/grabbers/torrentapi.rb, line 37 def request_and_parse(url) page = @agent.get(url).content JSON.parse(page) end