rework tagger a bit
This commit is contained in:
132
src/tagger.py
132
src/tagger.py
@@ -1,52 +1,38 @@
|
||||
from komgAPI import KOMGAPI_REST as komga
|
||||
from komgAPI.schemas import Metadata
|
||||
from .api import constants
|
||||
from .api.mangadex import MangadexAPI
|
||||
from komgapi import komgapi as komga
|
||||
from komconfig import KomConfig
|
||||
from .api.anilistapi import anilistAPI
|
||||
from typing import Union
|
||||
from dotenv import load_dotenv
|
||||
from .logger import log
|
||||
import os
|
||||
from src.logic.metadataHandlers import handleComicVine, handleAnilist
|
||||
from logic.metadataHandlers import handleAnilist
|
||||
from .similarity import similarity
|
||||
|
||||
load_dotenv()
|
||||
from .utils import write
|
||||
|
||||
|
||||
def translate(input: str):
|
||||
# replace & with and
|
||||
title = input.replace("&", "and")
|
||||
title = title.replace(" ", "%20")
|
||||
return title
|
||||
|
||||
|
||||
def write(name, file):
|
||||
with open(f"{file}.txt", "a") as f:
|
||||
# add a new line
|
||||
f.write(name + "\n")
|
||||
|
||||
|
||||
SKIPS = ["Fairy Tail Side Stories"]
|
||||
SKIPS = []
|
||||
|
||||
|
||||
class Tagger:
|
||||
def __init__(self, fallbackAPI=None):
|
||||
def __init__(self):
|
||||
self.config = KomConfig()
|
||||
self.komga = komga(
|
||||
url=os.getenv("KOMGA_URL"),
|
||||
username=os.getenv("KOMGA_USERNAME"),
|
||||
password=os.getenv("KOMGA_PASSWORD"),
|
||||
url=self.config.general.komga.url,
|
||||
username=self.config.general.komga.user,
|
||||
password=self.config.general.komga.password,
|
||||
timeout=60,
|
||||
)
|
||||
self.mangadex = MangadexAPI()
|
||||
self.mode = None
|
||||
|
||||
self.api: anilistAPI = anilistAPI()
|
||||
|
||||
log.info("Tagger initialized")
|
||||
# create failed and success files
|
||||
if not os.path.exists("failed.txt"):
|
||||
with open("failed.txt", "w") as f:
|
||||
f.write("")
|
||||
if not os.path.exists("success.txt"):
|
||||
with open("success.txt", "w") as f:
|
||||
f.write("")
|
||||
|
||||
def get_manga(self, manga_id):
|
||||
manga = self.komga.series_controller.getSeries(manga_id)
|
||||
return manga
|
||||
log.info("Tagger initialized")
|
||||
|
||||
@property
|
||||
def overwrite(self):
|
||||
@@ -74,7 +60,7 @@ class Tagger:
|
||||
if api_metadata.title.english
|
||||
else komga_metadata.name,
|
||||
"titleSortLock": True,
|
||||
"summary": api_metadata.description.split("<br></br>")[0],
|
||||
"summary": converted_api_metadata.summary,
|
||||
"summaryLock": True,
|
||||
"readingDirectionLock": True,
|
||||
"ageRating": converted_api_metadata.ageRating,
|
||||
@@ -94,9 +80,10 @@ class Tagger:
|
||||
"totalBookCount": converted_api_metadata.totalBookCount,
|
||||
"alternateTitles": converted_api_metadata.alternateTitles,
|
||||
}
|
||||
|
||||
if self.mode == "overwrite":
|
||||
log.debug(f"Overwriting metadata for {komga_metadata.name}")
|
||||
log.debug(newMetadata)
|
||||
# log.debug(newMetadata)
|
||||
self.komga.series_controller.patchMetadata(komga_id, newMetadata)
|
||||
elif self.mode == "merge":
|
||||
komga_tags = komga_metadata.metadata.tags
|
||||
@@ -122,6 +109,12 @@ class Tagger:
|
||||
f.write("")
|
||||
with open("success.txt", "r") as f:
|
||||
done = f.read().splitlines()
|
||||
|
||||
with open("failed.txt", "r") as f:
|
||||
failed = f.read().splitlines()
|
||||
|
||||
done.extend(failed)
|
||||
|
||||
for series in komgaseries:
|
||||
seriesTitle = series.name.strip()
|
||||
if "Omnibus" in seriesTitle:
|
||||
@@ -134,7 +127,7 @@ class Tagger:
|
||||
log.info(f"{seriesTitle} is in the skip list, skipping")
|
||||
continue
|
||||
if "&" in seriesTitle:
|
||||
log.info(f"{seriesTitle} contains an ampersand, might cause issues")
|
||||
log.warning(f"{seriesTitle} contains an ampersand, might cause issues")
|
||||
# continue
|
||||
log.info(f"Tagging {seriesTitle}")
|
||||
result = self.get_metadata(seriesTitle, series.id)
|
||||
@@ -143,7 +136,7 @@ class Tagger:
|
||||
write(seriesTitle, "success")
|
||||
else:
|
||||
log.info("Found no titles using main title, using alternate titles")
|
||||
mangadex_titles = self.api.getSeries(series.metadata.get("title"))
|
||||
mangadex_titles = self.api.getSeries(series.metadata.title)
|
||||
if mangadex_titles is None:
|
||||
log.error(f"No titles found for {seriesTitle}")
|
||||
write(seriesTitle, "failed")
|
||||
@@ -151,7 +144,7 @@ class Tagger:
|
||||
for result in mangadex_titles:
|
||||
title = result.title.english
|
||||
alt_titles = result.synonyms
|
||||
result = self.get_metadata(series.metadata.get("title"), series.id)
|
||||
result = self.get_metadata(series.metadata.title, series.id)
|
||||
if result == 1:
|
||||
log.success(f"Tagged {seriesTitle} successfully")
|
||||
write(seriesTitle, "success")
|
||||
@@ -183,7 +176,7 @@ class Tagger:
|
||||
if similarity(mtitle.lower(), result.title.english.lower()) > 0.8:
|
||||
title = result.title.english
|
||||
log.info(f"Found API result for {mtitle}")
|
||||
log.debug(result)
|
||||
log.debug("{}, {}".format(result.id, result.title.english))
|
||||
if title is None:
|
||||
log.warning(f"No title found for {mtitle}")
|
||||
write(mtitle, "failed")
|
||||
@@ -191,19 +184,16 @@ class Tagger:
|
||||
alt_titles = result.synonyms
|
||||
if title.strip() == title.strip():
|
||||
self.get_and_update_manga(id, result.id)
|
||||
log.success(f"Tagged {mtitle} successfully")
|
||||
return 1
|
||||
else:
|
||||
log.info(f"No direct match, searching in alternate Titles {mtitle}")
|
||||
if similarity(mtitle, title) > 0.8:
|
||||
self.get_and_update_manga(id, result.id)
|
||||
log.success(f"Tagged {mtitle} successfully")
|
||||
return 1
|
||||
|
||||
for alt in alt_titles:
|
||||
if similarity(mtitle, alt) > 0.8:
|
||||
self.get_and_update_manga(id, result.id)
|
||||
log.success(f"Tagged {mtitle} successfully")
|
||||
tagged = True
|
||||
break
|
||||
if not tagged:
|
||||
@@ -212,33 +202,33 @@ class Tagger:
|
||||
else:
|
||||
return 1
|
||||
|
||||
def tag_series(self, series_id):
|
||||
series = self.komga.series_controller.getSeries(series_id)
|
||||
log.info(f"Tagging {series.name}")
|
||||
title = translate(series.name)
|
||||
mangadex_titles = self.mangadex.get_series(title)
|
||||
if len(mangadex_titles) == 0:
|
||||
log.warning(f"No mangadex titles found for {series.name}")
|
||||
print(len(mangadex_titles))
|
||||
for result in mangadex_titles:
|
||||
if similarity(series.name, result.name) > 0.8:
|
||||
title = result.name
|
||||
alt_titles = result.alternate_names
|
||||
if series.name.strip() == title.strip():
|
||||
self.get_and_update_manga(series.id, result.series_id)
|
||||
log.info(f"Tagged {series.name} successfully")
|
||||
else:
|
||||
log.info(
|
||||
f"No direct match, searching in alternate Titles {series.name}"
|
||||
)
|
||||
found = False
|
||||
for alt in alt_titles:
|
||||
# alt is a dict, lang: title
|
||||
title = alt.get(list(alt.keys())[0])
|
||||
if series.name.strip() == title.strip():
|
||||
self.get_and_update_manga(series.id, result.series_id)
|
||||
log.info(f"Tagged {series.name} successfully")
|
||||
found = True
|
||||
break
|
||||
if found:
|
||||
break
|
||||
# def tag_series(self, series_id):
|
||||
# series = self.komga.series_controller.getSeries(series_id)
|
||||
# log.info(f"Tagging {series.name}")
|
||||
# title = translate(series.name)
|
||||
# mangadex_titles = self.mangadex.get_series(title)
|
||||
# if len(mangadex_titles) == 0:
|
||||
# log.warning(f"No mangadex titles found for {series.name}")
|
||||
# print(len(mangadex_titles))
|
||||
# for result in mangadex_titles:
|
||||
# if similarity(series.name, result.name) > 0.8:
|
||||
# title = result.name
|
||||
# alt_titles = result.alternate_names
|
||||
# if series.name.strip() == title.strip():
|
||||
# self.get_and_update_manga(series.id, result.series_id)
|
||||
# log.info(f"Tagged {series.name} successfully")
|
||||
# else:
|
||||
# log.info(
|
||||
# f"No direct match, searching in alternate Titles {series.name}"
|
||||
# )
|
||||
# found = False
|
||||
# for alt in alt_titles:
|
||||
# # alt is a dict, lang: title
|
||||
# title = alt.get(list(alt.keys())[0])
|
||||
# if series.name.strip() == title.strip():
|
||||
# self.get_and_update_manga(series.id, result.series_id)
|
||||
# log.info(f"Tagged {series.name} successfully")
|
||||
# found = True
|
||||
# break
|
||||
# if found:
|
||||
# break
|
||||
|
||||
Reference in New Issue
Block a user