diff --git a/NyaaPy/__init__.py b/NyaaPy/__init__.py index 5ca75df..050a8fc 100644 --- a/NyaaPy/__init__.py +++ b/NyaaPy/__init__.py @@ -7,4 +7,5 @@ __copyright__ = '2017 Juanjo Salvador' __license__ = 'MIT license' from NyaaPy.nyaa import Nyaa -from NyaaPy.pantsu import Pantsu \ No newline at end of file +from NyaaPy.pantsu import Pantsu +from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu \ No newline at end of file diff --git a/NyaaPy/nyaa.py b/NyaaPy/nyaa.py index ec8cbf9..a84870d 100644 --- a/NyaaPy/nyaa.py +++ b/NyaaPy/nyaa.py @@ -3,6 +3,8 @@ from bs4 import BeautifulSoup from NyaaPy.utils import Utils as utils class Nyaa: + URI = "http://nyaa.si" + def search(keyword, **kwargs): category = kwargs.get('category', 0) subcategory = kwargs.get('subcategory', 0) @@ -10,9 +12,9 @@ class Nyaa: page = kwargs.get('page', 0) if page > 0: - r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format("http://nyaa.si", filters, category, subcategory, keyword, page)) + r = requests.get("{}/?f={}&c={}_{}&q={}&p={}".format(Nyaa.URI, filters, category, subcategory, keyword, page)) else: - r = requests.get("{}/?f={}&c={}_{}&q={}".format("http://nyaa.si", filters, category, subcategory, keyword)) + r = requests.get("{}/?f={}&c={}_{}&q={}".format(Nyaa.URI, filters, category, subcategory, keyword)) soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') @@ -20,20 +22,20 @@ class Nyaa: return utils.parse_nyaa(rows, limit=None) def get(id): - r = requests.get("http://nyaa.si/view/{}".format(id)) + r = requests.get("{}/view/{}".format(Nyaa.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("http://nyaa.si/user/{}".format(username)) + r = requests.get("{}/user/{}".format(Nyaa.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("http://nyaa.si/") + r = requests.get(Nyaa.URI) soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') diff --git a/NyaaPy/pantsu.py b/NyaaPy/pantsu.py index b25a99e..4eb0852 100644 --- a/NyaaPy/pantsu.py +++ b/NyaaPy/pantsu.py @@ -3,27 +3,34 @@ from NyaaPy.utils import Utils as utils class Pantsu: -# Torrents - GET - def search(keyword, **kwargs): - request = requests.get("{}/search{}".format("https://nyaa.pantsu.cat/api", utils.query_builder(keyword, kwargs))) - + 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))) return request.json() def view(item_id): - request = requests.get("/view/{}".format("https://nyaa.pantsu.cat/api", item_id)) + request = requests.get("{}/view/{}".format(Pantsu.BASE_URL, item_id)) return request.json() # Torrents - POST + def upload(): + return "Work in progress!" + + def update(): + return "Work in progress!" + # Users def login(username, password): - login = requests.post("{}/login/".format(BASE_URL), data={'username': username, 'password': password}) + login = requests.post("{}/login/".format(Pantsu.BASE_URL), data={'username': username, 'password': password}) return login.json() def profile(user_id): - profile = requests.post("{}/profile/".format(BASE_URL), data={'id': user_id}) + profile = requests.post("{}/profile/".format(Pantsu.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 new file mode 100644 index 0000000..5819cc3 --- /dev/null +++ b/NyaaPy/sukebei.py @@ -0,0 +1,74 @@ +import requests +from bs4 import BeautifulSoup +from NyaaPy.utils import Utils as utils + +class SukebeiNyaa: + def search(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("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)) + + soup = BeautifulSoup(r.text, 'html.parser') + rows = soup.select('table tr') + + return utils.parse_nyaa(rows, limit=None) + + def get(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): + 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): + r = requests.get("http://sukebei.nyaa.si/") + soup = BeautifulSoup(r.text, 'html.parser') + rows = soup.select('table tr') + + return utils.parse_sukebei(rows, limit=number_of_results + 1) + +class SukebeiPantsu: + BASE_URL = "https://sukebei.pantsu.cat/api" + + # Torrents - GET + def search(keyword, **kwargs): + request = requests.get("{}/search{}".format(SukebeiPantsu.BASE_URL, utils.query_builder(keyword, kwargs))) + + return request.json() + + def view(item_id): + request = requests.get("{}/view/{}".format(SukebeiPantsu.BASE_URL, item_id)) + + return request.json() + + # Torrents - POST + + def upload(): + return "Work in progress!" + + def update(): + return "Work in progress!" + + # Users + + def login(username, password): + login = requests.post("{}/login/".format(SukebeiPantsu.BASE_URL), data={'username': username, 'password': password}) + + return login.json() + + def profile(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 91b9bcb..e4de540 100644 --- a/NyaaPy/utils.py +++ b/NyaaPy/utils.py @@ -142,6 +142,82 @@ class Utils: return torrent + def parse_sukebei(table_rows, limit): + if limit == 0: + limit = len(table_rows) + + torrents = [] + + for row in table_rows[:limit]: + block = [] + + for td in row.find_all('td'): + if td.find_all('a'): + for link in td.find_all('a'): + if link.get('href')[-9:] != '#comments': + block.append(link.get('href')) + if link.text.rstrip(): + block.append(link.text) + + if td.text.rstrip(): + block.append(td.text.rstrip()) + + try: + torrent = { + 'id': block[1].replace("/view/", ""), + 'category': Utils.sukebei_categories(block[0]), + 'url': "http://sukebei.nyaa.si{}".format(block[1]), + 'name': block[2], + 'download_url': "http://sukebei.nyaa.si{}".format(block[4]), + 'magnet': block[5], + 'size': block[6], + 'date': block[7], + 'seeders': block[8], + 'leechers': block[9], + 'completed_downloads': block[10], + } + + torrents.append(torrent) + except IndexError as ie: + pass + + return torrents + + def sukebei_categories(b): + c = b.replace('/?c=', '') + cats = c.split('_') + + cat = cats[0] + subcat = cats[1] + + categories = { + "1": { + "name": "Art", + "subcats": { + "1": "Anime", + "2": "Doujinshi", + "3": "Games", + "4": "Manga", + "5": "Pictures", + } + }, + "2": { + "name": "Real Life", + "subcats": { + "1": "Photobooks & Pictures", + "2": "Videos" + } + } + } + + try: + category_name = "{} - {}".format(categories[cat]['name'], categories[cat]['subcats'][subcat]) + except: + pass + + return category_name + + # Pantsu Utils def query_builder(q, params): available_params = ["category", "page", "limit", "userID", "fromID", "status", "maxage", "toDate", "fromDate",\ "dateType", "minSize", "maxSize", "sizeType", "sort", "order", "lang"]