Skip to content

Commit

Permalink
Issue #30 Improve the torrent objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Hungry-Dolphin committed Dec 13, 2020
1 parent d3b2892 commit 9db8af3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ nyaapy.egg-info
.vscode
env/
*.pyc
test_files
test_files
venv
.idea
8 changes: 2 additions & 6 deletions NyaaPy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# Info about the module
__version__ = '0.6.0'
__version__ = '0.6.3'
__author__ = 'Juanjo Salvador'
__email__ = '[email protected]'
__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'
29 changes: 18 additions & 11 deletions NyaaPy/nyaa.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -30,7 +32,7 @@ def search(self, keyword, **kwargs):
page = kwargs.get('page', 0)

if user:
user_uri = "user/{}".format(user)
user_uri = f"user/{user}"
else:
user_uri = ""

Expand All @@ -44,24 +46,29 @@ def search(self, keyword, **kwargs):

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)
17 changes: 17 additions & 0 deletions NyaaPy/torrent.py
Original file line number Diff line number Diff line change
@@ -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])
37 changes: 16 additions & 21 deletions NyaaPy/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
'''
Module utils
'''

import re
from enum import Enum
from lxml import etree

Expand All @@ -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",
Expand All @@ -38,22 +33,22 @@ 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"
}
},
"4": {
"name": "Live Action",
"subcats": {
"sub_cats": {
"1": "English-translated",
"2": "Idol/Promotional Video",
"3": "Non-English-translated",
Expand All @@ -62,25 +57,25 @@ def nyaa_categories(b):
},
"5": {
"name": "Pictures",
"subcats": {
"sub_cats": {
"1": "Graphics",
"2": "Photos"
}
},
"6": {
"name": "Software",
"subcats": {
"sub_cats": {
"1": "Applications",
"2": "Games"
}
}
}

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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
31 changes: 25 additions & 6 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from NyaaPy import Nyaa
from NyaaPy.nyaa import Nyaa
from pprint import pprint
from datetime import datetime
import json
Expand All @@ -17,27 +17,46 @@
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:",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sukebei.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from NyaaPy import SukebeiNyaa
from NyaaPy.sukebei import SukebeiNyaa
from datetime import datetime
import json
import os
Expand Down

0 comments on commit 9db8af3

Please sign in to comment.