Skip to content

Commit

Permalink
fix: search parse now returns a tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Sep 23, 2024
1 parent d4ff622 commit e6ab99c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/pynyaa/_clients/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
------
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/pynyaa/_clients/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
------
Expand All @@ -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()
Expand Down
10 changes: 3 additions & 7 deletions src/pynyaa/_parser.py
Original file line number Diff line number Diff line change
@@ -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]:
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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"<a href=\"(/view/\d+)\" title=\".*\">.*</a>", html)
for relative_link in parsed:
yield urljoin(base_url, relative_link)
return tuple(urljoin(base_url, relative_link) for relative_link in parsed)

0 comments on commit e6ab99c

Please sign in to comment.