add utils, anilist, metadatahandler
This commit is contained in:
21
src/api/anilistapi.py
Normal file
21
src/api/anilistapi.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from anilistapi import AnilistAPI
|
||||
|
||||
|
||||
class anilistAPI(AnilistAPI):
|
||||
__name__ = "AnilistAPI"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def getSeries(self, title):
|
||||
search = title
|
||||
manga = self.search_manga(search)
|
||||
|
||||
if manga:
|
||||
return manga
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_metadata(self, manga_id):
|
||||
metadata = self.get_manga(manga_id)
|
||||
return metadata
|
||||
121
src/logic/metadataHandlers.py
Normal file
121
src/logic/metadataHandlers.py
Normal file
@@ -0,0 +1,121 @@
|
||||
from komgapi.schemas import Metadata, AlternateTitle, Link
|
||||
|
||||
# from comicvineapi import Issue
|
||||
from anilistapi.schemas.manga import Manga
|
||||
|
||||
# import hasattr to check if a field exists in the metadata
|
||||
from builtins import hasattr
|
||||
from komconfig import KomConfig
|
||||
|
||||
config = KomConfig()
|
||||
|
||||
# def handleComicVine(metadata: Issue) -> Metadata:
|
||||
# """
|
||||
# Handle metadata from ComicVineAPI.
|
||||
|
||||
# Args:
|
||||
# metadata (Issue): Metadata from ComicVineAPI.
|
||||
|
||||
# Returns:
|
||||
# Metadata: Metadata in a format that can be used by KOMGA.
|
||||
# """
|
||||
# return Metadata()
|
||||
|
||||
|
||||
def anilistAgeRating(metadata: Manga) -> int:
|
||||
if metadata.isAdult:
|
||||
return 18
|
||||
else:
|
||||
if metadata.tags is not None:
|
||||
for tag in metadata.tags:
|
||||
if tag.isAdult:
|
||||
return 18
|
||||
if "Ecchi" in metadata.tags:
|
||||
return 16
|
||||
else:
|
||||
return 12
|
||||
return 12
|
||||
|
||||
|
||||
def anilistStatus(metadata: Manga) -> str:
|
||||
match metadata.status:
|
||||
case "RELEASING":
|
||||
return "ONGOING"
|
||||
case "FINISHED":
|
||||
return "ENDED"
|
||||
|
||||
|
||||
def readingDirection(metadata: Manga) -> str:
|
||||
if metadata.countryOfOrigin == "JP":
|
||||
return "RIGHT_TO_LEFT"
|
||||
|
||||
|
||||
def description(metadata: Manga) -> str:
|
||||
if config.komtagger.sanitize_description:
|
||||
desc = metadata.description
|
||||
if desc:
|
||||
desc = desc.split("\n<br><br>\n(")[0]
|
||||
desc = desc.replace("<br>", "")
|
||||
return desc
|
||||
return metadata.description
|
||||
|
||||
|
||||
def handleAnilist(metadata: Manga) -> Metadata:
|
||||
"""Handle Metadata from Anilist
|
||||
|
||||
Args:
|
||||
metadata (Manga): Metadata from Anilist
|
||||
|
||||
Returns:
|
||||
Metadata: Metadata in a format that can be used by KOMGA
|
||||
"""
|
||||
links = []
|
||||
if hasattr(metadata, "externalLinks") and metadata.externalLinks:
|
||||
for link in metadata.externalLinks:
|
||||
links.append(
|
||||
Link(url=link.url.replace(" ", "%20"), label=link.site).__dict__
|
||||
)
|
||||
# synonyms = [
|
||||
# AlternateTitle(title=alt, label="Synonym") for alt in metadata.synonyms
|
||||
# ]
|
||||
alternateTitles = []
|
||||
# alternateTitles is a list of dicts, use key as label and value as title
|
||||
|
||||
for alternate in metadata.title.alternateTitles:
|
||||
# alternate is a dict, set label from dict key, and value from dict value
|
||||
label = list(alternate.keys())[0]
|
||||
title = alternate[label]
|
||||
if title is not None:
|
||||
alt_Title = {"label": label, "title": title}
|
||||
alternateTitles.append(alt_Title)
|
||||
|
||||
# alternateTitles.extend(synonyms)
|
||||
return Metadata(
|
||||
status=anilistStatus(metadata),
|
||||
statusLock=True,
|
||||
title=metadata.title,
|
||||
titleLock=True,
|
||||
titleSort=metadata.title,
|
||||
titleSortLock=True,
|
||||
summary=description(metadata),
|
||||
summaryLock=True,
|
||||
readingDirection=readingDirection(metadata),
|
||||
readingDirectionLock=True,
|
||||
publisherLock=True,
|
||||
ageRating=anilistAgeRating(metadata),
|
||||
ageRatingLock=True,
|
||||
language="en",
|
||||
languageLock=True,
|
||||
genres=metadata.genres,
|
||||
genresLock=True,
|
||||
tags=[tag.name for tag in metadata.tags] if metadata.tags else None,
|
||||
tagsLock=True,
|
||||
totalBookCount=metadata.volumes if metadata.volumes else None,
|
||||
totalBookCountLock=True if metadata.volumes else False,
|
||||
sharingLabels=[None],
|
||||
sharingLabelsLock=False,
|
||||
links=links,
|
||||
linksLock=True,
|
||||
alternateTitles=alternateTitles,
|
||||
alternateTitlesLock=True,
|
||||
)
|
||||
4
src/utils.py
Normal file
4
src/utils.py
Normal file
@@ -0,0 +1,4 @@
|
||||
def write(name, file):
|
||||
with open(f"{file}.txt", "a") as f:
|
||||
# add a new line
|
||||
f.write(name + "\n")
|
||||
Reference in New Issue
Block a user