Skip to content

Commit

Permalink
replaced old manga model with a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonzorn committed Jul 13, 2024
1 parent fed1730 commit 5b19fc0
Show file tree
Hide file tree
Showing 33 changed files with 106 additions and 165 deletions.
2 changes: 1 addition & 1 deletion nlightreader/dialogs/Rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from data.ui.dialogs.rate import Ui_Dialog
from nlightreader.consts.enums import LIB_LISTS, Nl
from nlightreader.items import Manga
from nlightreader.models import Manga
from nlightreader.utils import translate
from nlightreader.utils.catalog_manager import (
get_catalog_by_id,
Expand Down
2 changes: 1 addition & 1 deletion nlightreader/items/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .RequestForm import RequestForm
from .manga_items import Manga, Chapter, Image, Character
from .manga_items import Chapter, Image, Character
from .other_items import HistoryNote, User, UserRate
from .sort_items import Kind, Genre, Order, Status
90 changes: 0 additions & 90 deletions nlightreader/items/manga_items.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,5 @@
import logging
import re

from PySide6.QtCore import QLocale

from nlightreader.consts.enums import Nl
from nlightreader.items.BaseItem import BaseItem
from nlightreader.items.sort_items import Genre


class Manga(BaseItem):
def __init__(self, content_id: str, catalog_id, name, russian):
super().__init__(content_id, catalog_id, name, russian)
self.__kind: Nl.MangaKind = Nl.MangaKind.undefined
self._description: dict[Nl.Language, str] = {}
self.__score: int | float = 0
self.status: str | None = None
self.genres: list[Genre] = []
self.volumes = 0
self.chapters = 0
self.preview_url: str | None = None

@property
def score(self):
return self.__score

@score.setter
def score(self, score):
if isinstance(score, float) and score.is_integer():
score = int(score)
self.__score = score

@property
def kind(self):
return self.__kind

@kind.setter
def kind(self, kind):
if not isinstance(kind, Nl.MangaKind):
raise TypeError("Kind must be Nl.MangaKind")
self.__kind = kind

def add_description(self, language: Nl.Language, description: str):
self._description.update({language: description})

def get_description(self) -> str:
if self._description.get(Nl.Language.undefined):
return self._description.get(Nl.Language.undefined)
if QLocale().language() in (
QLocale.Language.Russian,
QLocale.Language.Ukrainian,
) and self._description.get(Nl.Language.ru):
return self._description.get(Nl.Language.ru)
return self._description.get(Nl.Language.en)

def descriptions_to_str(self) -> str:
desc_str = ""
for key in self._description:
if self._description.get(key):
desc_str += (
f"<lang={key.name}>"
f"{self._description.get(key)}"
f"<end>"
)
return desc_str

def set_description_from_str(self, desc: str):
for lang, text in re.findall(
r"<lang=(\w+)>(.+?)<end>",
desc,
re.DOTALL,
):
self.add_description(
Nl.Language.from_str(lang),
text,
)

def to_dict(self) -> dict:
return {
"id": self.id,
"content_id": self.content_id,
"catalog_id": self.catalog_id,
"name": self.name,
"russian": self.russian,
"kind": self.kind.name,
"description": self.descriptions_to_str(),
"score": self.score,
"status": self.status,
"volumes": self.volumes,
"chapters": self.chapters,
"preview_url": self.preview_url,
}


class Chapter:
Expand Down
3 changes: 2 additions & 1 deletion nlightreader/items/other_items.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from nlightreader.consts.enums import Nl
from nlightreader.items import Chapter, Manga
from nlightreader.items import Chapter
from nlightreader.models import Manga


class HistoryNote:
Expand Down
3 changes: 2 additions & 1 deletion nlightreader/parsers/LocalLib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from nlightreader.items import Manga, RequestForm
from nlightreader.items import RequestForm
from nlightreader.models import Manga
from nlightreader.utils.database import Database


Expand Down
2 changes: 1 addition & 1 deletion nlightreader/parsers/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
Genre,
Image,
Kind,
Manga,
Order,
RequestForm,
User,
UserRate,
)
from nlightreader.models import Manga


class AbstractCatalog:
Expand Down
3 changes: 2 additions & 1 deletion nlightreader/parsers/combined/lib/lib_anilib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from nlightreader.consts.enums import Nl
from nlightreader.consts.items import AniLibItems
from nlightreader.consts.urls import URL_ANILIB
from nlightreader.items import Chapter, Manga
from nlightreader.items import Chapter
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractAnimeCatalog
from nlightreader.parsers.combined.lib.lib_base import LibBase
from nlightreader.utils.utils import get_html
Expand Down
7 changes: 4 additions & 3 deletions nlightreader/parsers/combined/lib/lib_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from nlightreader.consts.enums import Nl
from nlightreader.consts.urls import URL_LIB_API
from nlightreader.items import Chapter, Manga, RequestForm
from nlightreader.items import Chapter, RequestForm
from nlightreader.models import Manga
from nlightreader.parsers.catalog import AbstractCatalog
from nlightreader.utils.utils import get_html

Expand All @@ -21,7 +22,7 @@ def get_manga(self, manga: Manga) -> Manga:
if response:
data = response["data"]
manga.preview_url = data["cover"]["md"]
manga.score = data["rating"]["average"]
manga.score = float(data["rating"]["average"])
if description := data.get("summary"):
manga.add_description(Nl.Language.ru, description)
return manga
Expand Down Expand Up @@ -52,7 +53,7 @@ def search_manga(self, form: RequestForm) -> list[Manga]:
i["rus_name"],
)
manga.preview_url = i["cover"]["md"]
manga.score = i["rating"]["average"]
manga.score = float(i["rating"]["average"])
mangas.append(manga)
return mangas

Expand Down
3 changes: 2 additions & 1 deletion nlightreader/parsers/combined/lib/lib_mangalib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from nlightreader.consts.items import MangaLibItems
from nlightreader.consts.urls import URL_MANGALIB
from nlightreader.items import Chapter, Image, Manga
from nlightreader.items import Chapter, Image
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractMangaCatalog
from nlightreader.parsers.combined.lib.lib_base import LibBase
from nlightreader.utils.utils import get_html
Expand Down
3 changes: 2 additions & 1 deletion nlightreader/parsers/combined/lib/lib_ranobelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from nlightreader.consts.enums import Nl
from nlightreader.consts.items import RanobeLibItems
from nlightreader.consts.urls import URL_RANOBELIB
from nlightreader.items import Chapter, Image, Manga
from nlightreader.items import Chapter, Image
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractRanobeCatalog
from nlightreader.parsers.combined.lib.lib_base import LibBase
from nlightreader.utils.utils import get_html
Expand Down
14 changes: 8 additions & 6 deletions nlightreader/parsers/combined/shikimori/shikimori_anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
Chapter,
Character,
Genre,
Manga,
Order,
RequestForm,
)
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractAnimeCatalog
from nlightreader.parsers.service.kodik import Kodik
from nlightreader.utils.utils import get_html
Expand Down Expand Up @@ -43,11 +43,13 @@ def get_manga(self, manga: Manga) -> Manga:
data = response
# manga.kind = Nl.MangaKind.from_str(data.get("kind"))
manga.score = float(data.get("score"))
manga.status = data.get("status")
manga.add_description(
Nl.Language.undefined,
data.get("description"),
)
manga.status = Nl.MangaStatus.from_str(data.get("status"))

if description := data.get("description"):
manga.add_description(
Nl.Language.undefined,
description,
)
return manga

def search_manga(self, form: RequestForm):
Expand Down
14 changes: 8 additions & 6 deletions nlightreader/parsers/combined/shikimori/shikimori_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
URL_SHIKIMORI,
URL_SHIKIMORI_API,
)
from nlightreader.items import Character, Genre, Manga, Order
from nlightreader.items import Character, Genre, Order
from nlightreader.models import Manga
from nlightreader.parsers.catalog import AbstractCatalog
from nlightreader.utils.utils import get_html

Expand Down Expand Up @@ -36,16 +37,17 @@ def get_manga(self, manga: Manga) -> Manga:
data = response
manga.kind = Nl.MangaKind.from_str(data.get("kind"))
manga.score = float(data.get("score"))
manga.status = data.get("status")
manga.status = Nl.MangaStatus.from_str(data.get("status"))
if data.get("volumes"):
manga.volumes = int(data.get("volumes"))
if data.get("chapters"):
manga.chapters = int(data.get("chapters"))

manga.add_description(
Nl.Language.undefined,
data.get("description"),
)
if description := data.get("description"):
manga.add_description(
Nl.Language.undefined,
description,
)
return manga

def get_character(self, character: Character) -> Character:
Expand Down
3 changes: 2 additions & 1 deletion nlightreader/parsers/combined/shikimori/shikimori_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
URL_SHIKIMORI_TOKEN,
)
from nlightreader.consts.enums import Nl
from nlightreader.items import Manga, RequestForm, User, UserRate
from nlightreader.items import RequestForm, User, UserRate
from nlightreader.models import Manga
from nlightreader.parsers.catalog import LibParser
from nlightreader.parsers.combined.shikimori.shikimori_base import (
ShikimoriBase,
Expand Down
3 changes: 2 additions & 1 deletion nlightreader/parsers/hentai_manga/allhentai_hmanga.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from nlightreader.consts.urls import URL_ALLHENTAI, URL_ALLHENTAI_API
from nlightreader.consts.enums import Nl
from nlightreader.items import Chapter, Image, Manga
from nlightreader.items import Chapter, Image
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractHentaiMangaCatalog
from nlightreader.utils.utils import get_html, make_request

Expand Down
3 changes: 2 additions & 1 deletion nlightreader/parsers/hentai_manga/nhentai_hmanga.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from bs4 import BeautifulSoup

from nlightreader.consts.urls import URL_NHENTAI, URL_NHENTAI_API
from nlightreader.items import Chapter, Image, Manga
from nlightreader.items import Chapter, Image
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractHentaiMangaCatalog
from nlightreader.utils.utils import get_html

Expand Down
2 changes: 1 addition & 1 deletion nlightreader/parsers/manga/Lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from nlightreader.consts.items import MangaLibItems
from nlightreader.consts.urls import URL_SLASHLIB
from nlightreader.items import Manga
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractMangaCatalog
from nlightreader.utils.utils import get_html

Expand Down
18 changes: 5 additions & 13 deletions nlightreader/parsers/manga/desu_manga.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from nlightreader.consts.urls import DESU_HEADERS, URL_DESU, URL_DESU_API
from nlightreader.consts.enums import Nl
from nlightreader.consts.items import DesuItems
from nlightreader.items import Chapter, Genre, Image, Manga, RequestForm
from nlightreader.items import Chapter, Genre, Image, RequestForm
from nlightreader.models import Manga
from nlightreader.parsers.catalogs_base import AbstractMangaCatalog
from nlightreader.utils.utils import get_data, get_html

Expand All @@ -22,20 +23,11 @@ def get_manga(self, manga: Manga):
response = get_html(url, headers=self.headers, content_type="json")
if response:
data = get_data(response, ["response"], {})
manga.genres = [
Genre(
str(i.get("id")),
self.CATALOG_ID,
i.get("text"),
i.get("russian"),
)
for i in data.get("genres")
]
manga.score = data.get("score")
manga.kind = Nl.MangaKind.from_str(data.get("kind"))
manga.volumes = data.get("chapters").get("last").get("vol")
manga.chapters = data.get("chapters").get("last").get("ch")
manga.status = data.get("status")
manga.volumes = int(data["chapters"].get("last").get("vol"))
manga.chapters = int(data["chapters"]["count"])
manga.status = Nl.MangaStatus.from_str(data.get("status"))

manga.add_description(
Nl.Language.undefined,
Expand Down
17 changes: 7 additions & 10 deletions nlightreader/parsers/manga/mangadex_manga.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
Genre,
Image,
Kind,
Manga,
RequestForm,
User,
)
from nlightreader.models import Manga
from nlightreader.parsers.catalog import LibParser
from nlightreader.parsers.catalogs_base import AbstractMangaCatalog
from nlightreader.utils.decorators import singleton
Expand Down Expand Up @@ -45,8 +45,7 @@ def get_manga(self, manga: Manga) -> Manga:
if response:
data = get_data(response, ["data"])
manga.kind = Nl.MangaKind.from_str(data.get("type"))
description = get_data(data, ["attributes", "description"])
if description:
if description := get_data(data, ["attributes", "description"]):
if description.get("en"):
manga.add_description(
Nl.Language.en,
Expand All @@ -57,13 +56,11 @@ def get_manga(self, manga: Manga) -> Manga:
Nl.Language.ru,
description.get("ru"),
)
volumes = get_data(data, ["attributes", "lastVolume"])
chapters = get_data(data, ["attributes", "lastChapter"])
if volumes:
manga.volumes = volumes
if chapters:
manga.chapters = chapters
manga.status = get_data(data, ["attributes", "status"])
if volumes := get_data(data, ["attributes", "lastVolume"]):
manga.volumes = int(volumes)
if chapters := get_data(data, ["attributes", "lastChapter"]):
manga.chapters = int(chapters)
manga.status = Nl.MangaStatus.from_str(get_data(data, ["attributes", "status"]))
return manga

def setup_manga(self, data: dict):
Expand Down
Loading

0 comments on commit 5b19fc0

Please sign in to comment.