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: client rework #10

Merged
merged 9 commits into from
Sep 20, 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
82 changes: 0 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@

* [About](#about)
* [Installation](#installation)
* [Usage](#usage)
* [Docs](#docs)
* [License](#license)

## About

- Supports both sync and async.
- Supports caching.
- Provides easy access to every field except comments (comments are useless).
- Parses both the nyaa page itself and it's accompanying `.torrent` file.

Expand All @@ -47,86 +45,6 @@
pip install pynyaa
```

## Usage
### API
```py
from pynyaa import Nyaa

# You can pass any httpx.Client() keyword argument to Nyaa()
headers = {"user-agent": "my-app/0.0.1"}
client = Nyaa(headers=headers)

nyaa = client.get("https://nyaa.si/view/1693817") # Full URL
nyaa = client.get(1693817) # Only the ID also works

print(nyaa.title)
#> [LYS1TH3A] Fate/stay night Heaven's Feel I. Presage Flower (2017) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
print(nyaa.submitter)
#> pog42
print(nyaa.torrent.files)
#> [File('Fate.stay.night.Heavens.Feel.I.Presage.Flower.2017.1080p.BluRay.Opus5.1.H.265-LYS1TH3A.mkv', size=12263052206)]
print(nyaa.torrent.infohash)
#> 6fdc0395a7fdde6ce3fb7f144b31f3cabdcbf537

torrents = client.search("LYS1TH3A")

for torrent in torrents:
print(torrent)
#> [LYS1TH3A] Fate/Zero Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Market Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Love Story (2014) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
```

### CLI
```shell
$ pynyaa https://nyaa.si/view/1839609 --indent 2
```
```json
{
"id": 1839609,
"url": "https://nyaa.si/view/1839609",
"title": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"category": "Anime - English-translated",
"datetime": "2024-06-30T02:12:07Z",
"submitter": {
"name": "subsplease",
"url": "https://nyaa.si/user/subsplease",
"is_trusted": true,
"is_banned": false
},
"information": "https://subsplease.org/",
"seeders": 1998,
"leechers": 106,
"completed": 7736,
"is_trusted": true,
"is_remake": false,
"description": "...",
"torrent_file": "https://nyaa.si/download/1839609.torrent",
"magnet": "magnet:?xt=urn:btih:...&dn=...",
"torrent": {
"name": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"size": 1455173416,
"infohash": "767d16e0aef5888b1a513a26709e963478ed4123",
"piece_size": 1048576,
"private": null,
"trackers": [
["http://nyaa.tracker.wf:7777/announce"],
["wss://tracker.openwebtorrent.com"]
],
"comment": "https://nyaa.si/view/1839609",
"creation_date": "2024-06-30T07:42:07",
"created_by": "NyaaV2",
"source": null,
"files": [
{
"file": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"size": 1455173416
}
]
}
}
```

# Docs

Checkout the complete documentation [here](https://pynyaa.ravencentric.cc/).
Expand Down
81 changes: 0 additions & 81 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
## About

- Supports both sync and async.
- Supports caching.
- Provides easy access to every field except comments (comments are useless).
- Parses both the nyaa page itself and it's accompanying `.torrent` file.

Expand All @@ -40,86 +39,6 @@
pip install pynyaa
```

## Usage
### API
```py
from pynyaa import Nyaa

# You can pass any httpx.Client() keyword argument to Nyaa()
headers = {"user-agent": "my-app/0.0.1"}
client = Nyaa(headers=headers)

nyaa = client.get("https://nyaa.si/view/1693817") # Full URL
nyaa = client.get(1693817) # Only the ID also works

print(nyaa.title)
#> [LYS1TH3A] Fate/stay night Heaven's Feel I. Presage Flower (2017) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
print(nyaa.submitter)
#> pog42
print(nyaa.torrent.files)
#> [File('Fate.stay.night.Heavens.Feel.I.Presage.Flower.2017.1080p.BluRay.Opus5.1.H.265-LYS1TH3A.mkv', size=12263052206)]
print(nyaa.torrent.infohash)
#> 6fdc0395a7fdde6ce3fb7f144b31f3cabdcbf537

torrents = client.search("LYS1TH3A")

for torrent in torrents:
print(torrent)
#> [LYS1TH3A] Fate/Zero Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Market Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Love Story (2014) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
```

### CLI
```shell
$ pynyaa https://nyaa.si/view/1839609 --indent 2
```
```json
{
"id": 1839609,
"url": "https://nyaa.si/view/1839609",
"title": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"category": "Anime - English-translated",
"datetime": "2024-06-30T02:12:07Z",
"submitter": {
"name": "subsplease",
"url": "https://nyaa.si/user/subsplease",
"is_trusted": true,
"is_banned": false
},
"information": "https://subsplease.org/",
"seeders": 1998,
"leechers": 106,
"completed": 7736,
"is_trusted": true,
"is_remake": false,
"description": "...",
"torrent_file": "https://nyaa.si/download/1839609.torrent",
"magnet": "magnet:?xt=urn:btih:...&dn=...",
"torrent": {
"name": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"size": 1455173416,
"infohash": "767d16e0aef5888b1a513a26709e963478ed4123",
"piece_size": 1048576,
"private": null,
"trackers": [
["http://nyaa.tracker.wf:7777/announce"],
["wss://tracker.openwebtorrent.com"]
],
"comment": "https://nyaa.si/view/1839609",
"creation_date": "2024-06-30T07:42:07",
"created_by": "NyaaV2",
"source": null,
"files": [
{
"file": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"size": 1455173416
}
]
}
}
```

## License

Distributed under the [Unlicense](https://choosealicense.com/licenses/unlicense/) License. See [UNLICENSE](https://github.com/Ravencentric/pynyaa/blob/main/UNLICENSE) for more information.
67 changes: 67 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#### Quick Start

```py
import pynyaa

nyaa = pynyaa.get("https://nyaa.si/view/1693817") # Full URL
nyaa = pynyaa.get(1693817) # Only the ID also works

print(nyaa.title)
#> [LYS1TH3A] Fate/stay night Heaven's Feel I. Presage Flower (2017) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
print(nyaa.submitter)
#> pog42
print(nyaa.torrent.files)
#> [File('Fate.stay.night.Heavens.Feel.I.Presage.Flower.2017.1080p.BluRay.Opus5.1.H.265-LYS1TH3A.mkv', size=12263052206)]
print(nyaa.torrent.infohash)
#> 6fdc0395a7fdde6ce3fb7f144b31f3cabdcbf537

torrents = pynyaa.search("LYS1TH3A")

for torrent in torrents:
print(torrent)
#> [LYS1TH3A] Fate/Zero Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Market Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Love Story (2014) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
```

#### Advanced

If you're doing more than quick experiments or one-off scripts, it's best to use the [`Nyaa`][pynyaa.Nyaa]/[`AsyncNyaa`][pynyaa.AsyncNyaa] client directly. They give you far more flexibility and control over the requests along with improved performance.

!!! note
For more details, see the [HTTPX documentation](https://www.python-httpx.org/advanced/clients/#why-use-a-client) on clients.


Using the same example as above but with the `Nyaa` client:
```py
from pynyaa import Nyaa

with Nyaa() as client:
nyaa = client.get("https://nyaa.si/view/1693817")

print(nyaa.title)
#> [LYS1TH3A] Fate/stay night Heaven's Feel I. Presage Flower (2017) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]

torrents = client.search("LYS1TH3A")

for torrent in torrents:
print(torrent)
#> [LYS1TH3A] Fate/Zero Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Market Season 1 (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
#> [LYS1TH3A] Tamako Love Story (2014) (BD 1080p HEVC x265 10-bit Opus) [Dual-Audio]
```

You can also pass your own instance of an [`httpx.Client`](https://www.python-httpx.org/api/#client) to the [`Nyaa`][pynyaa.Nyaa] client. This gives you complete control over the requests. For example, we can use this to quickly implement aggressive caching with [`hishel`](https://github.com/karpetrosyan/hishel):

```py
from hishel import CacheClient, Controller, InMemoryStorage

from pynyaa import Nyaa

storage = InMemoryStorage(capacity=256)
controller = Controller(force_cache=True)

with Nyaa(client=CacheClient(storage=storage, controller=controller)) as client:
nyaa1 = client.get("https://nyaa.si/view/1693817") # First request
nyaa2 = client.get("https://nyaa.si/view/1693817") # Returns the cached result
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ markdown_extensions:

nav:
- Home: index.md
- Usage: usage.md
- CLI Reference: cli-reference.md
- API Reference:
- Clients: api-reference/clients.md
Expand Down
30 changes: 30 additions & 0 deletions src/pynyaa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
from __future__ import annotations

from httpx import HTTPStatusError
from typing_extensions import Generator

from pynyaa._clients._async import AsyncNyaa
from pynyaa._clients._sync import Nyaa
from pynyaa._enums import Category, Filter, SortBy
from pynyaa._models import NyaaTorrentPage, Submitter
from pynyaa._version import __version__, __version_tuple__


def get(page: int | str) -> NyaaTorrentPage: # pragma: no cover
"""
Shortcut for `pynyaa.Nyaa.get`.
For more advanced or configurable usage, use the `pynyaa.Nyaa` client directly.
"""
with Nyaa() as nyaa:
return nyaa.get(page)


def search(
query: str,
*,
category: Category = Category.ALL,
filter: Filter = Filter.NO_FILTER,
sort_by: SortBy = SortBy.DATETIME,
reverse: bool = False,
) -> Generator[NyaaTorrentPage]: # pragma: no cover
"""
Shortcut for `pynyaa.Nyaa.search`.
For more advanced or configurable usage, use the `pynyaa.Nyaa` client directly.
"""
with Nyaa() as nyaa:
yield from nyaa.search(query, category=category, filter=filter, sort_by=sort_by, reverse=reverse)


__all__ = (
# Clients
"AsyncNyaa",
"Nyaa",
# Top level API,
"get",
"search",
# Enums
"Category",
"Filter",
Expand Down
Loading