Class: DownloadTV::TorrentAPI

Inherits:
LinkGrabber show all
Defined in:
lib/download_tv/grabbers/torrentapi.rb

Overview

TorrentAPI.org grabber Interfaces with torrentapi.org/apidocs_v2.txt

Instance Attribute Summary collapse

Attributes inherited from LinkGrabber

#url

Instance Method Summary collapse

Constructor Details

#initializeTorrentAPI

Returns a new instance of TorrentAPI.



11
12
13
14
15
16
# 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.1
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/download_tv/grabbers/torrentapi.rb', line 8

def token
  @token
end

#waitObject (readonly)

Returns the value of attribute wait.



9
10
11
# File 'lib/download_tv/grabbers/torrentapi.rb', line 9

def wait
  @wait
end

Instance Method Details



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/download_tv/grabbers/torrentapi.rb', line 54

def get_links(show)
  @token ||= renew_token

  search = format(@url, show, @token)

  obj = request_and_parse(search)

  if obj['error_code'] == 4 # Token expired
    renew_token
    search = format(@url, show, @token)
    obj = request_and_parse(search)
  end

  while obj['error_code'] == 5 # Violate 1req/2s limit
    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
  else
    warn 'An unexpected error has occurred. Try updating the gem.'
    exit 1
  end
end

#online?Boolean

Specific implementation for TorrentAPI (requires token)

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/download_tv/grabbers/torrentapi.rb', line 20

def online?
  @agent.read_timeout = 2
  renew_token
  true
rescue Mechanize::ResponseCodeError => e
  if e.response_code == '429'
    sleep(@wait)
    retry
  else
    false
  end
rescue Net::HTTP::Persistent::Error => e
  false
end

#renew_tokenObject

Connects to Torrentapi.org and requests a token, returning it Tokens automatically expire every 15 minutes



46
47
48
49
50
51
52
# File 'lib/download_tv/grabbers/torrentapi.rb', line 46

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) ⇒ Object

Makes a get request tp the given url. Returns the JSON response parsed into a hash



38
39
40
41
# File 'lib/download_tv/grabbers/torrentapi.rb', line 38

def request_and_parse(url)
  page = @agent.get(url).content
  JSON.parse(page)
end