From 00f1c4ee7f0e10d7041401cf472a2b8f4094bccf Mon Sep 17 00:00:00 2001 From: JuanjoSalvador Date: Sat, 14 Oct 2017 12:55:54 +0200 Subject: [PATCH 1/3] nyaa.pantsu.cat update --- NyaaPy/nyaapantsu.py | 44 ++++++++++++++++---------------------- NyaaPy/utils.py | 51 ++++++++++++++++++++++++++++++++++++++++++-- tests/test.py | 23 ++++++++------------ 3 files changed, 76 insertions(+), 42 deletions(-) diff --git a/NyaaPy/nyaapantsu.py b/NyaaPy/nyaapantsu.py index 699bada..7074e0c 100644 --- a/NyaaPy/nyaapantsu.py +++ b/NyaaPy/nyaapantsu.py @@ -5,38 +5,30 @@ from NyaaPy.utils import Utils as utils class NyaaPantsu(): ''' - Make a query to nyaa.pantsu.cat using keyword as keyword. - Returns an array of OrderedDict with every result of the query. - Returns an empty array if no results. + Return a list of dicts with the results of the query. ''' - def search(keyword): - nyaapantsu_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q=" + def search(keyword, category, subcategory, filters, page): + if page > 0: + r = requests.get("http://nyaa.pantsu.cat/?f={}&c={}_{}&q={}&p={}".format(filters, category, subcategory, keyword, page)) + else: + r = requests.get("http://nyaa.pantsu.cat/?f={}&c={}_{}&q={}".format(filters, category, subcategory, keyword)) - request = requests.get(nyaa_baseurl + keyword) - response = xmltodict.parse(request.text) + soup = BeautifulSoup(r.text, 'html.parser') + rows = soup.select('table tr') - results = [] + results = {} - try: - if type(response['rss']['channel']['item']) is collections.OrderedDict: - results.append(response['rss']['channel']['item']) - else: - results = response['rss']['channel']['item'] - - except KeyError as ex: - results = [] + if rows: + results = utils.parse_pantsu(rows, limit=None) return results - + ''' - Returns an array of OrderedDict with the n last updates of nyaa.pantsu.cat + Returns an array of dicts with the n last updates of Nyaa.si ''' - def news(n): - nyaa_baseurl = "https://nyaa.pantsu.cat/feed" + def news(number_of_results): + r = requests.get("http://nyaa.pantsu.cat/") + soup = BeautifulSoup(r.text, 'html.parser') + rows = soup.select('table tr') - request = requests.get(nyaa_baseurl) - response = xmltodict.parse(request.text) - - results = response['rss']['channel']['item'] - - return results[:n] \ No newline at end of file + return utils.parse_pantsu(rows, limit=number_of_results) \ No newline at end of file diff --git a/NyaaPy/utils.py b/NyaaPy/utils.py index 253e9e0..7364fd0 100644 --- a/NyaaPy/utils.py +++ b/NyaaPy/utils.py @@ -4,7 +4,7 @@ class Utils(): - def get_categories(b): + def nyaa_categories(b): c = b.replace('/?c=', '') cats = c.split('_') @@ -68,6 +68,15 @@ class Utils(): return category_name + def pantsu_categories(b): + c = b.replace('/search?c=', '') + cats = c.split('_') + + cat = cats[0] + subcat = cats[1] + + return "{} - {}".format(cat, subcat) + def parse_nyaa(table_rows, limit): torrents = [] @@ -88,7 +97,7 @@ class Utils(): try: torrent = { - 'category': Utils.get_categories(block[0]), + 'category': Utils.nyaa_categories(block[0]), 'url': "http://nyaa.si{}".format(block[1]), 'name': block[2], 'download_url': "http://nyaa.si{}".format(block[4]), @@ -104,4 +113,42 @@ class Utils(): except IndexError as ie: pass + return torrents + + def parse_pantsu(table_rows, limit): + + torrents = [] + + limit = limit + 1 + + for row in table_rows[1:limit]: + block = [] + + for td in row.find_all('td'): + if td.find_all('a'): + for link in td.find_all('a'): + if "lang" not in link.get('href'): + block.append(link.get('href')) + + if td.text.rstrip(): + block.append(td.text.replace('\n', '')) + + try: + torrent = { + 'category': Utils.pantsu_categories(block[0]), + 'url': "http://nyaa.pantsu.cat{}".format(block[1]), + 'name': block[2], + 'download_url': block[4], + 'magnet': block[3], + 'size': block[5], + 'date': block[9], + 'seeders': block[6], + 'leechers': block[7], + 'completed_downloads': block[8], + } + + torrents.append(torrent) + except IndexError as ie: + pass + return torrents \ No newline at end of file diff --git a/tests/test.py b/tests/test.py index c170112..fb8671c 100644 --- a/tests/test.py +++ b/tests/test.py @@ -2,10 +2,13 @@ from NyaaPy import Nyaa, NyaaPantsu # Nyaa.si results def nyaa_search(): - nyaa_query = Nyaa.search(keyword='koe no katachi 1080', category=1, subcategory=0, filters=0, page=0) + try: + nyaa_query = Nyaa.search(keyword='koe no katachi 1080', category=1, subcategory=0, page=0) - for nyaa in nyaa_query: - print(nyaa) + for nyaa in nyaa_query: + print(nyaa) + except TypeError as te: + print(te) def nyaa_news(): news = Nyaa.news(number_of_results=5) @@ -15,22 +18,14 @@ def nyaa_news(): # Nyaa.pantsu.cat results def pantsu_search(): pantsu_query = NyaaPantsu.search('new game!!') - if len(pantsu_query) > 0: - for result in pantsu_query: - print(result['title']) - else: - print('Nothing here!') def pantsu_news(): - news = NyaaPantsu.news(5) - - for result in news: - print(result['title']) + print(NyaaPantsu.news(1)) # Uncomment whatever you want to test #nyaa_search() #pantsu_search() -nyaa_news() -#pantsu_news() \ No newline at end of file +#nyaa_news() +pantsu_news() \ No newline at end of file From 76f48b6cda165fc7fa1c2fc74e8cc92df2e79cf8 Mon Sep 17 00:00:00 2001 From: JuanjoSalvador Date: Sat, 14 Oct 2017 13:00:24 +0200 Subject: [PATCH 2/3] simple search added to nyaa.pantsu.cat --- NyaaPy/nyaapantsu.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/NyaaPy/nyaapantsu.py b/NyaaPy/nyaapantsu.py index 7074e0c..7e052bb 100644 --- a/NyaaPy/nyaapantsu.py +++ b/NyaaPy/nyaapantsu.py @@ -5,13 +5,14 @@ from NyaaPy.utils import Utils as utils class NyaaPantsu(): ''' - Return a list of dicts with the results of the query. + Simple search. + Return a list of dicts with the results of the query. ''' def search(keyword, category, subcategory, filters, page): if page > 0: - r = requests.get("http://nyaa.pantsu.cat/?f={}&c={}_{}&q={}&p={}".format(filters, category, subcategory, keyword, page)) + r = requests.get("http://nyaa.pantsu.cat/search/{}?c={}_{}&q={}".format(page, category, subcategory, keyword)) else: - r = requests.get("http://nyaa.pantsu.cat/?f={}&c={}_{}&q={}".format(filters, category, subcategory, keyword)) + r = requests.get("http://nyaa.pantsu.cat/search/?c={}_{}&q={}".format(category, subcategory, keyword)) soup = BeautifulSoup(r.text, 'html.parser') rows = soup.select('table tr') From 19b308104a7049bea63a6c4cd248bc92bfa65dd2 Mon Sep 17 00:00:00 2001 From: JuanjoSalvador Date: Sat, 14 Oct 2017 13:15:05 +0200 Subject: [PATCH 3/3] Removed nyaa.pantsu.cat (fixed #15) --- NyaaPy/__init__.py | 3 +-- NyaaPy/nyaapantsu.py | 35 --------------------------------- NyaaPy/utils.py | 47 -------------------------------------------- README.md | 4 ++-- setup.py | 2 +- 5 files changed, 4 insertions(+), 87 deletions(-) delete mode 100644 NyaaPy/nyaapantsu.py diff --git a/NyaaPy/__init__.py b/NyaaPy/__init__.py index 9dd667f..a6c4040 100644 --- a/NyaaPy/__init__.py +++ b/NyaaPy/__init__.py @@ -6,5 +6,4 @@ __url__ = 'http://juanjosalvador.me' __copyright__ = '2017 Juanjo Salvador' __license__ = 'MIT license' -from NyaaPy.nyaa import Nyaa -from NyaaPy.nyaapantsu import NyaaPantsu \ No newline at end of file +from NyaaPy.nyaa import Nyaa \ No newline at end of file diff --git a/NyaaPy/nyaapantsu.py b/NyaaPy/nyaapantsu.py deleted file mode 100644 index 7e052bb..0000000 --- a/NyaaPy/nyaapantsu.py +++ /dev/null @@ -1,35 +0,0 @@ -import requests -from bs4 import BeautifulSoup -from NyaaPy.utils import Utils as utils - - -class NyaaPantsu(): - ''' - Simple search. - Return a list of dicts with the results of the query. - ''' - def search(keyword, category, subcategory, filters, page): - if page > 0: - r = requests.get("http://nyaa.pantsu.cat/search/{}?c={}_{}&q={}".format(page, category, subcategory, keyword)) - else: - r = requests.get("http://nyaa.pantsu.cat/search/?c={}_{}&q={}".format(category, subcategory, keyword)) - - soup = BeautifulSoup(r.text, 'html.parser') - rows = soup.select('table tr') - - results = {} - - if rows: - results = utils.parse_pantsu(rows, limit=None) - - return results - - ''' - Returns an array of dicts with the n last updates of Nyaa.si - ''' - def news(number_of_results): - r = requests.get("http://nyaa.pantsu.cat/") - soup = BeautifulSoup(r.text, 'html.parser') - rows = soup.select('table tr') - - return utils.parse_pantsu(rows, limit=number_of_results) \ No newline at end of file diff --git a/NyaaPy/utils.py b/NyaaPy/utils.py index 7364fd0..36d5fee 100644 --- a/NyaaPy/utils.py +++ b/NyaaPy/utils.py @@ -68,15 +68,6 @@ class Utils(): return category_name - def pantsu_categories(b): - c = b.replace('/search?c=', '') - cats = c.split('_') - - cat = cats[0] - subcat = cats[1] - - return "{} - {}".format(cat, subcat) - def parse_nyaa(table_rows, limit): torrents = [] @@ -113,42 +104,4 @@ class Utils(): except IndexError as ie: pass - return torrents - - def parse_pantsu(table_rows, limit): - - torrents = [] - - limit = limit + 1 - - for row in table_rows[1:limit]: - block = [] - - for td in row.find_all('td'): - if td.find_all('a'): - for link in td.find_all('a'): - if "lang" not in link.get('href'): - block.append(link.get('href')) - - if td.text.rstrip(): - block.append(td.text.replace('\n', '')) - - try: - torrent = { - 'category': Utils.pantsu_categories(block[0]), - 'url': "http://nyaa.pantsu.cat{}".format(block[1]), - 'name': block[2], - 'download_url': block[4], - 'magnet': block[3], - 'size': block[5], - 'date': block[9], - 'seeders': block[6], - 'leechers': block[7], - 'completed_downloads': block[8], - } - - torrents.append(torrent) - except IndexError as ie: - pass - return torrents \ No newline at end of file diff --git a/README.md b/README.md index db3f34e..c89a270 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # NyaaPy -Unofficial Python module to search into Nyaa.si and nyaa.pantsu.cat. +Unofficial Python module to search into Nyaa.si Supports Python 3+ @@ -23,7 +23,7 @@ Install it using pip. ## Example ```python - from NyaaPy import Nyaa, NyaaPantsu + from NyaaPy import Nyaa nyaa_query = Nyaa.search(keyword='koe no katachi 1080', category=1, subcategory=0, filters=0, page=0) diff --git a/setup.py b/setup.py index e393963..8a36b5b 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup(name='nyaapy', - version='0.4.0', + version='0.4.1', url='https://github.com/juanjosalvador/nyaapy', download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.1.tar.gz', license='MIT',