Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: caching #4

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lxml = ">=5.1.0"
hishel = ">=0.0.25"
torf = ">=4.2.6"
typing-extensions = ">=4.10.0"
platformdirs = ">=4.2.0"
strenum = { version = ">=0.4.15", python = "<3.11" }
importlib-metadata = { version = ">=7.1.0", python = "<3.10" }
eval-type-backport = { version = ">=0.1.3", python = "<3.10" }
Expand Down
7 changes: 5 additions & 2 deletions src/pynyaa/_clients/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
from io import BytesIO
from typing import Any

import hishel
from bs4 import BeautifulSoup
from hishel import AsyncCacheClient, AsyncFileStorage
from pydantic import validate_call
from pydantic_core import Url
from torf import Torrent

from .._exceptions import HTMLParsingError
from .._models import NyaaTorrentPage
from .._types import HTTPXAsyncClientKwargs
from .._utils import _get_user_cache_path


class AsyncNyaa:
Expand Down Expand Up @@ -191,7 +192,9 @@ async def get(self, page: int | str) -> NyaaTorrentPage:
host = Url(page).host
self.base_url = f"https://{host}" if host is not None else "https://nyaa.si"

async with hishel.AsyncCacheClient(**self.kwargs) as client:
async with AsyncCacheClient(
storage=AsyncFileStorage(base_path=_get_user_cache_path()), **self.kwargs
) as client:
extensions = {"force_cache": self.cache, "cache_disabled": not self.cache}
nyaa = await client.get(url, extensions=extensions)
nyaa.raise_for_status()
Expand Down
5 changes: 3 additions & 2 deletions src/pynyaa/_clients/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
from io import BytesIO
from typing import Any

import hishel
from bs4 import BeautifulSoup
from hishel import CacheClient, FileStorage
from pydantic import validate_call
from pydantic_core import Url
from torf import Torrent

from .._exceptions import HTMLParsingError
from .._models import NyaaTorrentPage
from .._types import HTTPXClientKwargs
from .._utils import _get_user_cache_path


class Nyaa:
Expand Down Expand Up @@ -191,7 +192,7 @@ def get(self, page: int | str) -> NyaaTorrentPage:
host = Url(page).host
self.base_url = f"https://{host}" if host is not None else "https://nyaa.si"

with hishel.CacheClient(**self.kwargs) as client:
with CacheClient(storage=FileStorage(base_path=_get_user_cache_path()), **self.kwargs) as client:
extensions = {"force_cache": self.cache, "cache_disabled": not self.cache}
nyaa = client.get(url, extensions=extensions).raise_for_status()

Expand Down
12 changes: 12 additions & 0 deletions src/pynyaa/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from platformdirs import user_cache_path

if TYPE_CHECKING:
from pathlib import Path


def _get_user_cache_path() -> Path:
return user_cache_path(appname="pynaa", ensure_exists=True)