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