From e83c738a04966e386f5ad18d76348a370f0738b0 Mon Sep 17 00:00:00 2001 From: Vasilis Gerakaris Date: Tue, 9 Oct 2018 11:42:56 +0300 Subject: [PATCH] Conform to PEP8 style --- NyaaPy/__init__.py | 12 +++++----- NyaaPy/nyaa.py | 16 ++++++++----- NyaaPy/pantsu.py | 16 ++++++++----- NyaaPy/sukebei.py | 31 +++++++++++++++++-------- NyaaPy/utils.py | 57 +++++++++++++++++++++++++++------------------- setup.py | 7 +++--- tests/test.py | 5 ++-- 7 files changed, 87 insertions(+), 57 deletions(-) diff --git a/NyaaPy/__init__.py b/NyaaPy/__init__.py index 050a8fc..1c2e563 100644 --- a/NyaaPy/__init__.py +++ b/NyaaPy/__init__.py @@ -1,11 +1,11 @@ # Info about the module -__version__ = '0.6.0' -__author__ = 'Juanjo Salvador' -__email__ = 'juanjosalvador@netc.eu' -__url__ = 'http://juanjosalvador.me' +__version__ = '0.6.0' +__author__ = 'Juanjo Salvador' +__email__ = 'juanjosalvador@netc.eu' +__url__ = 'http://juanjosalvador.me' __copyright__ = '2017 Juanjo Salvador' -__license__ = 'MIT license' +__license__ = 'MIT license' from NyaaPy.nyaa import Nyaa from NyaaPy.pantsu import Pantsu -from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu \ No newline at end of file +from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu diff --git a/NyaaPy/nyaa.py b/NyaaPy/nyaa.py index bc423c2..071b536 100644 --- a/NyaaPy/nyaa.py +++ b/NyaaPy/nyaa.py @@ -5,8 +5,9 @@ from NyaaPy.utils import Utils utils = Utils() + class Nyaa: - + def __init__(self): self.URI = "http://nyaa.si" @@ -23,9 +24,12 @@ class Nyaa: user_uri = "" if page > 0: - r = requests.get("{}/{}?f={}&c={}_{}&q={}&p={}".format(self.URI, user_uri, filters, category, subcategory, keyword, page)) + r = requests.get("{}/{}?f={}&c={}_{}&q={}&p={}".format( + self.URI, user_uri, filters, category, subcategory, keyword, + page)) else: - r = requests.get("{}/{}?f={}&c={}_{}&q={}".format(self.URI, user_uri, filters, category, subcategory, keyword)) + r = requests.get("{}/{}?f={}&c={}_{}&q={}".format( + self.URI, user_uri, filters, category, subcategory, keyword)) soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') @@ -35,8 +39,8 @@ class Nyaa: def get(self, id): r = requests.get("{}/view/{}".format(self.URI, id)) soup = BeautifulSoup(r.text, 'html.parser') - content = soup.findAll("div", { "class": "panel", "id": None}) - + content = soup.findAll("div", {"class": "panel", "id": None}) + return utils.parse_single(content) def get_user(self, username): @@ -50,4 +54,4 @@ class Nyaa: soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') - return utils.parse_nyaa(rows, limit=number_of_results + 1) \ No newline at end of file + return utils.parse_nyaa(rows, limit=number_of_results + 1) diff --git a/NyaaPy/pantsu.py b/NyaaPy/pantsu.py index 116f983..2446db0 100644 --- a/NyaaPy/pantsu.py +++ b/NyaaPy/pantsu.py @@ -3,14 +3,16 @@ from NyaaPy.utils import Utils utils = Utils() + class Pantsu: - + def __init__(self): self.BASE_URL = "https://nyaa.pantsu.cat/api" # Torrents - GET - def search(self, keyword, **kwargs): - request = requests.get("{}/search{}".format(self.BASE_URL, utils.query_builder(keyword, kwargs))) + def search(self, keyword, **kwargs): + request = requests.get("{}/search{}".format( + self.BASE_URL, utils.query_builder(keyword, kwargs))) return request.json() def view(self, item_id): @@ -29,11 +31,13 @@ class Pantsu: # Users def login(self, username, password): - login = requests.post("{}/login/".format(self.BASE_URL), data={'username': username, 'password': password}) + login = requests.post("{}/login/".format( + self.BASE_URL), data={'username': username, 'password': password}) return login.json() def profile(self, user_id): - profile = requests.post("{}/profile/".format(self.BASE_URL), data={'id': user_id}) + profile = requests.post("{}/profile/".format( + self.BASE_URL), data={'id': user_id}) - return profile.json() \ No newline at end of file + return profile.json() diff --git a/NyaaPy/sukebei.py b/NyaaPy/sukebei.py index a7fed9c..1aae596 100644 --- a/NyaaPy/sukebei.py +++ b/NyaaPy/sukebei.py @@ -2,6 +2,7 @@ import requests from bs4 import BeautifulSoup from NyaaPy.utils import Utils as utils + class SukebeiNyaa: def search(self, keyword, **kwargs): category = kwargs.get('category', 0) @@ -10,9 +11,13 @@ class SukebeiNyaa: page = kwargs.get('page', 0) if page > 0: - r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format("http://sukebei.nyaa.si", filters, category, subcategory, keyword, page)) + r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format( + "http://sukebei.nyaa.si", filters, category, subcategory, + keyword, page)) else: - r = requests.get("{}/?f={}&c={}_{}&q={}".format("http://sukebei.nyaa.si", filters, category, subcategory, keyword)) + r = requests.get("{}/?f={}&c={}_{}&q={}".format( + "http://sukebei.nyaa.si", filters, category, subcategory, + keyword)) soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') @@ -22,8 +27,8 @@ class SukebeiNyaa: def get(self, id): r = requests.get("http://sukebei.nyaa.si/view/{}".format(id)) soup = BeautifulSoup(r.text, 'html.parser') - content = soup.findAll("div", { "class": "panel", "id": None}) - + content = soup.findAll("div", {"class": "panel", "id": None}) + return utils.parse_single(content) def get_user(self, username): @@ -39,17 +44,20 @@ class SukebeiNyaa: return utils.parse_sukebei(rows, limit=number_of_results + 1) + class SukebeiPantsu: BASE_URL = "https://sukebei.pantsu.cat/api" # Torrents - GET def search(self, keyword, **kwargs): - request = requests.get("{}/search{}".format(SukebeiPantsu.BASE_URL, utils.query_builder(keyword, kwargs))) - + request = requests.get("{}/search{}".format( + SukebeiPantsu.BASE_URL, utils.query_builder(keyword, kwargs))) + return request.json() def view(self, item_id): - request = requests.get("{}/view/{}".format(SukebeiPantsu.BASE_URL, item_id)) + request = requests.get("{}/view/{}".format( + SukebeiPantsu.BASE_URL, item_id)) return request.json() @@ -64,11 +72,14 @@ class SukebeiPantsu: # Users def login(self, username, password): - login = requests.post("{}/login/".format(SukebeiPantsu.BASE_URL), data={'username': username, 'password': password}) + login = requests.post("{}/login/".format( + SukebeiPantsu.BASE_URL), data={'username': username, + 'password': password}) return login.json() def profile(self, user_id): - profile = requests.post("{}/profile/".format(SukebeiPantsu.BASE_URL), data={'id': user_id}) + profile = requests.post("{}/profile/".format( + SukebeiPantsu.BASE_URL), data={'id': user_id}) - return profile.json() \ No newline at end of file + return profile.json() diff --git a/NyaaPy/utils.py b/NyaaPy/utils.py index 8f91d9c..b347247 100644 --- a/NyaaPy/utils.py +++ b/NyaaPy/utils.py @@ -4,6 +4,7 @@ import re + class Utils: def nyaa_categories(self, b): c = b.replace('/?c=', '') @@ -37,7 +38,7 @@ class Utils: "3": "Raw" } }, - "4": { + "4": { "name": "Live Action", "subcats": { "1": "English-translated", @@ -46,14 +47,14 @@ class Utils: "4": "Raw" } }, - "5": { + "5": { "name": "Pictures", "subcats": { "1": "Graphics", "2": "Photos" } }, - "6": { + "6": { "name": "Software", "subcats": { "1": "Applications", @@ -61,10 +62,11 @@ class Utils: } } } - + try: - category_name = "{} - {}".format(categories[cat]['name'], categories[cat]['subcats'][subcat]) - except: + category_name = "{} - {}".format( + categories[cat]['name'], categories[cat]['subcats'][subcat]) + except Exception: pass return category_name @@ -107,7 +109,7 @@ class Utils: torrents.append(torrent) except IndexError as ie: pass - + return torrents def parse_single(self, content): @@ -119,13 +121,14 @@ class Utils: for div in row.find_all('div', {'class': 'col-md-5'}): data.append(div.text.replace("\n", "")) - files = content[2].find('div', {'class', 'torrent-file-list'}).find_all('li') + files = content[2].find('div', + {'class', 'torrent-file-list'}).find_all('li') for file in files: torrent_files.append(file.text) - - torrent['title'] = re.sub('\n|\r|\t', '', content[0].find('h3', {"class": "panel-title"}).text.replace("\n", "")) + torrent['title'] = re.sub('\n|\r|\t', '', content[0].find('h3', { + "class": "panel-title"}).text.replace("\n", "")) torrent['category'] = data[0] torrent['uploader'] = data[2] torrent['uploader_profile'] = "https://nyaa.si/user/{}".format(data[2]) @@ -136,7 +139,8 @@ class Utils: torrent['leechers'] = data[5] torrent['completed'] = data[7] torrent['hash'] = data[8] - torrent['description'] = re.sub('\t', '', content[1].find('div', {'id': 'torrent-description'}).text) + torrent['description'] = re.sub('\t', '', content[1].find('div', { + 'id': 'torrent-description'}).text) torrent['files'] = torrent_files return torrent @@ -158,14 +162,15 @@ class Utils: if td.text.rstrip(): block.append(td.text.rstrip()) - + try: torrent = { 'id': block[1].replace("/view/", ""), 'category': Utils.sukebei_categories(self, block[0]), 'url': "http://sukebei.nyaa.si{}".format(block[1]), 'name': block[2], - 'download_url': "http://sukebei.nyaa.si{}".format(block[4]), + 'download_url': "http://sukebei.nyaa.si{}".format( + block[4]), 'magnet': block[5], 'size': block[6], 'date': block[7], @@ -173,9 +178,9 @@ class Utils: 'leechers': block[9], 'completed_downloads': block[10], } - + torrents.append(torrent) - + return torrents def sukebei_categories(self, b): @@ -204,32 +209,36 @@ class Utils: } } } - + try: - category_name = "{} - {}".format(categories[cat]['name'], categories[cat]['subcats'][subcat]) - except: + category_name = "{} - {}".format( + categories[cat]['name'], categories[cat]['subcats'][subcat]) + except Exception: pass return category_name # Pantsu Utils def query_builder(self, q, params): - available_params = ["category", "page", "limit", "userID", "fromID", "status", "maxage", "toDate", "fromDate",\ - "dateType", "minSize", "maxSize", "sizeType", "sort", "order", "lang"] + available_params = ["category", "page", "limit", "userID", "fromID", + "status", "maxage", "toDate", "fromDate", + "dateType", "minSize", "maxSize", "sizeType", + "sort", "order", "lang"] query = "?q={}".format(q.replace(" ", "+")) for param, value in params.items(): if param in available_params: - if param != "category" and param != "status" and param != "lang": + if (param != "category" and param != "status" and + param != "lang"): query += "&{}={}".format(param, value) elif param == "category": query += "&c={}_{}".format(value[0], value[1]) - + elif param == "status": query += "&s={}".format(value) - + elif param == "lang": for lang in value: query += "&lang={}".format(lang) - + return query diff --git a/setup.py b/setup.py index e9bae01..82141be 100644 --- a/setup.py +++ b/setup.py @@ -7,14 +7,15 @@ with open(path.join(currdir, 'README.md'), encoding='utf-8') as f: setup(name='nyaapy', version='0.6.3', - install_requires = [ + install_requires=[ "requests", "beautifulsoup4", ], url='https://github.com/juanjosalvador/nyaapy', long_description=long_desc, - long_description_content_type='text/markdown', - download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.6.2.tar.gz', + long_description_content_type='text/markdown', + download_url=('https://github.com/juanjosalvador/' + 'nyaapy/archive/0.6.2.tar.gz'), license='MIT', author='Juanjo Salvador', author_email='juanjosalvador@netc.eu', diff --git a/tests/test.py b/tests/test.py index a956284..0455e34 100644 --- a/tests/test.py +++ b/tests/test.py @@ -3,5 +3,6 @@ from NyaaPy import Pantsu, Nyaa pantsu = Pantsu() nyaa = Nyaa() -#print(pantsu.search(keyword='koe no katachi', lang=["es", "ja"], category=[1, 3])) -print(nyaa.search(keyword='yuru camp')) \ No newline at end of file +# print(pantsu.search(keyword='koe no katachi', +# lang=["es", "ja"], category=[1, 3])) +print(nyaa.search(keyword='yuru camp'))