Skip to content

Commit

Permalink
fix: rename date to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Sep 18, 2024
1 parent d05c1cc commit 0a6ee3f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ $ pynyaa https://nyaa.si/view/1839609 --indent 2
"url": "https://nyaa.si/view/1839609",
"title": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"category": "Anime - English-translated",
"date": "2024-06-30T02:12:07Z",
"datetime": "2024-06-30T02:12:07Z",
"submitter": {
"name": "subsplease",
"url": "https://nyaa.si/user/subsplease",
Expand Down
6 changes: 3 additions & 3 deletions docs/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "https://nyaa.si/view/1839609",
"title": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"category": "Anime - English-translated",
"date": "2024-06-30T02:12:07Z",
"datetime": "2024-06-30T02:12:07Z",
"submitter": {
"name": "subsplease",
"url": "https://nyaa.si/user/subsplease",
Expand Down Expand Up @@ -73,7 +73,7 @@
"url": "https://nyaa.si/view/1839609",
"title": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"category": "Anime - English-translated",
"date": "2024-06-30T02:12:07Z"
"datetime": "2024-06-30T02:12:07Z"
}
```

Expand All @@ -86,7 +86,7 @@
"url": "https://nyaa.si/view/1839609",
"title": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"category": "Anime - English-translated",
"date": "2024-06-30T02:12:07Z",
"datetime": "2024-06-30T02:12:07Z",
"submitter": {
"name": "subsplease",
"url": "https://nyaa.si/user/subsplease",
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $ pynyaa https://nyaa.si/view/1839609 --indent 2
"url": "https://nyaa.si/view/1839609",
"title": "[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv",
"category": "Anime - English-translated",
"date": "2024-06-30T02:12:07Z",
"datetime": "2024-06-30T02:12:07Z",
"submitter": {
"name": "subsplease",
"url": "https://nyaa.si/user/subsplease",
Expand Down
39 changes: 15 additions & 24 deletions src/pynyaa/_models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

from datetime import datetime
from os import fspath
from typing import Any

from pydantic import BaseModel, ConfigDict, HttpUrl, field_serializer, field_validator
from torf import Torrent

from ._enums import Category
from ._types import MagnetUrl
from ._types import MagnetUrl, UTCDateTime


class ParentModel(BaseModel):
Expand Down Expand Up @@ -50,11 +50,8 @@ class Submitter(ParentModel):
>>> a == c
False
>>> set((a, b, c)) # dedupe
{
Submitter(name='Jane', url='https://nyaa.si/user/jane', is_trusted=False, is_banned=False),
Submitter(name='John', url='https://nyaa.si/user/john', is_trusted=True, is_banned=False)
}
>>> set((a, b, c)) == {a, c} == {b, c} # dedupe
True
```
"""

Expand All @@ -80,7 +77,7 @@ def __hash__(self) -> int:
"""
Makes Submitter hashable.
"""
return self.url.__hash__()
return hash(self.url)

def __repr__(self) -> str:
"""
Expand Down Expand Up @@ -119,21 +116,14 @@ class NyaaTorrentPage(ParentModel):
>>> print(a)
[SubsPlease] Hibike! Euphonium S3 - 13 (1080p) [230618C3].mkv
>>> print(repr(a))
NyaaTorrentPage(title='[SubsPlease] Hibike! Euphonium S3 - 13 (1080p) [230618C3].mkv', url='https://nyaa.si/view/1839783', category='Anime - English-translated', date='2024-06-30T10:32:46+00:00', submitter='subsplease')
>>> a == b
True
>>> a == c
False
>>> set((a, b, c)) # dedupe
{
NyaaTorrentPage(title='[SubsPlease] Hibike! Euphonium S3 - 13 (1080p) [230618C3].mkv', url='https://nyaa.si/view/1839783', category='Anime - English-translated', date='2024-06-30T10:32:46+00:00', submitter='subsplease'),
NyaaTorrentPage(title='[SubsPlease] One Piece - 1110 (1080p) [B66CAB32].mkv', url='https://nyaa.si/view/1839609', category='Anime - English-translated', date='2024-06-30T02:12:07+00:00', submitter='subsplease')
}
```
>>> set((a, b, c)) == {a, c} == {b, c} # dedupe
True
"""

id: int
Expand All @@ -148,12 +138,12 @@ class NyaaTorrentPage(ParentModel):
category: Category
"""Torrent category."""

date: datetime
"""Date and time at which the torrent was submitted."""

submitter: Submitter
"""User who submitted the torrent."""

datetime: UTCDateTime
"""Date and time at which the torrent was submitted."""

information: str | None
"""Information about the torrent."""

Expand Down Expand Up @@ -201,7 +191,7 @@ class NyaaTorrentPage(ParentModel):

torrent: Torrent
"""
A [`torf.Torrent`](https://torf.readthedocs.io/en/latest/#torf.Torrent) object
A [`torf.Torrent`][torf.Torrent] object
representing the data stored in the `.torrent` file.
"""

Expand All @@ -221,7 +211,7 @@ def _serialize_torf_torrent(torrent: Torrent) -> dict[str, Any]:
# Convert torf.Files object into a list of dictionaries
files = []
for file in torrent.files:
files.append(dict(file=file.__fspath__(), size=file.size))
files.append(dict(file=fspath(file), size=file.size))

return dict(
name=torrent.name,
Expand Down Expand Up @@ -261,14 +251,15 @@ def __hash__(self) -> int:
"""
Makes NyaaTorrentPage hashable.
"""
return self.url.__hash__()
return hash(self.url)

def __repr__(self) -> str:
"""
A shorter human readable __repr__ because
the default one is too long.
"""
return f"{self.__class__.__name__}(title='{self.title}', url='{self.url}', category='{self.category}', date='{self.date.isoformat()}', submitter='{self.submitter.name}')"
body = f"title='{self.title}', url='{self.url}', category='{self.category}', submitter='{self.submitter}', datetime='{self.datetime.isoformat()}'"
return f"{self.__class__.__name__}({body})"

def __str__(self) -> str:
"""
Expand Down
6 changes: 5 additions & 1 deletion src/pynyaa/_types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

from datetime import datetime, timezone
from typing import Annotated, Literal, Union

from pydantic import AnyUrl, UrlConstraints
from pydantic import AfterValidator, AnyUrl, UrlConstraints

UTCDateTime = Annotated[datetime, AfterValidator(lambda dt: dt.astimezone(timezone.utc))]
"""datetime.datetime that's always in UTC."""

MagnetUrl = Annotated[AnyUrl, UrlConstraints(allowed_schemes=["magnet"])]
"""Url that only allows magnets."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ def test_nyaa_torrent_page() -> None:

assert (
page_a.model_dump_json()
== """{"id":123456,"url":"https://nyaa.si/view/123456","title":"title","category":"Anime - English-translated","date":"2024-06-30T00:00:00","submitter":{"name":"John","url":"https://nyaa.si/user/john","is_trusted":true,"is_banned":false},"information":null,"seeders":20,"leechers":30,"completed":100,"is_trusted":false,"is_remake":false,"description":null,"torrent_file":"https://nyaa.si/download/123456.torrent","magnet":"magnet:?xt=urn:btih:...&dn=...","torrent":{"name":"ubuntu-22.04.4-live-server-amd64.iso","size":2104408064,"infohash":"4d29c6c02c97caad937d8a9b66b0bb1b6f7cbbfe","piece_size":262144,"private":null,"trackers":[["https://torrent.ubuntu.com/announce"],["https://ipv6.torrent.ubuntu.com/announce"]],"comment":"Ubuntu CD releases.ubuntu.com","creation_date":null,"created_by":"mktorrent 1.1","source":null,"files":[{"file":"ubuntu-22.04.4-live-server-amd64.iso","size":2104408064}]}}"""
== """{"id":123456,"url":"https://nyaa.si/view/123456","title":"title","category":"Anime - English-translated","datetime":"2024-06-30T00:00:00","submitter":{"name":"John","url":"https://nyaa.si/user/john","is_trusted":true,"is_banned":false},"information":null,"seeders":20,"leechers":30,"completed":100,"is_trusted":false,"is_remake":false,"description":null,"torrent_file":"https://nyaa.si/download/123456.torrent","magnet":"magnet:?xt=urn:btih:...&dn=...","torrent":{"name":"ubuntu-22.04.4-live-server-amd64.iso","size":2104408064,"infohash":"4d29c6c02c97caad937d8a9b66b0bb1b6f7cbbfe","piece_size":262144,"private":null,"trackers":[["https://torrent.ubuntu.com/announce"],["https://ipv6.torrent.ubuntu.com/announce"]],"comment":"Ubuntu CD releases.ubuntu.com","creation_date":null,"created_by":"mktorrent 1.1","source":null,"files":[{"file":"ubuntu-22.04.4-live-server-amd64.iso","size":2104408064}]}}"""
)

0 comments on commit 0a6ee3f

Please sign in to comment.