Sha256: 8febb3bc0af6f4f49575a570e5a6ac835bb32febc716b42696a479e7415f3341

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

module DownloadTV
  ##
  # TorrentAPI.org grabber
  # Interfaces with http://torrentapi.org/apidocs_v2.txt
  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&app_id=DownloadTV&sort=seeders')
      @wait = 0.1
    end

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

    ##
    # Connects to Torrentapi.org and requests a token, returning it
    # Tokens automatically expire every 15 minutes
    def renew_token
      page = @agent.get('https://torrentapi.org/pubapi_v2.php?get_token=get_token&app_id=DownloadTV').content

      obj = JSON.parse(page)

      @token = obj['token']
    end

    def get_links(s)
      @token ||= renew_token

      # Format the url
      search = format(@url, s, @token)

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

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

      while obj['error_code'] == 5 # Violate 1req/2s limit
        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)
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
download_tv-2.4.7 lib/download_tv/grabbers/torrentapi.rb
download_tv-2.4.6 lib/download_tv/grabbers/torrentapi.rb