Skip to content

Commit

Permalink
Refactor _get_id_from_comic_info (#164)
Browse files Browse the repository at this point in the history
* Refactor the code getting the issue id from a CI note.
* Bump min version of darkseid
  • Loading branch information
bpepple authored Dec 1, 2024
1 parent faa48cf commit 95db60c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
31 changes: 11 additions & 20 deletions metrontagger/talker.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Role,
Series,
)
from darkseid.utils import get_issue_id_from_note
from imagehash import ImageHash, hex_to_hash, phash
from mokkari.exceptions import ApiError
from PIL import Image
Expand Down Expand Up @@ -233,36 +234,26 @@ def _get_id_from_metron_info(md: Metadata) -> None | tuple[InfoSource, int]:

@staticmethod
def _get_id_from_comic_info(md: Metadata) -> None | tuple[InfoSource, int]:
def _extract_id_str(notes: str, keyword: str) -> str:
return notes.split(keyword)[1].split("]")[0].strip()
if md.notes is None or not md.notes.comic_rack:
return None

# If `Notes` element doesn't exist let's bail.
if md.notes is None:
res = get_issue_id_from_note(md.notes.comic_rack)
if res is None:
return None

lower_notes = md.notes.comic_rack.lower()

if "metrontagger" in lower_notes:
source = InfoSource.metron
id_str = _extract_id_str(md.notes.comic_rack, "issue_id:")
elif "comictagger" in lower_notes:
if "metron" in lower_notes:
source = InfoSource.metron
elif "comic vine" in lower_notes:
source = InfoSource.comic_vine
else:
source = InfoSource.unknown
id_str = _extract_id_str(md.notes.comic_rack, "Issue ID")
else:
source_map = {"Metron": InfoSource.metron, "Comic Vine": InfoSource.comic_vine}

src = source_map.get(res["source"])
if src is None:
return None

try:
id_ = int(id_str)
id_ = int(res["id"])
except ValueError:
LOGGER.exception("Comic has invalid id: %s #%s", md.series.name, md.issue)
return None

return source, id_
return src, id_

def _process_file(self: Talker, fn: Path, interactive: bool) -> tuple[int | None, bool]: # noqa: PLR0912 PLR0911
"""Process a comic file for metadata.
Expand Down
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ imagehash = "^4.3.1"
pandas = "^2.2.1"
comicfn2dict = "^0.2.4"
tqdm = "^4.66.4"
darkseid = "^5.1.0"
darkseid = "^5.1.1"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.6.2"
Expand Down

0 comments on commit 95db60c

Please sign in to comment.