Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
whatapi
=======

This is a fork of `isaaczafuta/whatapi <https://github.com/isaaczafuta/whatapi>`_. I have added support for API token authentication. It is also possible set your own values for the throttler

This project is a simple wrapper around the What.cd AJAX API. Also compatible
with what-like trackers such as pth/apollo.

Expand Down Expand Up @@ -34,7 +36,24 @@ To use another tracker:
...


It's strongly recommended that your script implements saving/loading session cookies to prevent overloading the server.
To use API token authentication:
::

>>> import whatapi
>>> apihandle = whatapi.WhatAPI(apiKey='yourAPItoken'))
>>> apihandle.request("browse", searchstr="Talulah Gosh")
...

To set your own throttler:
::

>>> import whatapi
>>> throttler = whatapi.Throttler(num_requests=5, per_seconds=10)
>>> apihandle = whatapi.WhatAPI(apiKey='yourAPItoken', throttler=throttler)
>>> apihandle.request("browse", searchstr="Talulah Gosh")
...

It's strongly recommended that your script implements saving/loading session cookies to prevent overloading the server (only if you use username / password authentication)

Example:

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='whatapi',
version='0.2.0',
version='0.2.1',
description='What.cd API',
long_description=readme,
author='Isaac Zafuta',
Expand Down
2 changes: 1 addition & 1 deletion whatapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .whatapi import WhatAPI
from .whatapi import WhatAPI,Throttler

__version__ = "0.2.0"
46 changes: 32 additions & 14 deletions whatapi/whatapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import requests
import time

headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Accept-Charset': 'utf-8',
'User-Agent': 'whatapi [isaaczafuta]'
}


class LoginException(Exception):
pass
Expand All @@ -21,30 +17,52 @@ class RequestException(Exception):

class WhatAPI:
def __init__(self, config_file=None, username=None, password=None, cookies=None,
server="https://ssl.what.cd", throttler=None):
self.session = requests.Session()
self.session.headers = headers
self.authkey = None
self.passkey = None
self.server = server
self.throttler = Throttler(5, 10) if throttler is None else throttler
server="https://ssl.what.cd", throttler=None, apiKey=None):
if config_file:
config = ConfigParser()
config.read(config_file)
self.username = config.get('login', 'username')
self.password = config.get('login', 'password')
self.apiKey = config.get('login', 'apiKey')
else:
self.username = username
self.password = password
self.apiKey = apiKey

# Setup session
self.session = requests.Session()
headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Accept-Charset': 'utf-8',
'User-Agent': 'whatapi [isaaczafuta]'
}
if apiKey:
headers['Authorization'] = apiKey
self.session.headers = headers

self.authkey = None
self.passkey = None
self.server = server
self.throttler = Throttler(5, 10) if throttler is None else throttler
if cookies:
self.session.cookies = cookies
try:
self._auth()
except RequestException:
self._login()
elif apiKey:
try:
self._try_connection()
except:
print("Likely an invalid api key")
raise
else:
self._login()

def _try_connection(self):
''' Try to connect to tracker '''
self.request("index")

def _auth(self):
'''Gets auth key from server'''
accountinfo = self.request("index")
Expand All @@ -69,7 +87,7 @@ def get_torrent(self, torrent_id, full_response=False):

full_response: Returns the full response object (including headers) instead of a torrent file
'''
torrentpage = self.server + '/torrents.php'
torrentpage = self.server + '/ajax.php'
params = {'action': 'download', 'id': torrent_id}
if self.authkey:
params['authkey'] = self.authkey
Expand Down Expand Up @@ -120,4 +138,4 @@ def throttle_request(self):
if sleep_time > 0:
time.sleep(sleep_time)
self.request_times = self.request_times[1:]
self.request_times.append(request_time)
self.request_times.append(request_time)