From 78f9635917199387738d816f27521b5a3c167366 Mon Sep 17 00:00:00 2001 From: Brian Barbe Date: Fri, 5 Jul 2024 11:27:14 -0700 Subject: [PATCH] utorrentapi cleanup * Fix pylint errors in clienttest.py * Fix pylint errors in utorrentapi.py --- app/clienttest.py | 6 +-- app/utorrentapi.py | 126 ++++++++++++++++++++------------------------- 2 files changed, 59 insertions(+), 73 deletions(-) diff --git a/app/clienttest.py b/app/clienttest.py index 3d10c89..7f77a63 100644 --- a/app/clienttest.py +++ b/app/clienttest.py @@ -5,10 +5,10 @@ if apiclient is not None: torrents = apiclient.get_list() - index = 0 + INDEX = 0 for torrent in torrents['torrents']: - name = '%i - %s' % (index, torrent[2]) - index+=1 + name = f'{INDEX} - {torrent[2]}' + INDEX += 1 print(name) # apiclient.get_files(torrent[0]) # apiclient.recheck(torrent[0]) diff --git a/app/utorrentapi.py b/app/utorrentapi.py index 6c0aaf7..25cebc2 100644 --- a/app/utorrentapi.py +++ b/app/utorrentapi.py @@ -1,8 +1,7 @@ import requests -import base64 -import json from lxml import html + class StatusInfo: def __init__(self, data): mask = 1 @@ -23,34 +22,41 @@ def __init__(self, data): self.loaded = (data & mask) != 0 mask *= 2 + class TorrentInfo: def __init__(self, data): - ind = 0 + self.data = data self.hash = data[0] self.status = StatusInfo(data[1]) self.name = data[2] - self.size = data[3] # in bytes - self.percent_progress = data[4] # in mils - self.downloaded = data[5] # in bytes - self.uploaded = data[6] # in bytes - self.ratio = data[7] # in mils - self.upload_speed = data[8] # in bytes per second - self.download_speed = data[9] # in bytes per second - self.eta = data[10] # in seconds + self.size = data[3] # in bytes + self.percent_progress = data[4] # in mils + self.downloaded = data[5] # in bytes + self.uploaded = data[6] # in bytes + self.ratio = data[7] # in mils + self.upload_speed = data[8] # in bytes per second + self.download_speed = data[9] # in bytes per second + self.eta = data[10] # in seconds self.label = data[11] self.peers_connected = data[12] self.peers_in_swarm = data[13] self.seeds_connected = data[14] self.seeds_in_swarm = data[15] - self.availability = data[16] # int in 1/65535 + self.availability = data[16] # int in 1/65535 self.torrent_queue_order = data[17] - self.remaining = data[18] # in bytes + self.remaining = data[18] # in bytes + self.download_url = data[19] + self.status_message = data[21] + self.date_added = data[23] # epoch + self.date_completed = data[24] # epoch + class LabelInfo: def __init__(self, data): self.label = data[0] self.torrents_in_label = data[1] + class TorrentListInfo: def __init__(self, data): self.build = data['build'] @@ -59,20 +65,19 @@ def __init__(self, data): self.torrent_cache_id = data['torrentc'] -class UTorrentAPI(object): - +class UTorrentAPI(): def __init__(self, base_url, username, password): self.base_url = base_url self.username = username self.password = password - self.auth = requests.auth.HTTPBasicAuth(self.username, self.password) - self.token, self.cookies = self._get_token() + self.auth = requests.auth.HTTPBasicAuth(self.username, self.password) + self.token, self.cookies = self._get_token() def _get_token(self): url = self.base_url + '/token.html' - token = -1 - cookies = -1 + token = -1 + cookies = -1 try: response = requests.get(url, auth=self.auth) @@ -82,26 +87,21 @@ def _get_token(self): if response.status_code == 200: xtree = html.fromstring(response.content) token = xtree.xpath('//*[@id="token"]/text()')[0] - guid = response.cookies['GUID'] + guid = response.cookies['GUID'] else: token = -1 - cookies = dict(GUID = guid) + cookies = dict(GUID=guid) except requests.ConnectionError as error: token = 0 cookies = 0 print(error) - except: - print('error') return token, cookies def is_online(self): - if self.token != -1 and self.token != 0: - return True - else: - return False + return bool(self.token not in [-1, 0]) # public sectin --> def get_list(self): @@ -115,13 +115,11 @@ def get_list(self): except requests.ConnectionError as error: print(error) - except: - print('error') return torrents def get_files(self, torrentid): - path = 'action=getfiles&hash=%s' % (torrentid) + path = f'action=getfiles&hash={torrentid}' status, response = self._action(path) files = [] @@ -157,15 +155,12 @@ def remove(self, torrentid): def removedata(self, torrentid): return self._torrentaction('removedata', torrentid) - def recheck(self, torrentid): - return self._torrentaction('recheck', torrentid) - def set_priority(self, torrentid, fileindex, priority): # 0 = Don't Download # 1 = Low Priority # 2 = Normal Priority # 3 = High Priority - path = 'action=%s&hash=%s&p=%s&f=%s' % ('setprio', torrentid, priority, fileindex) + path = f'action=setprio&hash={torrentid}&p={priority}&f={fileindex}' status, response = self._action(path) files = [] @@ -181,34 +176,34 @@ def add_file(self, file_path): file = [] - url = '%s/?%s&token=%s' % (self.base_url, 'action=add-file', self.token) + url = f'{self.base_url}/?action=add-file&token={self.token}' headers = { - 'Content-Type': "multipart/form-data" + 'Content-Type': "multipart/form-data" } - files = {'torrent_file': open(file_path, 'rb')} - - try: - if files: - response = requests.post(url, files=files, auth=self.auth, cookies=self.cookies) - if response.status_code == 200: - file = response.json() - print('file added') + with open(file_path, 'rb') as fref: + files = {'torrent_file': fref} + + try: + if files: + response = requests.post( + url, files=files, headers=headers, auth=self.auth, + cookies=self.cookies) + if response.status_code == 200: + file = response.json() + print('file added') + else: + print(response.status_code) else: - print(response.status_code) - else: - print('file not found') + print('file not found') - pass - except requests.ConnectionError as error: - print(error) - except Exception as e: - print(e) + except requests.ConnectionError as error: + print(error) - return file + return file - def add_url(self, fiel_path): - path = 'action=add-url&s=%s' % (fiel_path) + def add_url(self, file_path): + path = f'action=add-url&s={file_path}' status, response = self._action(path) files = [] @@ -219,20 +214,14 @@ def add_url(self, fiel_path): else: print(response.status_code) - pass except requests.ConnectionError as error: print(error) - except Exception as e: - print(e) - - print(files) return files - # private section --> def _torrentaction(self, action, torrentid): - path = 'action=%s&hash=%s' % (action, torrentid) + path = f'action={action}&hash={torrentid}' files = [] @@ -246,24 +235,21 @@ def _torrentaction(self, action, torrentid): except requests.ConnectionError as error: print(error) - except: - print('error') return files def _action(self, path): - url = '%s/?%s&token=%s' % (self.base_url, path, self.token) + url = f'{self.base_url}/?{path}&token={self.token}' headers = { - 'Content-Type': "application/json" + 'Content-Type': "application/json" } try: - response = requests.get(url, auth=self.auth, cookies=self.cookies, headers=headers) - # use utf8 for multi-language + response = requests.get( + url, auth=self.auth, cookies=self.cookies, headers=headers) + # use utf8 for multi-language # default is ISO-8859-1 response.encoding = 'utf8' except requests.ConnectionError as error: print(error) - except: - pass return response.status_code, response