Skip to content

Commit

Permalink
feat!: rewrite enums and add new id property
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Jun 23, 2024
1 parent 625fb7f commit a1bffa3
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 25 deletions.
6 changes: 6 additions & 0 deletions docs/api-reference/enums.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
::: pynyaa._enums.NyaaCategory
::: pynyaa._enums.Anime
::: pynyaa._enums.Audio
::: pynyaa._enums.Literature
::: pynyaa._enums.LiveAction
::: pynyaa._enums.Pictures
::: pynyaa._enums.Software
165 changes: 146 additions & 19 deletions src/pynyaa/_enums.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,155 @@
from ._compat import StrEnum

__all__ = ("NyaaCategory",)

class NyaaCategory(StrEnum):
"""Nyaa categories"""

ANIME_MUSIC_VIDEO = "Anime - Anime Music Video"
ANIME_ENGLISH_TRANSLATED = "Anime - English-translated"
ANIME_NON_ENGLISH_TRANSLATED = "Anime - Non-English-translated"
ANIME_RAW = "Anime - Raw"
class Anime(StrEnum):
ALL = "Anime"
MUSIC_VIDEO = "Anime - Anime Music Video"
ENGLISH_TRANSLATED = "Anime - English-translated"
NON_ENGLISH_TRANSLATED = "Anime - Non-English-translated"
RAW = "Anime - Raw"

AUDIO_LOSSLESS = "Audio - Lossless"
AUDIO_LOSSY = "Audio - Lossy"
@property
def id(self) -> str:
"""
Returns the ID of the category.
LITERATURE_ENGLISH_TRANSLATED = "Literature - English-translated"
LITERATURE_NON_ENGLISH_TRANSLATED = "Literature - Non-English-translated"
LITERATURE_RAW = "Literature - Raw"
This ID corresponds to the category as seen in the URL
`https://nyaa.si/?f=0&c=1_2&q=`, where `c=1_2` is the ID for `Anime - English-translated`.
"""
mapping = {
"Anime": "1_0",
"Anime - Anime Music Video": "1_1",
"Anime - English-translated": "1_2",
"Anime - Non-English-translated": "1_3",
"Anime - Raw": "1_4",
}

LIVE_ACTION_ENGLISH_TRANSLATED = "Live Action - English-translated"
LIVE_ACTION_IDOL_PROMOTIONAL_VIDEO = "Live Action - Idol/Promotional Video"
LIVE_ACTION_NON_ENGLISH_TRANSLATED = "Live Action - Non-English-translated"
LIVE_ACTION_RAW = "Live Action - Raw"
return mapping[self.value]

PICTURES_GRAPHICS = "Pictures - Graphics"
PICTURES_PHOTOS = "Pictures - Photos"

SOFTWARE_APPLICATIONS = "Software - Applications"
SOFTWARE_GAMES = "Software - Games"
class Audio(StrEnum):
ALL = "Audio"
LOSSLESS = "Audio - Lossless"
LOSSY = "Audio - Lossy"

@property
def id(self) -> str:
"""
Returns the ID of the category.
This ID corresponds to the category as seen in the URL
`https://nyaa.si/?f=0&c=2_1&q=`, where `c=2_1` is the ID for `Audio - Lossless`.
"""
mapping = {
"Audio": "2_0",
"Audio - Lossless": "2_1",
"Audio - Lossy": "2_2",
}

return mapping[self.value]


class Literature(StrEnum):
ALL = "Literature"
ENGLISH_TRANSLATED = "Literature - English-translated"
NON_ENGLISH_TRANSLATED = "Literature - Non-English-translated"
RAW = "Literature - Raw"

@property
def id(self) -> str:
"""
Returns the ID of the category.
This ID corresponds to the category as seen in the URL
`https://nyaa.si/?f=0&c=3_1&q=`, where `c=3_1` is the ID for `Literature - English-translated`.
"""
mapping = {
"Literature": "3_0",
"Literature - English-translated": "3_1",
"Literature - Non-English-translated": "3_2",
"Literature - Raw": "3_3",
}

return mapping[self.value]


class LiveAction(StrEnum):
ALL = "Live Action"
ENGLISH_TRANSLATED = "Live Action - English-translated"
IDOL_PROMOTIONAL_VIDEO = "Live Action - Idol/Promotional Video"
NON_ENGLISH_TRANSLATED = "Live Action - Non-English-translated"
RAW = "Live Action - Raw"

@property
def id(self) -> str:
"""
Returns the ID of the category.
This ID corresponds to the category as seen in the URL
`https://nyaa.si/?f=0&c=4_1&q=`, where `c=4_1` is the ID for `Live Action - English-translated`.
"""
mapping = {
"Live Action": "4_0",
"Live Action - English-translated": "4_1",
"Live Action - Idol/Promotional Video": "4_2",
"Live Action - Non-English-translated": "4_3",
"Live Action - Raw": "4_4",
}

return mapping[self.value]


class Pictures(StrEnum):
ALL = "Pictures"
GRAPHICS = "Pictures - Graphics"
PHOTOS = "Pictures - Photos"

@property
def id(self) -> str:
"""
Returns the ID of the category.
This ID corresponds to the category as seen in the URL
`https://nyaa.si/?f=0&c=5_1&q=`, where `c=5_1` is the ID for `Pictures - Graphics`.
"""
mapping = {
"Pictures": "5_0",
"Pictures - Graphics": "5_1",
"Pictures - Photos": "5_2",
}

return mapping[self.value]


class Software(StrEnum):
ALL = "Software"
APPLICATIONS = "Software - Applications"
GAMES = "Software - Games"

@property
def id(self) -> str:
"""
Returns the ID of the category.
This ID corresponds to the category as seen in the URL
`https://nyaa.si/?f=0&c=6_1&q=`, where `c=6_1` is the ID for `Software - Applications`.
"""
mapping = {
"Software": "6_0",
"Software - Applications": "6_1",
"Software - Games": "6_2",
}

return mapping[self.value]

class NyaaCategory:
"""This class holds all the available categories on Nyaa.si"""

ANIME = Anime
AUDIO = Audio
LITERATURE = Literature
LIVE_ACTION = LiveAction
PICTURES = Pictures
SOFTWARE = Software
6 changes: 4 additions & 2 deletions src/pynyaa/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydantic import AnyUrl, BaseModel, ConfigDict, HttpUrl, field_validator
from torf import Torrent

from ._enums import NyaaCategory
from ._enums import Anime, Audio, Literature, LiveAction, Pictures, Software

__all__ = (
"Submitter",
Expand Down Expand Up @@ -47,7 +47,7 @@ class NyaaTorrentPage(ParentModel):
title: str
"""Title of the torrent."""

category: NyaaCategory
category: Anime | Audio | Literature | LiveAction | Pictures | Software
"""Torrent category."""

date: datetime
Expand Down Expand Up @@ -128,4 +128,6 @@ def replace_empty_desc_with_none(cls, description: str) -> str | None:
if description == "#### No description.":
return None
return description



4 changes: 2 additions & 2 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def test_nyaa_trusted() -> None:
assert nyaa.is_remake is False
assert nyaa.is_trusted is True
assert nyaa.description == description
assert nyaa.category == NyaaCategory.ANIME_ENGLISH_TRANSLATED
assert nyaa.category == NyaaCategory.ANIME.ENGLISH_TRANSLATED


async def test_nyaa_trusted_and_remake() -> None:
Expand All @@ -57,7 +57,7 @@ async def test_nyaa_anon() -> None:
assert nyaa.url.__str__() == "https://nyaa.si/view/1765655"
assert nyaa.information == "https://www.goodreads.com/series/220639-ascendance-of-a-bookworm-light-novel"
assert nyaa.submitter.name == "Anonymous"
assert nyaa.category == NyaaCategory.LITERATURE_ENGLISH_TRANSLATED
assert nyaa.category == NyaaCategory.LITERATURE.ENGLISH_TRANSLATED


async def test_nyaa_banned_and_trusted() -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_nyaa_trusted() -> None:
assert nyaa.is_remake is False
assert nyaa.is_trusted is True
assert nyaa.description == description
assert nyaa.category == NyaaCategory.ANIME_ENGLISH_TRANSLATED
assert nyaa.category == NyaaCategory.ANIME.ENGLISH_TRANSLATED


def test_nyaa_trusted_and_remake() -> None:
Expand All @@ -57,7 +57,7 @@ def test_nyaa_anon() -> None:
assert nyaa.url.__str__() == "https://nyaa.si/view/1765655"
assert nyaa.information == "https://www.goodreads.com/series/220639-ascendance-of-a-bookworm-light-novel"
assert nyaa.submitter.name == "Anonymous"
assert nyaa.category == NyaaCategory.LITERATURE_ENGLISH_TRANSLATED
assert nyaa.category == NyaaCategory.LITERATURE.ENGLISH_TRANSLATED


def test_nyaa_banned_and_trusted() -> None:
Expand Down

0 comments on commit a1bffa3

Please sign in to comment.