From 3d8406a57737dc0dd319135d4c593db58044ceae Mon Sep 17 00:00:00 2001 From: Juanjo Salvador Date: Sat, 5 Oct 2019 08:50:58 +0200 Subject: [PATCH 1/3] Update HOW-TO-CONTRIBUTE.md --- HOW-TO-CONTRIBUTE.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/HOW-TO-CONTRIBUTE.md b/HOW-TO-CONTRIBUTE.md index 0ea9ba9..15ff81b 100644 --- a/HOW-TO-CONTRIBUTE.md +++ b/HOW-TO-CONTRIBUTE.md @@ -4,8 +4,27 @@ 1. Star the repo, it will help me a lot. 2. Make a fork for you. -3. Use the `dev` branch, never master. +3. Clone the repo into your local machine. +4. Create a new branch for your changes. +5. Start hacking :-) +## Not familiarized with the Python workflow? + +1. Be sure that you have Python 3 and virtualenv installed (if not, install them) +2. Create a new virtualenv + +``` + python -m virtualenv env -p python3 +``` + +3. And activate it! +4. Now it's time to install the dependencies. + +``` + pip install -r requirements.txt +``` + +5. And now you're ready to hack. ## Hacking From d3b2892c1f2a58ef49f50dfe0f4fa167082cd328 Mon Sep 17 00:00:00 2001 From: Juanjo Salvador Date: Sat, 5 Oct 2019 08:50:58 +0200 Subject: [PATCH 2/3] Update HOW-TO-CONTRIBUTE.md --- HOW-TO-CONTRIBUTE.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/HOW-TO-CONTRIBUTE.md b/HOW-TO-CONTRIBUTE.md index 0ea9ba9..15ff81b 100644 --- a/HOW-TO-CONTRIBUTE.md +++ b/HOW-TO-CONTRIBUTE.md @@ -4,8 +4,27 @@ 1. Star the repo, it will help me a lot. 2. Make a fork for you. -3. Use the `dev` branch, never master. +3. Clone the repo into your local machine. +4. Create a new branch for your changes. +5. Start hacking :-) +## Not familiarized with the Python workflow? + +1. Be sure that you have Python 3 and virtualenv installed (if not, install them) +2. Create a new virtualenv + +``` + python -m virtualenv env -p python3 +``` + +3. And activate it! +4. Now it's time to install the dependencies. + +``` + pip install -r requirements.txt +``` + +5. And now you're ready to hack. ## Hacking From 9db8af3a60dfd3a9aa569a632ff7bc96e11563ad Mon Sep 17 00:00:00 2001 From: Dolphin Date: Sun, 13 Dec 2020 21:12:56 +0100 Subject: [PATCH 3/3] Issue #30 Improve the torrent objects --- .gitignore | 4 +++- NyaaPy/__init__.py | 8 ++------ NyaaPy/nyaa.py | 29 ++++++++++++++++++----------- NyaaPy/torrent.py | 17 +++++++++++++++++ NyaaPy/utils.py | 37 ++++++++++++++++--------------------- tests/test.py | 31 +++++++++++++++++++++++++------ tests/test_sukebei.py | 2 +- 7 files changed, 82 insertions(+), 46 deletions(-) create mode 100644 NyaaPy/torrent.py diff --git a/.gitignore b/.gitignore index 39ed182..5648665 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ nyaapy.egg-info .vscode env/ *.pyc -test_files \ No newline at end of file +test_files +venv +.idea \ No newline at end of file diff --git a/NyaaPy/__init__.py b/NyaaPy/__init__.py index 1c2e563..0299c47 100644 --- a/NyaaPy/__init__.py +++ b/NyaaPy/__init__.py @@ -1,11 +1,7 @@ # Info about the module -__version__ = '0.6.0' +__version__ = '0.6.3' __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.pantsu import Pantsu -from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu +__license__ = 'MIT license' \ No newline at end of file diff --git a/NyaaPy/nyaa.py b/NyaaPy/nyaa.py index 7b9d4c0..cbd93ff 100644 --- a/NyaaPy/nyaa.py +++ b/NyaaPy/nyaa.py @@ -1,27 +1,29 @@ import requests from NyaaPy import utils +from NyaaPy import torrent class Nyaa: def __init__(self): self.SITE = utils.TorrentSite.NYAASI - self.URI = "https://nyaa.si" + self.URL = "https://nyaa.si" def last_uploads(self, number_of_results): - r = requests.get(self.SITE.value) + r = requests.get(self.URL) # If anything up with nyaa servers let the user know. r.raise_for_status() - return utils.parse_nyaa( + json_data = utils.parse_nyaa( request_text=r.text, limit=number_of_results + 1, site=self.SITE ) + return torrent.json_to_class(json_data) def search(self, keyword, **kwargs): - url = self.SITE.value + url = self.URL user = kwargs.get('user', None) category = kwargs.get('category', 0) @@ -30,7 +32,7 @@ class Nyaa: page = kwargs.get('page', 0) if user: - user_uri = "user/{}".format(user) + user_uri = f"user/{user}" else: user_uri = "" @@ -44,24 +46,29 @@ class Nyaa: r.raise_for_status() - return utils.parse_nyaa( + json_data = utils.parse_nyaa( request_text=r.text, limit=None, site=self.SITE ) - def get(self, id): - r = requests.get("{}/view/{}".format(self.SITE.value, id)) + return torrent.json_to_class(json_data) + + def get(self, view_id): + r = requests.get(f'{self.URL}/view/{view_id}') r.raise_for_status() - return utils.parse_single(request_text=r.text, site=self.SITE) + json_data = utils.parse_single(request_text=r.text, site=self.SITE) + + return torrent.json_to_class(json_data) def get_user(self, username): - r = requests.get("{}/user/{}".format(self.SITE.value, username)) + r = requests.get(f'{self.URL}/user/{username}') r.raise_for_status() - return utils.parse_nyaa( + json_data = utils.parse_nyaa( request_text=r.text, limit=None, site=self.SITE ) + return torrent.json_to_class(json_data) diff --git a/NyaaPy/torrent.py b/NyaaPy/torrent.py new file mode 100644 index 0000000..8269c05 --- /dev/null +++ b/NyaaPy/torrent.py @@ -0,0 +1,17 @@ +def json_to_class(data): + # We check if the data passed is a list or not + if isinstance(data, list): + object_list = [] + for item in data: + object_list.append(Torrent(item)) + # Return a list of Torrent objects + return object_list + else: + return Torrent(data) + + +# This deals with converting the dict to an object +class Torrent(object): + def __init__(self, my_dict): + for key in my_dict: + setattr(self, key, my_dict[key]) diff --git a/NyaaPy/utils.py b/NyaaPy/utils.py index c6b0d71..83c35c7 100644 --- a/NyaaPy/utils.py +++ b/NyaaPy/utils.py @@ -1,8 +1,3 @@ -''' - Module utils -''' - -import re from enum import Enum from lxml import etree @@ -24,12 +19,12 @@ def nyaa_categories(b): cats = c.split('_') cat = cats[0] - subcat = cats[1] + sub_cat = cats[1] categories = { "1": { "name": "Anime", - "subcats": { + "sub_cats": { "1": "Anime Music Video", "2": "English-translated", "3": "Non-English-translated", @@ -38,14 +33,14 @@ def nyaa_categories(b): }, "2": { "name": "Audio", - "subcats": { + "sub_cats": { "1": "Lossless", "2": "Lossy" } }, "3": { "name": "Literature", - "subcats": { + "sub_cats": { "1": "English-translated", "2": "Non-English-translated", "3": "Raw" @@ -53,7 +48,7 @@ def nyaa_categories(b): }, "4": { "name": "Live Action", - "subcats": { + "sub_cats": { "1": "English-translated", "2": "Idol/Promotional Video", "3": "Non-English-translated", @@ -62,14 +57,14 @@ def nyaa_categories(b): }, "5": { "name": "Pictures", - "subcats": { + "sub_cats": { "1": "Graphics", "2": "Photos" } }, "6": { "name": "Software", - "subcats": { + "sub_cats": { "1": "Applications", "2": "Games" } @@ -77,10 +72,10 @@ def nyaa_categories(b): } try: - category_name = "{} - {}".format( - categories[cat]['name'], categories[cat]['subcats'][subcat]) - except Exception: - pass + category_name = f"{categories[cat]['name']} - {categories[cat]['sub_cats'][sub_cat]}" + except KeyError: + print("Unable to get Nyaa category name") + return return category_name @@ -130,7 +125,7 @@ def parse_nyaa(request_text, limit, site): elif site in [TorrentSite.SUKEBEINYAASI, TorrentSite.SUKEBEINYAANET]: category = sukebei_categories(block[0]) else: - raise ArgumentException("Unknown TorrentSite received!") + raise ValueError("Unknown TorrentSite received!") # Create torrent object try: @@ -227,10 +222,10 @@ def sukebei_categories(b): } try: - category_name = "{} - {}".format( - categories[cat]['name'], categories[cat]['subcats'][subcat]) - except Exception: - pass + category_name = f"{categories[cat]['name']} - {categories[cat]['subcats'][subcat]}" + except KeyError: + print("Unable to get Sukebei category name") + return return category_name diff --git a/tests/test.py b/tests/test.py index 8b9215e..10f67fd 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,4 +1,4 @@ -from NyaaPy import Nyaa +from NyaaPy.nyaa import Nyaa from pprint import pprint from datetime import datetime import json @@ -17,27 +17,46 @@ dt_latest_torrents_begin = datetime.now() latest_torrents = nyaa.last_uploads(100) dt_latest_torrents_end = datetime.now() with open("test_files/nyaa_latest_torrent_test.json", 'w') as f: - json.dump(latest_torrents, f) + for torrent in latest_torrents: + try: + # This prints it as byte like objects since unicode is fun + f.write(str(torrent.name.encode('utf-8')) + '\n') + except AttributeError: + f.write('No name found for this torrent') # Search some nasty stuff dt_search_begin = datetime.now() test_search = nyaa.search("kimi no na wa") dt_search_end = datetime.now() with open("test_files/nyaa_search_test.json", 'w') as f: - json.dump(test_search, f) + for torrent in test_search: + try: + # This prints it as byte like objects since unicode is fun + f.write(str(torrent.name.encode('utf-8')) + '\n') + except AttributeError: + f.write('No name found for this torrent') # Get first torrent from found torrents dt_single_torrent_begin = datetime.now() -single_torrent = nyaa.get(test_search[0]["id"]) +single_torrent = test_search[0] dt_single_torrent_end = datetime.now() with open("test_files/nyaa_single_torrent_test.json", 'w') as f: - json.dump(single_torrent, f) + try: + # This prints it as byte like objects since unicode is fun + f.write(str(torrent.name.encode('utf-8')) + '\n') + except AttributeError: + f.write('No name found for this torrent') dt_user_begin = datetime.now() user_torrents = nyaa.get_user("HorribleSubs") dt_user_end = datetime.now() with open("test_files/nyaa_single_user_test.json", 'w') as f: - json.dump(user_torrents, f) + for torrent in user_torrents: + try: + # This prints it as byte like objects since unicode is fun + f.write(str(torrent.name.encode('utf-8')) + '\n') + except AttributeError: + f.write('No name found for this torrent') print( "Latest torrents time:", diff --git a/tests/test_sukebei.py b/tests/test_sukebei.py index eebaaf6..a6ef1cb 100644 --- a/tests/test_sukebei.py +++ b/tests/test_sukebei.py @@ -1,4 +1,4 @@ -from NyaaPy import SukebeiNyaa +from NyaaPy.sukebei import SukebeiNyaa from datetime import datetime import json import os