def __init__(self, username=None, password=None, throttle=False): self.session = requests.Session() self.session.headers = headers self.authkey = None self.passkey = None self.username = username self.password = password self.throttle = throttle self.throttler = Throttler(5, 10) self._login()
class CustomWhatAPI: def __init__(self, username=None, password=None, throttle=False): self.session = requests.Session() self.session.headers = headers self.authkey = None self.passkey = None self.username = username self.password = password self.throttle = throttle self.throttler = Throttler(5, 10) self._login() def _login(self): try: login_cache = WhatLoginCache.objects.get() for cookie in pickle.loads(login_cache.cookies): self.session.cookies.set_cookie(cookie) self.authkey = login_cache.authkey self.passkey = login_cache.passkey self.request('index') except Exception: '''Logs in user and gets authkey from server''' loginpage = 'https://{0}/login.php'.format(WHAT_CD_DOMAIN) data = { 'username': self.username, 'password': self.password, 'keeplogged': 1, 'login': '******', } r = self.session.post(loginpage, data=data, allow_redirects=False) if r.status_code != 302: if '<form class="auth_form" name="2fa" id="2fa"' in r.text: raise LoginException( "2FA is enabled on your account, unable to login.") else: raise LoginException("Unsuccessful login attempt.") accountinfo = self.request("index") self.authkey = accountinfo["response"]["authkey"] self.passkey = accountinfo["response"]["passkey"] for cache in WhatLoginCache.objects.all(): cache.delete() login_cache = WhatLoginCache(cookies=pickle.dumps( [c for c in self.session.cookies]), authkey=self.authkey, passkey=self.passkey) login_cache.save() def request(self, action, **kwargs): '''Makes an AJAX request at a given action page''' ajaxpage = 'https://{0}/ajax.php'.format(WHAT_CD_DOMAIN) params = {'action': action} if self.authkey: params['auth'] = self.authkey params.update(kwargs) if self.throttle: self.throttler.throttle_request() r = self.session.get(ajaxpage, params=params, allow_redirects=False) try: json_response = r.json() if json_response["status"] != "success": if json_response['error'] == 'bad id parameter': raise BadIdException(json_response) elif json_response['error'] == 'rate limit exceeded': raise RateLimitExceededException(json_response) raise RequestException( message=json_response['error'] if 'error' in json_response else json_response, response=json_response) return json_response except ValueError: if action == 'torrentlog' and r.content == 'no payload data (empty result set)': return {'status': 'success', 'response': []} raise RequestException() def get_torrent(self, torrent_id): '''Downloads the torrent at torrent_id using the authkey and passkey''' torrentpage = 'https://{0}/torrents.php'.format(WHAT_CD_DOMAIN) params = {'action': 'download', 'id': torrent_id} if self.authkey: params['authkey'] = self.authkey params['torrent_pass'] = self.passkey r = self.session.get(torrentpage, params=params, allow_redirects=False) if r.status_code == 200 and 'application/x-bittorrent' in r.headers[ 'content-type']: filename = re.search('filename="(.*)"', r.headers['content-disposition']).group(1) return filename, r.content return None def get_free_torrents(self): # Start form 1 up for page in count(1): response = self.request('browse', freetorrent=1, page=page)['response'] if response['pages'] > 20 and socket.gethostname( ) == FREELEECH_HOSTNAME: send_freeleech_email('Site-wide freeleech.') raise Exception( 'More than 20 pages of free torrents. Site-wide freeleech?' ) for result in response['results']: yield result if response['currentPage'] == response['pages']: break sleep(2) def get_free_torrent_ids(self): for free_group in self.get_free_torrents(): if 'torrents' in free_group: for torrent in free_group['torrents']: if torrent['isFreeleech']: yield int(torrent['torrentId']), free_group, torrent else: if free_group['isFreeleech']: yield int(free_group['torrentId']), free_group, free_group
class CustomWhatAPI: def __init__(self, username=None, password=None, throttle=False): self.session = requests.Session() self.session.headers = headers self.authkey = None self.passkey = None self.username = username self.password = password self.throttle = throttle self.throttler = Throttler(5, 10) self._login() def _login(self): try: login_cache = WhatLoginCache.objects.get() for cookie in pickle.loads(login_cache.cookies): self.session.cookies.set_cookie(cookie) self.authkey = login_cache.authkey self.passkey = login_cache.passkey self.request('index') except Exception: '''Logs in user and gets authkey from server''' loginpage = 'https://{0}/login.php'.format(WHAT_CD_DOMAIN) data = { 'username': self.username, 'password': self.password, 'keeplogged': 1, 'login': '******', } r = self.session.post(loginpage, data=data, allow_redirects=False) if r.status_code != 302: if '<form class="auth_form" name="2fa" id="2fa"' in r.text: raise LoginException("2FA is enabled on your account, unable to login.") else: raise LoginException("Unsuccessful login attempt.") accountinfo = self.request("index") self.authkey = accountinfo["response"]["authkey"] self.passkey = accountinfo["response"]["passkey"] for cache in WhatLoginCache.objects.all(): cache.delete() login_cache = WhatLoginCache( cookies=pickle.dumps([c for c in self.session.cookies]), authkey=self.authkey, passkey=self.passkey ) login_cache.save() def request(self, action, **kwargs): '''Makes an AJAX request at a given action page''' ajaxpage = 'https://{0}/ajax.php'.format(WHAT_CD_DOMAIN) params = {'action': action} if self.authkey: params['auth'] = self.authkey params.update(kwargs) if self.throttle: self.throttler.throttle_request() r = self.session.get(ajaxpage, params=params, allow_redirects=False) try: json_response = r.json() if json_response["status"] != "success": if json_response['error'] == 'bad id parameter': raise BadIdException(json_response) elif json_response['error'] == 'rate limit exceeded': raise RateLimitExceededException(json_response) raise RequestException( message=json_response['error'] if 'error' in json_response else json_response, response=json_response) return json_response except ValueError: if action == 'torrentlog' and r.content == 'no payload data (empty result set)': return {'status': 'success', 'response': []} raise RequestException() def get_torrent(self, torrent_id): '''Downloads the torrent at torrent_id using the authkey and passkey''' torrentpage = 'https://{0}/torrents.php'.format(WHAT_CD_DOMAIN) params = {'action': 'download', 'id': torrent_id} if self.authkey: params['authkey'] = self.authkey params['torrent_pass'] = self.passkey r = self.session.get(torrentpage, params=params, allow_redirects=False) if r.status_code == 200 and 'application/x-bittorrent' in r.headers['content-type']: filename = re.search('filename="(.*)"', r.headers['content-disposition']).group(1) return filename, r.content return None def get_free_torrents(self): # Start form 1 up for page in count(1): response = self.request('browse', freetorrent=1, page=page)['response'] if response['pages'] > 20 and socket.gethostname() == FREELEECH_HOSTNAME: send_freeleech_email('Site-wide freeleech.') raise Exception('More than 20 pages of free torrents. Site-wide freeleech?') for result in response['results']: yield result if response['currentPage'] == response['pages']: break sleep(2) def get_free_torrent_ids(self): for free_group in self.get_free_torrents(): if 'torrents' in free_group: for torrent in free_group['torrents']: if torrent['isFreeleech']: yield int(torrent['torrentId']), free_group, torrent else: if free_group['isFreeleech']: yield int(free_group['torrentId']), free_group, free_group