From c0283c57c3533fe7654e5d79727e5d828a30093b Mon Sep 17 00:00:00 2001 From: JuanjoSalvador Date: Fri, 13 Oct 2017 00:28:55 +0200 Subject: [PATCH] need to fix the cat - subcat extraction --- NyaaPy/__init__.py | 47 ++++++++++++++++++++++++++++++++++------------ tests/test.py | 31 ++++-------------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/NyaaPy/__init__.py b/NyaaPy/__init__.py index 81b46d4..3899790 100644 --- a/NyaaPy/__init__.py +++ b/NyaaPy/__init__.py @@ -1,5 +1,4 @@ import requests -import json from bs4 import BeautifulSoup # Info about the module @@ -15,7 +14,7 @@ class Nyaa(): Return a list of dicts with the results of the query. ''' def search(keyword, category, subcategory, filters, page): - if page: + if page > 0: r = requests.get("http://nyaa.si/?f={}&c={}_{}&q={}&p={}".format(filters, category, subcategory, keyword, page)) else: r = requests.get("http://nyaa.si/?f={}&c={}_{}&q={}".format(filters, category, subcategory, keyword)) @@ -41,12 +40,8 @@ class Nyaa(): block.append(td.text.rstrip()) try: - c = block[0].replace('/?c=', '') - cats = c.split('_') - torrent = { - 'category': cats[0], - 'subcategory': cats[1], + 'category': get_categories(block[0]), 'url': "http://nyaa.si{}".format(block[1]), 'name': block[2], 'download_url': "http://nyaa.si{}".format(block[4]), @@ -63,6 +58,7 @@ class Nyaa(): pass return torrents + ''' Returns an array of dicts with the n last updates of Nyaa.si ''' @@ -88,12 +84,8 @@ class Nyaa(): block.append(td.text.rstrip()) try: - c = block[0].replace('/?c=', '') - cats = c.split('_') - torrent = { - 'category': cats[0], - 'subcategory': cats[1], + 'category': get_categories(block[0]), 'url': "http://nyaa.si{}".format(block[1]), 'name': block[2], 'download_url': "http://nyaa.si{}".format(block[4]), @@ -109,6 +101,7 @@ class Nyaa(): except IndexError: pass + return torrents[:n] class NyaaPantsu(): @@ -148,3 +141,33 @@ class NyaaPantsu(): results = response['rss']['channel']['item'] return results[:n] + +# Auxiliar functions + +def get_categories(b): + c = b.replace('/?c=', '') + cats = c.split('_') + + cat = cats[0] + subcat = cats[1] + + categories = { + "1": { + "name": "Anime", + "subcats": { + "1": "test", + "2": "test", + "3": "test", + "4": "test", + "5": "test", + "6": "test", + "7": "test" + } + }, + "2": "Audio", + "3": "Literature", + "4": "Live Action", + "5": "Pictures", + "6": "Software" + } + return "{} - {}".format() \ No newline at end of file diff --git a/tests/test.py b/tests/test.py index d72f1d3..96c746f 100644 --- a/tests/test.py +++ b/tests/test.py @@ -3,7 +3,7 @@ from NyaaPy import Nyaa, NyaaPantsu # Nyaa.si results def nyaa_search(): - nyaa_query = Nyaa.search('koe no katachi 1080', 1, 0, 0, 2) + nyaa_query = Nyaa.search('koe no katachi 1080', 1, 0, 0, 0) for nyaa in nyaa_query: print(nyaa['date']) @@ -12,7 +12,7 @@ def nyaa_news(): news = Nyaa.news(5) for result in news: - print(result['name']) + print(result) # Nyaa.pantsu.cat results def pantsu_search(): @@ -32,30 +32,7 @@ def pantsu_news(): # Uncomment whatever you want to test -nyaa_search() +#nyaa_search() #pantsu_search() nyaa_news() -#pantsu_news() - -''' r = requests.get("http://nyaa.si/") -soup = BeautifulSoup(r.text, 'html.parser') -rows = soup.select('table tr') - -torrents = [] - -for row in rows: - td = row.find_all('td') - torrent = [] - - for i in td: - if i.find('a'): - torrent.append(i.find('a').get('href')) - text = i.text.rstrip() - if len(text) > 0: - torrent.append(text) - else: - text = i.text.rstrip() - if len(text) > 0: - torrent.append(text) - - torrents.append(torrent) ''' \ No newline at end of file +#pantsu_news() \ No newline at end of file