Add ImageHash support for alternate_urls

This commit is contained in:
Mizaki 2025-02-12 23:15:19 +00:00 committed by Timmy Welch
parent c3f5badc7d
commit d2499f6bae
3 changed files with 25 additions and 15 deletions

View File

@ -211,7 +211,7 @@ class GenericMetadata:
# urls to cover image, not generally part of the metadata
_cover_image: str | ImageHash | None = None
_alternate_images: list[str] = dataclasses.field(default_factory=list)
_alternate_images: list[str | ImageHash] = dataclasses.field(default_factory=list)
def __post_init__(self) -> None:
for key, value in self.__dict__.items():

View File

@ -307,7 +307,7 @@ class IssueIdentifier:
def _get_issue_cover_match_score(
self,
primary_img_url: str | ImageHash,
alt_urls: list[str],
alt_urls: list[str | ImageHash],
local_hashes: list[tuple[str, int]],
use_alt_urls: bool = False,
) -> Score:
@ -316,17 +316,23 @@ class IssueIdentifier:
# If there is no URL return 100
if not primary_img_url:
return Score(score=100, url="", remote_hash=0)
return Score(score=100, url="", remote_hash=0, local_hash=0, local_hash_name="0")
self._user_canceled()
remote_hashes = []
# If the cover is ImageHash and the alternate covers are URLs, the alts will not be hashed/checked currently
if isinstance(primary_img_url, ImageHash):
remote_hashes = [("0", primary_img_url.Hash)]
# ImageHash doesn't have a url so we just give it an empty string
remote_hashes.append(("", primary_img_url.Hash))
if use_alt_urls and alt_urls:
remote_hashes.extend(("", alt_hash.Hash) for alt_hash in alt_urls if isinstance(alt_hash, ImageHash))
else:
urls = [primary_img_url]
if use_alt_urls:
urls.extend(alt_urls)
self.log_msg(f"[{len(alt_urls)} alt. covers]")
only_urls = [url for url in alt_urls if isinstance(url, str)]
urls.extend(only_urls)
self.log_msg(f"[{len(only_urls)} alt. covers]")
remote_hashes = self._get_remote_hashes(urls)
@ -519,10 +525,14 @@ class IssueIdentifier:
)
try:
image_url = issue._cover_image or ""
alt_urls = issue._alternate_images
image_url = issue._cover_image if isinstance(issue._cover_image, str) else ""
# We only include urls in the IssueResult so we don't have to deal with it down the line
# TODO: display the hash to the user so they know a direct hash was used instead of downloading an image
alt_urls: list[str] = [url for url in issue._alternate_images if isinstance(url, str)]
score_item = self._get_issue_cover_match_score(image_url, alt_urls, hashes, use_alt_urls=use_alternates)
score_item = self._get_issue_cover_match_score(
image_url, issue._alternate_images, hashes, use_alt_urls=use_alternates
)
except Exception:
logger.exception(f"Scoring series{alternate} covers failed")
return []
@ -539,7 +549,7 @@ class IssueIdentifier:
month=issue.month,
year=issue.year,
publisher=None,
image_url=str(image_url),
image_url=image_url,
alt_image_urls=alt_urls,
description=issue.description or "",
)
@ -629,7 +639,7 @@ class IssueIdentifier:
if issue._cover_image.Kind == "phash":
self.image_hasher = 3
break
elif issue._cover_image == "ahash":
elif issue._cover_image.Kind == "ahash":
self.image_hasher = 1 # Set to 1 on init but might as well be sure
break

View File

@ -221,10 +221,10 @@ class IssueSelectionWindow(QtWidgets.QDialog):
QtWidgets.QApplication.restoreOverrideCursor()
self.issue_number = issue.issue or ""
cover_image = ""
if isinstance(issue._cover_image, str):
cover_image = issue._cover_image
self.coverWidget.set_issue_details(self.issue_id, [cover_image, *issue._alternate_images])
# We don't currently have a way to display hashes to the user
# TODO: display the hash to the user so they know it will be used for cover matching
alt_images = [url for url in issue._alternate_images if isinstance(url, str)]
self.coverWidget.set_issue_details(self.issue_id, [str(issue._cover_image) or "", *alt_images])
if issue.description is None:
self.set_description(self.teDescription, "")
else: