Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue #198 #210

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 40 additions & 41 deletions MangaManager/ExternalSources/MetadataSources/Providers/AniList.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class AniList(IMetadataSource):
_log = logging.getLogger()
# Map the Role from API to the ComicInfo tags to write
person_mapper = {}
_HOW_METADATA_MAPS_TOOLTIP = "How metadata field will map to ComicInfo fields"
romaji_as_series = True

_HOW_METADATA_MAPS_TOOLTIP = "How metadata field will map to ComicInfo fields"
romaji_as_series = Settings().get(name, AniListSetting.SeriesTitleLanguage)
def init_settings(self):
self.settings = [
SettingSection(self.name, self.name, [
Expand Down Expand Up @@ -81,43 +81,12 @@ def save_settings(self):
self.name, AniListPerson.Art).split(',')
self.person_mapper["Assistant"] = Settings().get(self.name, AniListPerson.Assistant).split(',')

@staticmethod
def is_valid_person_tag(key, value):
invalid_people = get_invalid_person_tag(value)

if len(invalid_people) == 0:
return ""
return ", ".join(invalid_people) + " are not a valid tags"
@staticmethod
def get_manga_id_from_url(url):
pattern = r"https:\/\/anilist\.co\/manga\/(\d+)"
match = re.search(pattern, url)
if match:
return match.group(1)
return None
@classmethod
def _get_id_from_series(cls, cinfo: ComicInfo) -> Optional[int]:

manga_id = cls.get_manga_id_from_url(cinfo.web)
if manga_id is not None:
return manga_id

try:
content = cls._search_for_manga_title_by_manga_title(cinfo.series, "MANGA", {})
except MangaNotFoundError:
content = cls.search_for_manga_title_by_manga_title_with_adult(cinfo.series, "MANGA", {})

if content is None:
return None
return content.get("id")

@classmethod
def get_cinfo(cls, comic_info_from_ui: ComicInfo) -> ComicInfo | None:
def get_cinfo(self, comic_info_from_ui: ComicInfo) -> ComicInfo | None:
comicinfo = ComicInfo()
serie_id = cls._get_id_from_series(comic_info_from_ui)
serie_id = self._get_id_from_series(comic_info_from_ui)
if serie_id is None:
return None
data = cls._search_details_by_series_id(serie_id, "MANGA", {})
data = self._search_details_by_series_id(serie_id, "MANGA", {})

startdate = data.get("startDate")
comicinfo.day = startdate.get("day")
Expand All @@ -130,27 +99,57 @@ def get_cinfo(cls, comic_info_from_ui: ComicInfo) -> ComicInfo | None:

# Title (Series & LocalizedSeries)
title = data.get("title")
cls._log.info("[AniList] Fetch Data found title " + str(title) + " for " + comic_info_from_ui.series)
self._log.info("[AniList] Fetch Data found title " + str(title) + " for " + comic_info_from_ui.series)
title_english = (data.get("title").get("english") or "").strip()
title_romaji = (data.get("title").get("romaji") or "").strip()
if cls.romaji_as_series:
if self.romaji_as_series:
comicinfo.series = title_romaji
if title_romaji != title_english:
comicinfo.localized_series = title_english
else:
comicinfo.series = title_english
comicinfo.series = title_english if title_english != "" else title_romaji
if title_romaji != title_english:
comicinfo.localized_series = title_romaji

# Summary
comicinfo.summary = IMetadataSource.clean_description(data.get("description"), remove_source=True)

# People
cls.update_people_from_mapping(data["staff"]["edges"], cls.person_mapper, comicinfo,
self.update_people_from_mapping(data["staff"]["edges"], self.person_mapper, comicinfo,
lambda item: item["node"]["name"]["full"],
lambda item: item["role"])

return comicinfo
@staticmethod
def is_valid_person_tag(key, value):
invalid_people = get_invalid_person_tag(value)

if len(invalid_people) == 0:
return ""
return ", ".join(invalid_people) + " are not a valid tags"
@staticmethod
def get_manga_id_from_url(url):
pattern = r"https:\/\/anilist\.co\/manga\/(\d+)"
match = re.search(pattern, url)
if match:
return match.group(1)
return None

@classmethod
def _get_id_from_series(cls, cinfo: ComicInfo) -> Optional[int]:

manga_id = cls.get_manga_id_from_url(cinfo.web)
if manga_id is not None:
return manga_id

try:
content = cls._search_for_manga_title_by_manga_title(cinfo.series, "MANGA", {})
except MangaNotFoundError:
content = cls.search_for_manga_title_by_manga_title_with_adult(cinfo.series, "MANGA", {})

if content is None:
return None
return content.get("id")

@classmethod
def _post(cls, query, variables, logging_info):
Expand Down
28 changes: 23 additions & 5 deletions MangaManager/src/Common/LoadedComicInfo/LoadedComicInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ def get_template_filename(self, input_template: str) -> str|None:
:return: None if there's a missing key in the template
"""
try:
return input_template.format_map(self.get_template_values()).replace(" ", " ")
illegal_chars = ('<', '>', ':', '"', '\\','/', '|', "?", '*')
path_split = input_template.format_map(self.get_template_values()).replace(" ", " ").split('/')
sanitized_path_split = []
for path_component in path_split:
sanitized_path_split.append("".join(ch for ch in path_component if ch.isalnum() or ch not in illegal_chars).strip())
return "/".join(sanitized_path_split)

except KeyError as e:
logger.error(f"Could not get {list(e.args)} keys when filling template values")
return None
Expand Down Expand Up @@ -178,10 +184,22 @@ def _process(self, write_metadata=False, do_convert_to_webp=False, **_):
try:
with ArchiveFile(self.file_path, 'r') as zin:
assert initial_file_count == len(zin.namelist())
os.remove(self.file_path)
os.rename(tmpname, self.file_path)
logger.debug(f"[{'Processing':13s}] Successfully deleted old file and named tempfile as the old file",
extra=self._logging_extra)
# If move_on_update setting is True, we should move the temp file to new location, then delete original file.
if Settings().get("Main", "move_on_update") == True:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand this new code and why it would be used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! I thought the pull was matched to commit not pulling HEAD. Was adding file management functionality on my fork to aid in library management. Lemme close this out and make sure any file management code is removed.

template_str = Settings().get("Main", "move_to_template")
library_path = Settings().get("Main", "library_path")
original_ext = self.file_path.split(".")[-1]
new_file_name = self.get_template_filename(template_str)
new_file_path = f'{library_path}/{new_file_name}.{original_ext}'
os.renames(tmpname, new_file_path)
os.remove(self.file_path)
logger.debug(f"[{'Processing':13s}] Moved file to new template filename under library",
extra=self._logging_extra)
else:
os.remove(self.file_path)
os.rename(tmpname, self.file_path)
logger.debug(f"[{'Processing':13s}] Successfully deleted old file and named tempfile as the old file",
extra=self._logging_extra)
# If we fail to delete original file we delete temp file effecively aborting the metadata update
except PermissionError:
logger.exception(f"[{'Processing':13s}] Permission error. Aborting and clearing temp files",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def template_validation(key_list):
validate=lambda key, value: '[' + ", ".join(template_validation(
re.findall(r'\{(\w+)\}', value))) + "] are not valid tags" if len(
template_validation(re.findall(r'\{(\w+)\}', value))) != 0 else ""),
"move_on_update": SettingControl("move_on_update", "Rename on update",
SettingControlType.Bool, True,
"If enabled, files will be renamed according to the Rename Filename template under the Library Path"),
"clean_ui_on_drag_drop": SettingControl("remove_old_selection_on_drag_drop","Clean previous selection\non drag and drop", SettingControlType.Bool, True, "After you drag and drop, previous selected files will be discarded")
},
SettingHeading.WebpConverter: {
Expand Down
4 changes: 2 additions & 2 deletions MangaManager/src/Settings/SettingsDefault.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class SettingHeading(StrEnum):
{"create_backup_comicinfo": True},
# {"selected_layout": "default"},
{"move_to_template": ""},
{"remove_old_selection_on_drag_drop":True}

{"remove_old_selection_on_drag_drop":True},
{"move_on_update": False}
],
SettingHeading.WebpConverter: [
{"default_base_path": ""},
Expand Down