From e6ab99c48907a67ede58a048eaafc79d06d454a3 Mon Sep 17 00:00:00 2001 From: Ravencentric <78981416+Ravencentric@users.noreply.github.com> Date: Mon, 23 Sep 2024 09:36:05 +0530 Subject: [PATCH] fix: search parse now returns a tuple --- src/pynyaa/_clients/_async.py | 6 +++--- src/pynyaa/_clients/_sync.py | 6 +++--- src/pynyaa/_parser.py | 10 +++------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/pynyaa/_clients/_async.py b/src/pynyaa/_clients/_async.py index 091e753..7da092c 100644 --- a/src/pynyaa/_clients/_async.py +++ b/src/pynyaa/_clients/_async.py @@ -27,7 +27,7 @@ def __init__(self, base_url: str = "https://nyaa.si/", client: AsyncClient | Non The base URL of Nyaa. This is used for constructing the full URL from relative URLs. client : Client, optional - An [httpx.Client](https://www.python-httpx.org/api/#client) instance used to make requests to Nyaa. + An [`httpx.Client`](https://www.python-httpx.org/api/#client) instance used to make requests to Nyaa. """ self._base_url = base_url self._client = AsyncClient(headers={"user-agent": f"pynyaa/{__version__}"}) if client is None else client @@ -109,7 +109,7 @@ async def search( sort_by : SortBy, optional Defines how to sort the results. reverse : bool, optional - Determines the order of the results: ascending if True, descending if False. + Determines the order of the results: ascending if `True`, descending if `False`. Raises ------ @@ -126,7 +126,7 @@ async def search( c=category.id, q=query, s=sort_by, - o="asc" if reverse else "desc", + o="asc" if reverse else "desc", # desc is the default on nyaa ) nyaa = await self._client.get(self._base_url, params=params) diff --git a/src/pynyaa/_clients/_sync.py b/src/pynyaa/_clients/_sync.py index 953add5..029bb39 100644 --- a/src/pynyaa/_clients/_sync.py +++ b/src/pynyaa/_clients/_sync.py @@ -27,7 +27,7 @@ def __init__(self, base_url: str = "https://nyaa.si/", client: Client | None = N The base URL of Nyaa. Default is `https://nyaa.si/`. This is used for constructing the full URL from relative URLs. client : Client, optional - An [httpx.Client](https://www.python-httpx.org/api/#client) instance used to make requests to Nyaa. + An [`httpx.Client`](https://www.python-httpx.org/api/#client) instance used to make requests to Nyaa. """ self._base_url = base_url self._client = Client(headers={"user-agent": f"pynyaa/{__version__}"}) if client is None else client @@ -105,7 +105,7 @@ def search( sort_by : SortBy, optional Defines how to sort the results. reverse : bool, optional - Determines the order of the results: ascending if True, descending if False. + Determines the order of the results: ascending if `True`, descending if `False`. Raises ------ @@ -122,7 +122,7 @@ def search( c=category.id, q=query, s=sort_by, - o="asc" if reverse else "desc", + o="asc" if reverse else "desc", # desc is the default on nyaa ) nyaa = self._client.get(self._base_url, params=params).raise_for_status() diff --git a/src/pynyaa/_parser.py b/src/pynyaa/_parser.py index 0de2d1e..c79b01b 100644 --- a/src/pynyaa/_parser.py +++ b/src/pynyaa/_parser.py @@ -1,14 +1,11 @@ from __future__ import annotations import re -from typing import TYPE_CHECKING, Any +from typing import Any from urllib.parse import urljoin from bs4 import BeautifulSoup -if TYPE_CHECKING: - from typing_extensions import Generator - def parse_nyaa_torrent_page(base_url: str, html: str) -> dict[str, Any]: """ @@ -120,7 +117,7 @@ def parse_nyaa_torrent_page(base_url: str, html: str) -> dict[str, Any]: ) -def parse_nyaa_search_results(html: str, base_url: str = "https://nyaa.si") -> Generator[str]: +def parse_nyaa_search_results(html: str, base_url: str = "https://nyaa.si") -> tuple[str, ...]: """ Parses the HTML of a Nyaa search results page to extract torrent links. @@ -137,5 +134,4 @@ def parse_nyaa_search_results(html: str, base_url: str = "https://nyaa.si") -> G The full URL of torrent page, in the order they were present. """ parsed = re.findall(r".*", html) - for relative_link in parsed: - yield urljoin(base_url, relative_link) + return tuple(urljoin(base_url, relative_link) for relative_link in parsed)