Skip to content

Commit

Permalink
feat(nyaasi): add static classes instead of object-based ones
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanjoSalvador committed Jul 18, 2024
1 parent bbd129c commit 2c3d3ea
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 527 deletions.
39 changes: 20 additions & 19 deletions nyaapy/nyaa.py → nyaapy/anime_site.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import requests
from nyaapy import utils
from nyaapy import torrent
from nyaapy.parser import parse_nyaa, parse_single, parse_nyaa_rss

class AnimeTorrentSite:
SITE = torrent.TorrentSite.NYAASI
URL = "https://nyaa.si"

class Nyaa:

def __init__(self):
self.SITE = utils.TorrentSite.NYAASI
self.URL = "https://nyaa.si"

def last_uploads(self, number_of_results):
@classmethod
def last_uploads(self, number_of_results: int):
r = requests.get(self.URL)

# If anything up with nyaa servers let the user know.
r.raise_for_status()

json_data = utils.parse_nyaa(
request_text=r.text, limit=number_of_results + 1, site=self.SITE
json_data = parse_nyaa(
request_text=r.text, limit=number_of_results, site=self.SITE
)

return torrent.json_to_class(json_data)

def search(self, keyword, **kwargs):
@classmethod
def search(self, keyword: str, **kwargs):
base_url = self.URL

user = kwargs.get("user", None)
Expand Down Expand Up @@ -67,28 +66,30 @@ def search(self, keyword, **kwargs):
http_response.raise_for_status()

if user:
json_data = utils.parse_nyaa(
request_text=http_response.text, limit=None, site=self.SITE
json_data = parse_nyaa(
request_text=http_response.content, limit=None, site=self.SITE
)
else:
json_data = utils.parse_nyaa_rss(
request_text=http_response.text, limit=None, site=self.SITE
json_data = parse_nyaa_rss(
request_text=http_response.content, limit=None, site=self.SITE
)

# Convert JSON data to a class object
return torrent.json_to_class(json_data)

def get(self, view_id):
@classmethod
def get(self, view_id: int):
r = requests.get(f"{self.URL}/view/{view_id}")
r.raise_for_status()

json_data = utils.parse_single(request_text=r.text, site=self.SITE)
json_data = parse_single(request_text=r.content, site=self.SITE)

return torrent.json_to_class(json_data)

def get_user(self, username):
@classmethod
def get_from_user(self, username):
r = requests.get(f"{self.URL}/user/{username}")
r.raise_for_status()

json_data = utils.parse_nyaa(request_text=r.text, limit=None, site=self.SITE)
json_data = parse_nyaa(request_text=r.content, limit=None, site=self.SITE)
return torrent.json_to_class(json_data)
22 changes: 22 additions & 0 deletions nyaapy/magnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import urllib
from urllib.parse import urlencode

def magnet_builder(info_hash, title):
"""
Generates a magnet link using the info_hash and title of a given file.
"""
known_trackers = [
"http://nyaa.tracker.wf:7777/announce",
"udp://open.stealth.si:80/announce",
"udp://tracker.opentrackr.org:1337/announce",
"udp://exodus.desync.com:6969/announce",
"udp://tracker.torrent.eu.org:451/announce",
]

magnet_link = f"magnet:?xt=urn:btih:{info_hash}&" + urlencode(
{"dn": title}, quote_via=urllib.parse.quote
)
for tracker in known_trackers:
magnet_link += f"&{urlencode({'tr': tracker})}"

return magnet_link
Empty file added nyaapy/nyaasi/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions nyaapy/nyaasi/nyaa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from nyaapy.anime_site import AnimeTorrentSite
from nyaapy.torrent import TorrentSite


class Nyaa(AnimeTorrentSite):
SITE = TorrentSite.NYAASI
URL = "https://nyaa.si"
6 changes: 6 additions & 0 deletions nyaapy/nyaasi/sukebei.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from nyaapy.anime_site import AnimeTorrentSite
from nyaapy.torrent import TorrentSite

class SukebeiNyaa(AnimeTorrentSite):
SITE = TorrentSite.SUKEBEINYAASI
URL = "https://sukebei.nyaa.si"
Loading

0 comments on commit 2c3d3ea

Please sign in to comment.