Skip to content

Commit

Permalink
Update for latest darkseid
Browse files Browse the repository at this point in the history
  • Loading branch information
bpepple committed Oct 31, 2024
1 parent 9754b60 commit 4a0d682
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 42 deletions.
46 changes: 20 additions & 26 deletions metrontagger/talker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
from darkseid.comic import Comic, MetadataFormat
from darkseid.issue_string import IssueString
from darkseid.metadata import (
URLS,
AgeRatings,
Arc,
Basic,
Credit,
InfoSources,
Links,
Metadata,
Notes,
Publisher,
Role,
Series,
WebsiteInfo,
)
from imagehash import ImageHash, hex_to_hash, phash
from mokkari.exceptions import ApiError
Expand Down Expand Up @@ -222,22 +222,14 @@ def _get_hamming_results(self: Talker, comic: Comic, lst: list[BaseIssue]) -> li
@staticmethod
def _get_id_from_metron_info(md: Metadata) -> None | tuple[InfoSource, int]:
online_sources = {"metron", "comic vine"}
md_sources = [md.info_source.primary, *md.info_source.alternatives]
return next(
(
(
(
InfoSource.metron
if md_source.name.lower() == "metron"
else InfoSource.comic_vine
),
md_source.id_,
for src in md.info_source:
lower_name = src.name.lower()
if lower_name in online_sources:
return (
InfoSource.metron if lower_name == "metron" else InfoSource.comic_vine,
src.id_,
)
for md_source in md_sources
if md_source and md_source.name.lower() in online_sources
),
None,
)
return None

@staticmethod
def _get_id_from_comic_info(md: Metadata) -> None | tuple[InfoSource, int]:
Expand All @@ -250,7 +242,6 @@ def _extract_id_str(notes: str, keyword: str) -> str:

lower_notes = md.notes.comic_rack.lower()

id_str = ""
if "metrontagger" in lower_notes:
source = InfoSource.metron
id_str = _extract_id_str(md.notes.comic_rack, "issue_id:")
Expand Down Expand Up @@ -589,7 +580,7 @@ def create_role_list(roles: list[GenericItem]) -> list[Role]:
meta_data.add_credit(Credit(c.creator, [], c.id))
return meta_data

def map_ratings(rating: str) -> str:
def map_ratings(rating: str) -> AgeRatings:
"""Map a rating string to a standardized format.
This function maps a given rating string to a standardized format ('Everyone', 'Teen', 'Mature 17+',
Expand All @@ -603,14 +594,17 @@ def map_ratings(rating: str) -> str:
"""
age_rating = rating.lower()
if age_rating in {"everyone", "cca"}:
return "Everyone"
return AgeRatings(metron_info="Everyone", comic_rack="Everyone")
if age_rating in {"teen", "teen plus"}:
return "Teen"
return "Mature 17+" if age_rating == "mature" else "Unknown"
return AgeRatings(metron_info="Teen", comic_rack="Teen")
if age_rating in {"mature"}:
return AgeRatings(metron_info="Mature", comic_rack="Mature 17+")
return AgeRatings(metron_info="Unknown", comic_rack="Unknown")

md = Metadata()
alt_info_source = [WebsiteInfo("Comic Vine", resp.cv_id)] if resp.cv_id else []
md.info_source = InfoSources(WebsiteInfo("Metron", resp.id), alt_info_source)

alt_info_source = [InfoSources("Comic Vine", resp.cv_id)] if resp.cv_id else []
md.info_source = [InfoSources("Metron", resp.id, True)] + alt_info_source # NOQA: RUF005
md.series = Series(
name=resp.series.name,
id_=resp.series.id,
Expand All @@ -628,7 +622,7 @@ def map_ratings(rating: str) -> str:
)
md.cover_date = resp.cover_date or None
md.comments = resp.desc
md.notes = create_notes(md.info_source.primary.id_)
md.notes = create_notes(resp.id)
md.modified = resp.modified
if resp.story_titles:
md.stories = [Basic(story) for story in resp.story_titles]
Expand All @@ -647,6 +641,6 @@ def map_ratings(rating: str) -> str:
if resp.rating:
md.age_rating = map_ratings(resp.rating.name)
if resp.resource_url:
md.web_link = URLS(str(resp.resource_url), [])
md.web_link = [Links(str(resp.resource_url), True)]

return md
42 changes: 38 additions & 4 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 = {path = "/home/bpepple/PycharmProjects/darkseid/dist/darkseid-5.0.5-py3-none-any.whl"}
darkseid = { path = "/home/bpepple/PycharmProjects/darkseid/dist/darkseid-5.0.7-py3-none-any.whl" }

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.6.2"
Expand Down
18 changes: 7 additions & 11 deletions tests/test_talker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest
from darkseid.comic import Comic, MetadataFormat
from darkseid.metadata import URLS, Basic, InfoSources, Metadata, Notes, Series, WebsiteInfo
from darkseid.metadata import AgeRatings, Basic, InfoSources, Links, Metadata, Notes, Series
from mokkari.schemas.base import BaseResource
from mokkari.schemas.generic import GenericItem
from mokkari.schemas.issue import BaseIssue, BasicSeries, Credit, Issue, IssueSeries
Expand Down Expand Up @@ -113,10 +113,10 @@ def test_map_resp_to_metadata(talker: Talker, test_issue: Issue) -> None:
assert md.credits[0].person == "Al Milgrom"
assert md.credits[0].role[0].name == "Cover"
assert md.reprints == create_reprint_list(test_issue.reprints)
assert md.age_rating == "Everyone"
assert md.web_link == URLS(
"https://metron.cloud/issue/the-spectacular-spider-man-1976-47/", []
)
assert md.age_rating == AgeRatings("Everyone", "Everyone")
assert md.web_link == [
Links("https://metron.cloud/issue/the-spectacular-spider-man-1976-47/", True)
]


def test_map_resp_to_metadata_with_no_story_name(
Expand Down Expand Up @@ -279,7 +279,7 @@ def test_retrieve_single_issue(
# Happy path tests
("metron", 123, [], (InfoSource.metron, 123)),
("comic vine", 456, [], (InfoSource.comic_vine, 456)),
("anilist", 789, [WebsiteInfo("metron", 321)], (InfoSource.metron, 321)),
("anilist", 789, [InfoSources("metron", 321)], (InfoSource.metron, 321)),
# Edge case
("anilist", 789, [], None),
],
Expand All @@ -292,11 +292,7 @@ def test_retrieve_single_issue(
)
def test_get_id_from_metron_info(primary_name, primary_id, alternatives, expected):
# Arrange
md = Metadata(
info_source=InfoSources(
primary=WebsiteInfo(primary_name, primary_id), alternatives=alternatives
)
)
md = Metadata(info_source=[InfoSources(primary_name, primary_id, True), *alternatives])

# Act
result = Talker._get_id_from_metron_info(md)
Expand Down

0 comments on commit 4a0d682

Please sign in to comment.