Issue #30 Improve the torrent objects
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -4,4 +4,6 @@ nyaapy.egg-info
|
||||
.vscode
|
||||
env/
|
||||
*.pyc
|
||||
test_files
|
||||
test_files
|
||||
venv
|
||||
.idea
|
||||
@@ -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'
|
||||
@@ -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)
|
||||
|
||||
17
NyaaPy/torrent.py
Normal file
17
NyaaPy/torrent.py
Normal file
@@ -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])
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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:",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from NyaaPy import SukebeiNyaa
|
||||
from NyaaPy.sukebei import SukebeiNyaa
|
||||
from datetime import datetime
|
||||
import json
|
||||
import os
|
||||
|
||||
Reference in New Issue
Block a user