add async api

This commit is contained in:
2025-06-29 14:20:10 +02:00
parent b5cdabf0f6
commit bfaad4de0d
3 changed files with 138 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
from .modules.anime_site import AnimeTorrentSite from .modules.anime_site import AnimeTorrentSite, AnimeTorrentSiteAsync
from .modules.torrent import TorrentSite from .modules.torrent import TorrentSite
from .sites.nyaa import Nyaa, SukebeiNyaa from .sites.nyaa import Nyaa, SukebeiNyaa
from .modules.torrent import Torrent from .modules.torrent import Torrent

View File

@@ -1,4 +1,4 @@
import requests import httpx
from komsuite_nyaapy.modules import torrent from komsuite_nyaapy.modules import torrent
from komsuite_nyaapy.modules.parser import parse_nyaa, parse_single, parse_nyaa_rss from komsuite_nyaapy.modules.parser import parse_nyaa, parse_single, parse_nyaa_rss
@@ -8,21 +8,20 @@ class AnimeTorrentSite:
URL = SITE URL = SITE
@classmethod @classmethod
def last_uploads(self, number_of_results: int): def last_uploads(cls, number_of_results: int):
r = requests.get(self.URL) with httpx.Client() as client:
r = client.get(cls.URL)
r.raise_for_status()
# If anything up with nyaa servers let the user know. json_data = parse_nyaa(
r.raise_for_status() request_text=r.text, limit=number_of_results, site=cls.SITE
)
json_data = parse_nyaa(
request_text=r.text, limit=number_of_results, site=self.SITE
)
return torrent.json_to_class(json_data) return torrent.json_to_class(json_data)
@classmethod @classmethod
def search( def search(
self, cls,
keyword: str, keyword: str,
category: int = 0, category: int = 0,
subcategory: int = 0, subcategory: int = 0,
@@ -32,10 +31,9 @@ class AnimeTorrentSite:
order: str = "desc", order: str = "desc",
**kwargs, **kwargs,
): ):
base_url = self.URL base_url = cls.URL
user = kwargs.get("user", None) user = kwargs.get("user", None)
user_uri = f"user/{user}" if user else "" user_uri = f"user/{user}" if user else ""
if page > 0: if page > 0:
@@ -64,34 +62,134 @@ class AnimeTorrentSite:
if not user: if not user:
search_uri += "&page=rss" search_uri += "&page=rss"
http_response = requests.get(search_uri)
http_response.raise_for_status()
if user: with httpx.Client() as client:
http_response = client.get(search_uri)
http_response.raise_for_status()
if user:
json_data = parse_nyaa(
request_text=http_response.content, limit=None, site=cls.SITE
)
else:
json_data = parse_nyaa_rss(
request_text=http_response.content, limit=None, site=cls.SITE
)
return torrent.json_to_class(json_data)
@classmethod
def get(cls, view_id: int):
with httpx.Client() as client:
r = client.get(f"{cls.URL}/view/{view_id}")
r.raise_for_status()
json_data = parse_single(request_text=r.content, site=cls.SITE)
return torrent.json_to_class(json_data)
@classmethod
def get_from_user(cls, username):
with httpx.Client() as client:
r = client.get(f"{cls.URL}/user/{username}")
r.raise_for_status()
json_data = parse_nyaa(request_text=r.content, limit=None, site=cls.SITE)
return torrent.json_to_class(json_data)
class AnimeTorrentSiteAsync:
SITE = torrent.TorrentSite.NYAASI
URL = SITE
@classmethod
async def last_uploads(cls, number_of_results: int):
async with httpx.AsyncClient() as client:
r = await client.get(cls.URL)
r.raise_for_status()
json_data = parse_nyaa( json_data = parse_nyaa(
request_text=http_response.content, limit=None, site=self.SITE request_text=r.text, limit=number_of_results, site=cls.SITE
)
return torrent.json_to_class(json_data)
@classmethod
async def search(
cls,
keyword: str,
category: int = 0,
subcategory: int = 0,
filters: int = 0,
page: int = 0,
sorting: str = "id",
order: str = "desc",
**kwargs,
):
base_url = cls.URL
user = kwargs.get("user", None)
user_uri = f"user/{user}" if user else ""
if page > 0:
search_uri = "{}/{}?f={}&c={}_{}&q={}&p={}&s={}&o={}".format(
base_url,
user_uri,
filters,
category,
subcategory,
keyword,
page,
sorting,
order,
) )
else: else:
json_data = parse_nyaa_rss( search_uri = "{}/{}?f={}&c={}_{}&q={}&s={}&o={}".format(
request_text=http_response.content, limit=None, site=self.SITE base_url,
user_uri,
filters,
category,
subcategory,
keyword,
sorting,
order,
) )
# Convert JSON data to a class object if not user:
return torrent.json_to_class(json_data) search_uri += "&page=rss"
@classmethod async with httpx.AsyncClient() as client:
def get(self, view_id: int): http_response = await client.get(search_uri)
r = requests.get(f"{self.URL}/view/{view_id}") http_response.raise_for_status()
r.raise_for_status()
json_data = parse_single(request_text=r.content, site=self.SITE) if user:
json_data = parse_nyaa(
request_text=http_response.content, limit=None, site=cls.SITE
)
else:
json_data = parse_nyaa_rss(
request_text=http_response.content, limit=None, site=cls.SITE
)
return torrent.json_to_class(json_data) return torrent.json_to_class(json_data)
@classmethod @classmethod
def get_from_user(self, username): async def get(cls, view_id: int):
r = requests.get(f"{self.URL}/user/{username}") async with httpx.AsyncClient() as client:
r.raise_for_status() r = await client.get(f"{cls.URL}/view/{view_id}")
r.raise_for_status()
json_data = parse_single(request_text=r.content, site=cls.SITE)
return torrent.json_to_class(json_data)
@classmethod
async def get_from_user(cls, username):
async with httpx.AsyncClient() as client:
r = await client.get(f"{cls.URL}/user/{username}")
r.raise_for_status()
json_data = parse_nyaa(request_text=r.content, limit=None, site=cls.SITE)
json_data = parse_nyaa(request_text=r.content, limit=None, site=self.SITE)
return torrent.json_to_class(json_data) return torrent.json_to_class(json_data)

View File

@@ -1,4 +1,4 @@
from komsuite_nyaapy import AnimeTorrentSite, TorrentSite from komsuite_nyaapy import AnimeTorrentSite, TorrentSite, AnimeTorrentSiteAsync
class SukebeiNyaa(AnimeTorrentSite): class SukebeiNyaa(AnimeTorrentSite):
@@ -9,3 +9,12 @@ class SukebeiNyaa(AnimeTorrentSite):
class Nyaa(AnimeTorrentSite): class Nyaa(AnimeTorrentSite):
SITE = TorrentSite.NYAASI SITE = TorrentSite.NYAASI
URL = TorrentSite.get_site(SITE) URL = TorrentSite.get_site(SITE)
class SukebiNyaaAsync(AnimeTorrentSiteAsync):
SITE = TorrentSite.SUKEBEINYAASI
URL = TorrentSite.get_site(SITE)
class NyaaAsync(AnimeTorrentSiteAsync):
SITE = TorrentSite.NYAASI
URL = TorrentSite.get_site(SITE)