From 1e2ba25882c19d48f926c56576fb39ea0d1ecce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Salvador?= Date: Thu, 8 Mar 2018 23:33:54 +0100 Subject: [PATCH 1/2] Fix Pylint errors (optimized code) --- .gitignore | 4 +++- NyaaPy/nyaa.py | 26 +++++++++++++++----------- NyaaPy/pantsu.py | 29 ++++++++++++++++------------- NyaaPy/sukebei.py | 20 ++++++++++---------- NyaaPy/utils.py | 17 ++++++++--------- tests/test.py | 4 +++- 6 files changed, 55 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 9e4ea62..6f7eb31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build/ dist/ nyaapy.egg-info -.vscode \ No newline at end of file +.vscode +env/ +*.pyc \ No newline at end of file diff --git a/NyaaPy/nyaa.py b/NyaaPy/nyaa.py index a84870d..16440e0 100644 --- a/NyaaPy/nyaa.py +++ b/NyaaPy/nyaa.py @@ -1,41 +1,45 @@ import requests from bs4 import BeautifulSoup -from NyaaPy.utils import Utils as utils +from NyaaPy.utils import Utils + +utils = Utils() class Nyaa: - URI = "http://nyaa.si" + + def __init__(self): + self.URI = "http://nyaa.si" - def search(keyword, **kwargs): + def search(self, keyword, **kwargs): category = kwargs.get('category', 0) subcategory = kwargs.get('subcategory', 0) filters = kwargs.get('filters', 0) page = kwargs.get('page', 0) if page > 0: - r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format(Nyaa.URI, filters, category, subcategory, keyword, page)) + r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format(self.URI, filters, category, subcategory, keyword, page)) else: - r = requests.get("{}/?f={}&c={}_{}&q={}".format(Nyaa.URI, filters, category, subcategory, keyword)) + r = requests.get("{}/?f={}&c={}_{}&q={}".format(self.URI, filters, category, subcategory, keyword)) soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') return utils.parse_nyaa(rows, limit=None) - def get(id): - r = requests.get("{}/view/{}".format(Nyaa.URI, id)) + 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}) return utils.parse_single(content) - def get_user(username): - r = requests.get("{}/user/{}".format(Nyaa.URI, username)) + def get_user(self, username): + r = requests.get("{}/user/{}".format(self.URI, username)) soup = BeautifulSoup(r.text, 'html.parser') return utils.parse_nyaa(soup.select('table tr'), limit=None) - def news(number_of_results): - r = requests.get(Nyaa.URI) + def news(self, number_of_results): + r = requests.get(self.URI) soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') diff --git a/NyaaPy/pantsu.py b/NyaaPy/pantsu.py index 4eb0852..116f983 100644 --- a/NyaaPy/pantsu.py +++ b/NyaaPy/pantsu.py @@ -1,36 +1,39 @@ import requests -from NyaaPy.utils import Utils as utils +from NyaaPy.utils import Utils -class Pantsu: +utils = Utils() - BASE_URL = "https://nyaa.pantsu.cat/api" +class Pantsu: + + def __init__(self): + self.BASE_URL = "https://nyaa.pantsu.cat/api" # Torrents - GET - def search(keyword, **kwargs): - request = requests.get("{}/search{}".format(Pantsu.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(item_id): - request = requests.get("{}/view/{}".format(Pantsu.BASE_URL, item_id)) + def view(self, item_id): + request = requests.get("{}/view/{}".format(self.BASE_URL, item_id)) return request.json() # Torrents - POST - def upload(): + def upload(self): return "Work in progress!" - def update(): + def update(self): return "Work in progress!" # Users - def login(username, password): - login = requests.post("{}/login/".format(Pantsu.BASE_URL), data={'username': username, 'password': password}) + def login(self, username, password): + login = requests.post("{}/login/".format(self.BASE_URL), data={'username': username, 'password': password}) return login.json() - def profile(user_id): - profile = requests.post("{}/profile/".format(Pantsu.BASE_URL), data={'id': user_id}) + def profile(self, user_id): + profile = requests.post("{}/profile/".format(self.BASE_URL), data={'id': user_id}) return profile.json() \ No newline at end of file diff --git a/NyaaPy/sukebei.py b/NyaaPy/sukebei.py index 5819cc3..a7fed9c 100644 --- a/NyaaPy/sukebei.py +++ b/NyaaPy/sukebei.py @@ -3,7 +3,7 @@ from NyaaPy.utils import Utils as utils class SukebeiNyaa: - def search(keyword, **kwargs): + def search(self, keyword, **kwargs): category = kwargs.get('category', 0) subcategory = kwargs.get('subcategory', 0) filters = kwargs.get('filters', 0) @@ -19,20 +19,20 @@ def search(keyword, **kwargs): return utils.parse_nyaa(rows, limit=None) - def get(id): + 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}) return utils.parse_single(content) - def get_user(username): + def get_user(self, username): r = requests.get("http://sukebei.nyaa.si/user/{}".format(username)) soup = BeautifulSoup(r.text, 'html.parser') return utils.parse_nyaa(soup.select('table tr'), limit=None) - def news(number_of_results): + def news(self, number_of_results): r = requests.get("http://sukebei.nyaa.si/") soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') @@ -43,32 +43,32 @@ class SukebeiPantsu: BASE_URL = "https://sukebei.pantsu.cat/api" # Torrents - GET - def search(keyword, **kwargs): + def search(self, keyword, **kwargs): request = requests.get("{}/search{}".format(SukebeiPantsu.BASE_URL, utils.query_builder(keyword, kwargs))) return request.json() - def view(item_id): + def view(self, item_id): request = requests.get("{}/view/{}".format(SukebeiPantsu.BASE_URL, item_id)) return request.json() # Torrents - POST - def upload(): + def upload(self): return "Work in progress!" - def update(): + def update(self): return "Work in progress!" # Users - def login(username, password): + def login(self, username, password): login = requests.post("{}/login/".format(SukebeiPantsu.BASE_URL), data={'username': username, 'password': password}) return login.json() - def profile(user_id): + def profile(self, user_id): profile = requests.post("{}/profile/".format(SukebeiPantsu.BASE_URL), data={'id': user_id}) return profile.json() \ No newline at end of file diff --git a/NyaaPy/utils.py b/NyaaPy/utils.py index e4de540..726365d 100644 --- a/NyaaPy/utils.py +++ b/NyaaPy/utils.py @@ -5,8 +5,7 @@ import re class Utils: - - def nyaa_categories(b): + def nyaa_categories(self, b): c = b.replace('/?c=', '') cats = c.split('_') @@ -70,7 +69,7 @@ def nyaa_categories(b): return category_name - def parse_nyaa(table_rows, limit): + def parse_nyaa(self, table_rows, limit): if limit == 0: limit = len(table_rows) @@ -93,7 +92,7 @@ def parse_nyaa(table_rows, limit): try: torrent = { 'id': block[1].replace("/view/", ""), - 'category': Utils.nyaa_categories(block[0]), + 'category': Utils.nyaa_categories(self, block[0]), 'url': "http://nyaa.si{}".format(block[1]), 'name': block[2], 'download_url': "http://nyaa.si{}".format(block[4]), @@ -111,7 +110,7 @@ def parse_nyaa(table_rows, limit): return torrents - def parse_single(content): + def parse_single(self, content): torrent = {} data = [] torrent_files = [] @@ -142,7 +141,7 @@ def parse_single(content): return torrent - def parse_sukebei(table_rows, limit): + def parse_sukebei(self, table_rows, limit): if limit == 0: limit = len(table_rows) @@ -165,7 +164,7 @@ def parse_sukebei(table_rows, limit): try: torrent = { 'id': block[1].replace("/view/", ""), - 'category': Utils.sukebei_categories(block[0]), + '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]), @@ -183,7 +182,7 @@ def parse_sukebei(table_rows, limit): return torrents - def sukebei_categories(b): + def sukebei_categories(self, b): c = b.replace('/?c=', '') cats = c.split('_') @@ -218,7 +217,7 @@ def sukebei_categories(b): return category_name # Pantsu Utils - def query_builder(q, params): + def query_builder(self, q, params): available_params = ["category", "page", "limit", "userID", "fromID", "status", "maxage", "toDate", "fromDate",\ "dateType", "minSize", "maxSize", "sizeType", "sort", "order", "lang"] query = "?q={}".format(q.replace(" ", "+")) diff --git a/tests/test.py b/tests/test.py index c37dbb9..2d1132b 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,3 +1,5 @@ from NyaaPy import Pantsu -print(Pantsu.search('koe no katachi', lang=["es", "ja"], category=[1, 3])) \ No newline at end of file +pantsu = Pantsu() + +print(pantsu.search(keyword='koe no katachi', lang=["es", "ja"], category=[1, 3])) \ No newline at end of file From fbdd79bd5217753266fd7ef397889acf20a0cd25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Salvador?= Date: Thu, 8 Mar 2018 23:49:27 +0100 Subject: [PATCH 2/2] Fix #36 --- NyaaPy/nyaa.py | 11 +++++++++-- setup.py | 6 +++--- tests/test.py | 6 ++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/NyaaPy/nyaa.py b/NyaaPy/nyaa.py index 16440e0..bc423c2 100644 --- a/NyaaPy/nyaa.py +++ b/NyaaPy/nyaa.py @@ -1,4 +1,5 @@ import requests +import urllib.parse from bs4 import BeautifulSoup from NyaaPy.utils import Utils @@ -10,15 +11,21 @@ def __init__(self): self.URI = "http://nyaa.si" def search(self, keyword, **kwargs): + user = kwargs.get('user', None) category = kwargs.get('category', 0) subcategory = kwargs.get('subcategory', 0) filters = kwargs.get('filters', 0) page = kwargs.get('page', 0) + if user: + user_uri = "user/{}".format(user) + else: + user_uri = "" + if page > 0: - r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format(self.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, 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') diff --git a/setup.py b/setup.py index 0424241..2739939 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,13 @@ from setuptools import setup, find_packages setup(name='nyaapy', - version='0.6.1', + version='0.6.2', install_requires = [ "requests", - "bs4", + "beautifulsoup4", ], url='https://github.com/juanjosalvador/nyaapy', - download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.6.1.tar.gz', + 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 2d1132b..a956284 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,5 +1,7 @@ -from NyaaPy import Pantsu +from NyaaPy import Pantsu, Nyaa pantsu = Pantsu() +nyaa = Nyaa() -print(pantsu.search(keyword='koe no katachi', lang=["es", "ja"], category=[1, 3])) \ 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')) \ No newline at end of file