Skip to content

Commit

Permalink
fix: return None for empty descriptions and information fields
Browse files Browse the repository at this point in the history
Previously it just returned Nyaa's placeholder value
  • Loading branch information
Ravencentric committed Jun 20, 2024
1 parent 9273c8e commit 79ef55b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
28 changes: 25 additions & 3 deletions src/pynyaa/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from datetime import datetime

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

from ._enums import NyaaCategory
Expand Down Expand Up @@ -51,7 +51,7 @@ class NyaaTorrentPage(ParentModel):
submitter: Submitter
"""User who submitted the torrent."""

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

seeders: int
Expand All @@ -78,7 +78,7 @@ class NyaaTorrentPage(ParentModel):
This is a current limitation that I don't know how to work around.
"""

description: str
description: str | None
"""Torrent description."""

torrent_file: HttpUrl
Expand All @@ -102,6 +102,28 @@ class NyaaTorrentPage(ParentModel):
representing the data stored in the `.torrent` file.
"""

@field_validator("information")
@classmethod
def replace_empty_info_with_none(cls, information: str) -> str | None:
"""
If the information field is empty, Nyaa replaces it with a placeholder value.
This replaces said placeholder value with `None`.
"""
if information == "No information.":
return None
return information

@field_validator("description")
@classmethod
def replace_empty_desc_with_none(cls, description: str) -> str | None:
"""
If the description field is empty, Nyaa replaces it with a placeholder value.
This replaces said placeholder value with `None`.
"""
if description == "#### No description.":
return None
return description


__all__ = [
"Submitter",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ async def test_nyaa_banned_and_trusted() -> None:

async def test_nyaa_empty_desc_info() -> None:
nyaa = await client.get("https://nyaa.si/view/1586776")
assert nyaa.information == "No information."
assert nyaa.description == "#### No description."
assert nyaa.information is None
assert nyaa.description is None
4 changes: 2 additions & 2 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ def test_nyaa_banned_and_trusted() -> None:

def test_nyaa_empty_desc_info() -> None:
nyaa = client.get("https://nyaa.si/view/1586776")
assert nyaa.information == "No information."
assert nyaa.description == "#### No description."
assert nyaa.information is None
assert nyaa.description is None

0 comments on commit 79ef55b

Please sign in to comment.