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
|
.vscode
|
||||||
env/
|
env/
|
||||||
*.pyc
|
*.pyc
|
||||||
test_files
|
test_files
|
||||||
|
venv
|
||||||
|
.idea
|
||||||
@@ -1,11 +1,7 @@
|
|||||||
# Info about the module
|
# Info about the module
|
||||||
__version__ = '0.6.0'
|
__version__ = '0.6.3'
|
||||||
__author__ = 'Juanjo Salvador'
|
__author__ = 'Juanjo Salvador'
|
||||||
__email__ = 'juanjosalvador@netc.eu'
|
__email__ = 'juanjosalvador@netc.eu'
|
||||||
__url__ = 'http://juanjosalvador.me'
|
__url__ = 'http://juanjosalvador.me'
|
||||||
__copyright__ = '2017 Juanjo Salvador'
|
__copyright__ = '2017 Juanjo Salvador'
|
||||||
__license__ = 'MIT license'
|
__license__ = 'MIT license'
|
||||||
|
|
||||||
from NyaaPy.nyaa import Nyaa
|
|
||||||
from NyaaPy.pantsu import Pantsu
|
|
||||||
from NyaaPy.sukebei import SukebeiNyaa, SukebeiPantsu
|
|
||||||
@@ -1,27 +1,29 @@
|
|||||||
import requests
|
import requests
|
||||||
from NyaaPy import utils
|
from NyaaPy import utils
|
||||||
|
from NyaaPy import torrent
|
||||||
|
|
||||||
|
|
||||||
class Nyaa:
|
class Nyaa:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.SITE = utils.TorrentSite.NYAASI
|
self.SITE = utils.TorrentSite.NYAASI
|
||||||
self.URI = "https://nyaa.si"
|
self.URL = "https://nyaa.si"
|
||||||
|
|
||||||
def last_uploads(self, number_of_results):
|
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.
|
# If anything up with nyaa servers let the user know.
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
return utils.parse_nyaa(
|
json_data = utils.parse_nyaa(
|
||||||
request_text=r.text,
|
request_text=r.text,
|
||||||
limit=number_of_results + 1,
|
limit=number_of_results + 1,
|
||||||
site=self.SITE
|
site=self.SITE
|
||||||
)
|
)
|
||||||
|
return torrent.json_to_class(json_data)
|
||||||
|
|
||||||
def search(self, keyword, **kwargs):
|
def search(self, keyword, **kwargs):
|
||||||
url = self.SITE.value
|
url = self.URL
|
||||||
|
|
||||||
user = kwargs.get('user', None)
|
user = kwargs.get('user', None)
|
||||||
category = kwargs.get('category', 0)
|
category = kwargs.get('category', 0)
|
||||||
@@ -30,7 +32,7 @@ class Nyaa:
|
|||||||
page = kwargs.get('page', 0)
|
page = kwargs.get('page', 0)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
user_uri = "user/{}".format(user)
|
user_uri = f"user/{user}"
|
||||||
else:
|
else:
|
||||||
user_uri = ""
|
user_uri = ""
|
||||||
|
|
||||||
@@ -44,24 +46,29 @@ class Nyaa:
|
|||||||
|
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
return utils.parse_nyaa(
|
json_data = utils.parse_nyaa(
|
||||||
request_text=r.text,
|
request_text=r.text,
|
||||||
limit=None,
|
limit=None,
|
||||||
site=self.SITE
|
site=self.SITE
|
||||||
)
|
)
|
||||||
|
|
||||||
def get(self, id):
|
return torrent.json_to_class(json_data)
|
||||||
r = requests.get("{}/view/{}".format(self.SITE.value, id))
|
|
||||||
|
def get(self, view_id):
|
||||||
|
r = requests.get(f'{self.URL}/view/{view_id}')
|
||||||
r.raise_for_status()
|
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):
|
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()
|
r.raise_for_status()
|
||||||
|
|
||||||
return utils.parse_nyaa(
|
json_data = utils.parse_nyaa(
|
||||||
request_text=r.text,
|
request_text=r.text,
|
||||||
limit=None,
|
limit=None,
|
||||||
site=self.SITE
|
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 enum import Enum
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
@@ -24,12 +19,12 @@ def nyaa_categories(b):
|
|||||||
cats = c.split('_')
|
cats = c.split('_')
|
||||||
|
|
||||||
cat = cats[0]
|
cat = cats[0]
|
||||||
subcat = cats[1]
|
sub_cat = cats[1]
|
||||||
|
|
||||||
categories = {
|
categories = {
|
||||||
"1": {
|
"1": {
|
||||||
"name": "Anime",
|
"name": "Anime",
|
||||||
"subcats": {
|
"sub_cats": {
|
||||||
"1": "Anime Music Video",
|
"1": "Anime Music Video",
|
||||||
"2": "English-translated",
|
"2": "English-translated",
|
||||||
"3": "Non-English-translated",
|
"3": "Non-English-translated",
|
||||||
@@ -38,14 +33,14 @@ def nyaa_categories(b):
|
|||||||
},
|
},
|
||||||
"2": {
|
"2": {
|
||||||
"name": "Audio",
|
"name": "Audio",
|
||||||
"subcats": {
|
"sub_cats": {
|
||||||
"1": "Lossless",
|
"1": "Lossless",
|
||||||
"2": "Lossy"
|
"2": "Lossy"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"3": {
|
"3": {
|
||||||
"name": "Literature",
|
"name": "Literature",
|
||||||
"subcats": {
|
"sub_cats": {
|
||||||
"1": "English-translated",
|
"1": "English-translated",
|
||||||
"2": "Non-English-translated",
|
"2": "Non-English-translated",
|
||||||
"3": "Raw"
|
"3": "Raw"
|
||||||
@@ -53,7 +48,7 @@ def nyaa_categories(b):
|
|||||||
},
|
},
|
||||||
"4": {
|
"4": {
|
||||||
"name": "Live Action",
|
"name": "Live Action",
|
||||||
"subcats": {
|
"sub_cats": {
|
||||||
"1": "English-translated",
|
"1": "English-translated",
|
||||||
"2": "Idol/Promotional Video",
|
"2": "Idol/Promotional Video",
|
||||||
"3": "Non-English-translated",
|
"3": "Non-English-translated",
|
||||||
@@ -62,14 +57,14 @@ def nyaa_categories(b):
|
|||||||
},
|
},
|
||||||
"5": {
|
"5": {
|
||||||
"name": "Pictures",
|
"name": "Pictures",
|
||||||
"subcats": {
|
"sub_cats": {
|
||||||
"1": "Graphics",
|
"1": "Graphics",
|
||||||
"2": "Photos"
|
"2": "Photos"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"6": {
|
"6": {
|
||||||
"name": "Software",
|
"name": "Software",
|
||||||
"subcats": {
|
"sub_cats": {
|
||||||
"1": "Applications",
|
"1": "Applications",
|
||||||
"2": "Games"
|
"2": "Games"
|
||||||
}
|
}
|
||||||
@@ -77,10 +72,10 @@ def nyaa_categories(b):
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
category_name = "{} - {}".format(
|
category_name = f"{categories[cat]['name']} - {categories[cat]['sub_cats'][sub_cat]}"
|
||||||
categories[cat]['name'], categories[cat]['subcats'][subcat])
|
except KeyError:
|
||||||
except Exception:
|
print("Unable to get Nyaa category name")
|
||||||
pass
|
return
|
||||||
|
|
||||||
return category_name
|
return category_name
|
||||||
|
|
||||||
@@ -130,7 +125,7 @@ def parse_nyaa(request_text, limit, site):
|
|||||||
elif site in [TorrentSite.SUKEBEINYAASI, TorrentSite.SUKEBEINYAANET]:
|
elif site in [TorrentSite.SUKEBEINYAASI, TorrentSite.SUKEBEINYAANET]:
|
||||||
category = sukebei_categories(block[0])
|
category = sukebei_categories(block[0])
|
||||||
else:
|
else:
|
||||||
raise ArgumentException("Unknown TorrentSite received!")
|
raise ValueError("Unknown TorrentSite received!")
|
||||||
|
|
||||||
# Create torrent object
|
# Create torrent object
|
||||||
try:
|
try:
|
||||||
@@ -227,10 +222,10 @@ def sukebei_categories(b):
|
|||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
category_name = "{} - {}".format(
|
category_name = f"{categories[cat]['name']} - {categories[cat]['subcats'][subcat]}"
|
||||||
categories[cat]['name'], categories[cat]['subcats'][subcat])
|
except KeyError:
|
||||||
except Exception:
|
print("Unable to get Sukebei category name")
|
||||||
pass
|
return
|
||||||
|
|
||||||
return category_name
|
return category_name
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from NyaaPy import Nyaa
|
from NyaaPy.nyaa import Nyaa
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
@@ -17,27 +17,46 @@ dt_latest_torrents_begin = datetime.now()
|
|||||||
latest_torrents = nyaa.last_uploads(100)
|
latest_torrents = nyaa.last_uploads(100)
|
||||||
dt_latest_torrents_end = datetime.now()
|
dt_latest_torrents_end = datetime.now()
|
||||||
with open("test_files/nyaa_latest_torrent_test.json", 'w') as f:
|
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
|
# Search some nasty stuff
|
||||||
dt_search_begin = datetime.now()
|
dt_search_begin = datetime.now()
|
||||||
test_search = nyaa.search("kimi no na wa")
|
test_search = nyaa.search("kimi no na wa")
|
||||||
dt_search_end = datetime.now()
|
dt_search_end = datetime.now()
|
||||||
with open("test_files/nyaa_search_test.json", 'w') as f:
|
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
|
# Get first torrent from found torrents
|
||||||
dt_single_torrent_begin = datetime.now()
|
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()
|
dt_single_torrent_end = datetime.now()
|
||||||
with open("test_files/nyaa_single_torrent_test.json", 'w') as f:
|
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()
|
dt_user_begin = datetime.now()
|
||||||
user_torrents = nyaa.get_user("HorribleSubs")
|
user_torrents = nyaa.get_user("HorribleSubs")
|
||||||
dt_user_end = datetime.now()
|
dt_user_end = datetime.now()
|
||||||
with open("test_files/nyaa_single_user_test.json", 'w') as f:
|
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(
|
print(
|
||||||
"Latest torrents time:",
|
"Latest torrents time:",
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from NyaaPy import SukebeiNyaa
|
from NyaaPy.sukebei import SukebeiNyaa
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|||||||
Reference in New Issue
Block a user