From 1c5c85e9d97f93543110822c0c16a7f9f60d366e Mon Sep 17 00:00:00 2001 From: JuanjoSalvador Date: Sun, 13 Aug 2017 17:02:15 +0200 Subject: [PATCH] development version --- NyaaPy/__init__.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++ NyaaPy/nyaa.py | 44 ----------------------- setup.py | 2 +- src/nyaa.py | 54 ---------------------------- tests/test.py | 45 +++++++++++++++++------- 5 files changed, 122 insertions(+), 111 deletions(-) delete mode 100644 NyaaPy/nyaa.py delete mode 100644 src/nyaa.py diff --git a/NyaaPy/__init__.py b/NyaaPy/__init__.py index e69de29..ba26252 100644 --- a/NyaaPy/__init__.py +++ b/NyaaPy/__init__.py @@ -0,0 +1,88 @@ +import requests +import xmltodict +import json +import collections + +# Info about the module +__version__ = '0.4' +__author__ = 'Juanjo Salvador' +__email__ = 'juanjosalvador@netc.eu' +__url__ = 'http://juanjosalvador.me' +__copyright__ = '2017 Juanjo Salvador' +__license__ = 'MIT license' + +class Nyaa(): + ''' + Make a query to nyaa.si 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): + 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 + + ''' + Returns an array of OrderedDict with the n last updates of Nyaa.si + ''' + def news(n): + nyaa_baseurl = "https://nyaa.si/?page=rss" + + request = requests.get(nyaa_baseurl) + response = xmltodict.parse(request.text) + + results = response['rss']['channel']['item'] + + return results[:n] + +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] diff --git a/NyaaPy/nyaa.py b/NyaaPy/nyaa.py deleted file mode 100644 index f27f708..0000000 --- a/NyaaPy/nyaa.py +++ /dev/null @@ -1,44 +0,0 @@ -import requests -import xmltodict -import json -import collections - -class Nyaa(): - 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(): - 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 diff --git a/setup.py b/setup.py index 290f21a..0a7998d 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', 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..b323660 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,20 +1,41 @@ -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('koe no katachi 1080') -if len(nyaa_query) > 0: - for result in nyaa_query: + if len(nyaa_query) > 0: + for result in nyaa_query: + print(result['title']) + else: + print('Nothing here!') + +def nyaa_news(): + news = Nyaa.news(5) + + for result in news: 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: +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()