diff --git a/.gitignore b/.gitignore index f72cc34..9e4ea62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ dist/ nyaapy.egg-info +.vscode \ No newline at end of file diff --git a/NyaaPy/__init__.py b/NyaaPy/__init__.py index e69de29..9dd667f 100644 --- a/NyaaPy/__init__.py +++ b/NyaaPy/__init__.py @@ -0,0 +1,10 @@ +# Info about the module +__version__ = '0.4.1' +__author__ = 'Juanjo Salvador' +__email__ = 'juanjosalvador@netc.eu' +__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 diff --git a/NyaaPy/nyaa.py b/NyaaPy/nyaa.py index 5843968..c7eab47 100644 --- a/NyaaPy/nyaa.py +++ b/NyaaPy/nyaa.py @@ -1,47 +1,33 @@ import requests -import xmltodict -import json -import collections +from bs4 import BeautifulSoup +from NyaaPy.utils import Utils as utils class Nyaa(): - def search(keyword, page): - if not page: - url = "https://nyaa.si/?page=rss&c=1_0&f=0&q={}".format(keyword) + ''' + 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.si/?f={}&c={}_{}&q={}&p={}".format(filters, category, subcategory, keyword, page)) else: - url = "https://nyaa.si/?page=rss&c=1_0&f=0&q={}&p={}".format(keyword, page) + r = requests.get("http://nyaa.si/?f={}&c={}_{}&q={}".format(filters, category, subcategory, keyword)) - request = requests.get(url) - 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_nyaa(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.si/") + soup = BeautifulSoup(r.text, 'html.parser') + rows = soup.select('table tr') -class NyaaPantsu(): - def search(keyword): - nyaapantsu_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q=" - - request = requests.get(nyaa_baseurl + keyword) - response = xmltodict.parse(request.text) - - 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 = [] - - return results + return utils.parse_nyaa(rows, limit=number_of_results) \ No newline at end of file diff --git a/NyaaPy/nyaapantsu.py b/NyaaPy/nyaapantsu.py new file mode 100644 index 0000000..699bada --- /dev/null +++ b/NyaaPy/nyaapantsu.py @@ -0,0 +1,42 @@ +import requests +from bs4 import BeautifulSoup +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. + ''' + def search(keyword): + nyaapantsu_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q=" + + request = requests.get(nyaa_baseurl + keyword) + response = xmltodict.parse(request.text) + + 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 = [] + + return results + + ''' + Returns an array of OrderedDict with the n last updates of nyaa.pantsu.cat + ''' + def news(n): + nyaa_baseurl = "https://nyaa.pantsu.cat/feed" + + request = requests.get(nyaa_baseurl) + response = xmltodict.parse(request.text) + + results = response['rss']['channel']['item'] + + return results[:n] \ No newline at end of file diff --git a/NyaaPy/utils.py b/NyaaPy/utils.py new file mode 100644 index 0000000..253e9e0 --- /dev/null +++ b/NyaaPy/utils.py @@ -0,0 +1,107 @@ +''' + Module utils +''' + +class Utils(): + + def get_categories(b): + c = b.replace('/?c=', '') + cats = c.split('_') + + cat = cats[0] + subcat = cats[1] + + categories = { + "1": { + "name": "Anime", + "subcats": { + "1": "Anime Music Video", + "2": "English-translated", + "3": "Non-English-translated", + "4": "Raw" + } + }, + "2": { + "name": "Audio", + "subcats": { + "1": "Lossless", + "2": "Lossy" + } + }, + "3": { + "name": "Literature", + "subcats": { + "1": "English-translated", + "2": "Non-English-translated", + "3": "Raw" + } + }, + "4": { + "name": "Live Action", + "subcats": { + "1": "English-translated", + "2": "Idol/Promotional Video", + "3": "Non-English-translated", + "4": "Raw" + } + }, + "5": { + "name": "Pictures", + "subcats": { + "1": "Graphics", + "2": "Photos" + } + }, + "6": { + "name": "Software", + "subcats": { + "1": "Applications", + "2": "Games" + } + } + } + + try: + category_name = "{} - {}".format(categories[cat]['name'], categories[cat]['subcats'][subcat]) + except: + pass + + return category_name + + def parse_nyaa(table_rows, limit): + + 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 = { + 'category': Utils.get_categories(block[0]), + 'url': "http://nyaa.si{}".format(block[1]), + 'name': block[2], + 'download_url': "http://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 \ No newline at end of file diff --git a/README.md b/README.md index 4c7d9ed..6d9937a 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,132 @@ Unofficial Python module to search into Nyaa.si and nyaa.pantsu.cat. -Based on [Kylart's Nyaapi](https://github.com/Kylart/Nyaapi). +Supports Python 3+ + +* [Installation](#installation) +* [Example](#example) +* [Methods](#methods) + * [search()](#search) + * [news()](#news) +* [Categories and subcategories](#categories-and-subcategories) +* [Contributions and development](#contributons-and-development) +* [License](#license) -### Installation and ussage +## Installation Install it using pip. pip install nyaapy +## Example + +```python + from NyaaPy import Nyaa, NyaaPantsu + + nyaa_query = Nyaa.search(keyword='koe no katachi 1080', category=1, subcategory=0, filters=0, page=0) + + nyaa_news = Nyaa.news(5) + + if len(nyaa_query) > 0: + for result in nyaa_query: + print(result['title']) + else: + print('Nothing here!') + + for new in nyaa_news: + print(new['title]) +``` + +## Methods + +### search() + +Returns a list of dicts with the search results. + +Parameters: + +* **keyword**: String. Keyword for the search query. +* **category**: Integer. +* **subcategory**: Integer. +* **filters**: Integer. +* **page**: Integer. + +`page` must be between 0 and 1000. + +#### Dict returned for Nyaa.si + +```python + 'category': "Anime - English-translated", + 'url': "https://nyaa.si/view/968600", + 'name': "[HorribleSubs] Shoukoku no Altair - 14 [720p].mkv", + 'download_url': "https://nyaa.si/download/968600.torrent", + 'magnet': + 'size': "317.2 MiB", + 'date': "2017-10-13 20:16", + 'seeders': "538", + 'leechers': "286", + 'completed_downloads': "852" +``` + +### news() + +Parameters: + +* **number_of_results**: Integer + +`number_of_results` must be between 1 and 75. + + +## Categories and subcategories + +List of available categories and subcategories: + +1. Anime. + + 1.1 - Anime Music Video + + 1.2 - English-translated + + 1.3 - Non-English-translated + + 1.4 - Raw + +2. Audio. + + 2.1 - Lossless + + 2.2 - Lossy + +3. Literature. + + 3.1 - English-translated + + 3.2 - Non-English-translated + + 3.3 - Raw + +4. Live Action. + + 4.1 - English-translated + + 4.2 - Idol/Promotional Video + + 4.3 - Non-English-translated + + 4.4 - Raw + +5. Pictures. + + 5.1 - Graphics + + 5.2 - Photos + +6. Software. + + 6.1 - Applications + + 6.2 - Games ### Contributions and development @@ -35,28 +152,6 @@ At this moment there isn't an official Nyaa.si API, so we only can make requests 4. Always use the code into `src` folder, never the package. -### Example code - - from NyaaPy.nyaa import Nyaa - from NyaaPy.nyaa import NyaaPantsu - - # Nyaa.si results - nyaa_query = Nyaa.search('illo que pasa') - - if len(nyaa_query) > 0: - for result in nyaa_query: - print(result['title']) - else: - print('Nothing here!') - - # Nyaa.pantsu.cat results - pantsu_query = NyaaPantsu.search('new game') - if len(pantsu_query) > 0: - for result in pantsu_query: - print(result['title']) - else: - print('Nothing here!') - -### License +## License MIT license. diff --git a/requirements.txt b/requirements.txt index 8d986c4..8983f13 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,2 @@ -certifi==2017.7.27.1 -chardet==3.0.4 -idna==2.5 -requests==2.18.3 -urllib3==1.22 +requests==2.18.1 +beautifulsoup4==4.6.0 \ No newline at end of file diff --git a/setup.py b/setup.py index 290f21a..8a36b5b 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup(name='nyaapy', - version='0.3.1', + version='0.4.1', url='https://github.com/juanjosalvador/nyaapy', download_url = 'https://github.com/juanjosalvador/nyaapy/archive/0.1.tar.gz', license='MIT', diff --git a/src/nyaa.py b/src/nyaa.py deleted file mode 100644 index 9f1454d..0000000 --- a/src/nyaa.py +++ /dev/null @@ -1,54 +0,0 @@ -import requests -import xmltodict -import json -import collections - -class Nyaa: - ''' - Makes a search query to nyaa.si with the given keyword that returns a - RSS file converted into a dictionary that we can use. - ''' - - def search(keyword): - nyaa_baseurl = "https://nyaa.si/?page=rss&c=1_0&f=0&q=" - - request = requests.get(nyaa_baseurl + keyword) - response = xmltodict.parse(request.text) - - 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 = [] - - return results - -class NyaaPantsu: - ''' - Makes a search query to nyaa.pantsu.cat with the given keyword that returns a - RSS file converted into a dictionary that we can use. - ''' - - def search(keyword): - nyaa_baseurl = "https://nyaa.pantsu.cat/feed?c=_&s=0&max=99999&userID=0&q=" - - request = requests.get(nyaa_baseurl + keyword) - response = xmltodict.parse(request.text) - - 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 = [] - - return results diff --git a/tests/test.py b/tests/test.py index ae4202d..c170112 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,20 +1,36 @@ -from NyaaPy.nyaa import Nyaa -from NyaaPy.nyaa import NyaaPantsu -import json +from NyaaPy import Nyaa, NyaaPantsu # Nyaa.si results -nyaa_query = Nyaa.search('koe no katachi 1080') +def nyaa_search(): + nyaa_query = Nyaa.search(keyword='koe no katachi 1080', category=1, subcategory=0, filters=0, page=0) -if len(nyaa_query) > 0: - for result in nyaa_query: - print(result['title']) -else: - print('Nothing here!') + for nyaa in nyaa_query: + print(nyaa) + +def nyaa_news(): + news = Nyaa.news(number_of_results=5) + for n in news: + print(n) # Nyaa.pantsu.cat results -pantsu_query = NyaaPantsu.search('new game!!') -if len(pantsu_query) > 0: - for result in pantsu_query: +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']) -else: - print('Nothing here!') + +# Uncomment whatever you want to test + +#nyaa_search() +#pantsu_search() +nyaa_news() +#pantsu_news() \ No newline at end of file