From c0880c9afed0aa595df22b39ac47320dc3ca7647 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Wed, 13 Jul 2022 23:11:14 -0700 Subject: [PATCH] Account for aliases field from CV --- comictaggerlib/comiccacher.py | 21 ++++++++++++++++----- comictaggerlib/comicvinetalker.py | 18 ++++++++++++++---- comictaggerlib/resulttypes.py | 2 ++ testing/comicdata.py | 2 ++ testing/comicvine.py | 1 + tests/comicvinetalker_test.py | 2 +- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/comictaggerlib/comiccacher.py b/comictaggerlib/comiccacher.py index 3eb1c91..2c70834 100644 --- a/comictaggerlib/comiccacher.py +++ b/comictaggerlib/comiccacher.py @@ -84,7 +84,8 @@ class ComicCacher: + "image_url TEXT," + "description TEXT," + "timestamp DATE DEFAULT (datetime('now','localtime'))," - + "source_name TEXT NOT NULL)" + + "source_name TEXT NOT NULL," + + "aliases TEXT)" # Newline separated ) cur.execute( @@ -96,6 +97,7 @@ class ComicCacher: + "start_year INT," + "timestamp DATE DEFAULT (datetime('now','localtime')), " + "source_name TEXT NOT NULL," + + "aliases TEXT," # Newline separated + "PRIMARY KEY (id, source_name))" ) @@ -105,6 +107,7 @@ class ComicCacher: + "url_list TEXT," + "timestamp DATE DEFAULT (datetime('now','localtime')), " + "source_name TEXT NOT NULL," + + "aliases TEXT," # Newline separated + "PRIMARY KEY (issue_id, source_name))" ) @@ -121,6 +124,7 @@ class ComicCacher: + "description TEXT," + "timestamp DATE DEFAULT (datetime('now','localtime')), " + "source_name TEXT NOT NULL," + + "aliases TEXT," # Newline separated + "PRIMARY KEY (id, source_name))" ) @@ -153,8 +157,8 @@ class ComicCacher: cur.execute( "INSERT INTO VolumeSearchCache " - + "(source_name, search_term, id, name, start_year, publisher, count_of_issues, image_url, description) " - + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", + + "(source_name, search_term, id, name, start_year, publisher, count_of_issues, image_url, description, aliases) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ( source_name, search_term.casefold(), @@ -165,6 +169,7 @@ class ComicCacher: record["count_of_issues"], url, record["description"], + record["aliases"], ), ) @@ -197,6 +202,7 @@ class ComicCacher: "description": record[7], "publisher": {"name": record[4]}, "image": {"super_url": record[6]}, + "aliases": record[10], } ) @@ -268,6 +274,7 @@ class ComicCacher: "count_of_issues": cv_volume_record["count_of_issues"], "start_year": cv_volume_record["start_year"], "timestamp": timestamp, + "aliases": cv_volume_record["aliases"], } self.upsert(cur, "volumes", data) @@ -295,6 +302,7 @@ class ComicCacher: "thumb_url": issue["image"]["thumb_url"], "description": issue["description"], "timestamp": timestamp, + "aliases": issue["aliases"], } self.upsert(cur, "issues", data) @@ -313,7 +321,8 @@ class ComicCacher: # fetch cur.execute( - "SELECT source_name,id,name,publisher,count_of_issues,start_year FROM Volumes WHERE id=? AND source_name=?", + "SELECT source_name,id,name,publisher,count_of_issues,start_year,aliases FROM Volumes" + " WHERE id=? AND source_name=?", [volume_id, source_name], ) @@ -330,6 +339,7 @@ class ComicCacher: "count_of_issues": row[4], "start_year": row[5], "publisher": {"name": row[3]}, + "aliases": row[6], } ) @@ -352,7 +362,7 @@ class ComicCacher: cur.execute( ( - "SELECT source_name,id,name,issue_number,site_detail_url,cover_date,super_url,thumb_url,description" + "SELECT source_name,id,name,issue_number,site_detail_url,cover_date,super_url,thumb_url,description,aliases" " FROM Issues WHERE volume_id=? AND source_name=?" ), [volume_id, source_name], @@ -370,6 +380,7 @@ class ComicCacher: "cover_date": row[5], "image": {"super_url": row[6], "thumb_url": row[7]}, "description": row[8], + "aliases": row[9], } ) diff --git a/comictaggerlib/comicvinetalker.py b/comictaggerlib/comicvinetalker.py index 089edb4..4db18ae 100644 --- a/comictaggerlib/comicvinetalker.py +++ b/comictaggerlib/comicvinetalker.py @@ -298,7 +298,17 @@ class ComicVineTalker: if not literal: # Remove any search results that don't contain all the search terms (iterate backwards for easy removal) for record in reversed(search_results): - if not utils.titles_match(search_series_name, record["name"]): + matched = False + aliases = [] + if record["aliases"]: + aliases = record["aliases"].split("\n") + aliases.append(record["name"]) + + for name in aliases: + if utils.titles_match(search_series_name, name): + matched = True + break + if not matched: search_results.remove(record) # Cache these search results, even if it's literal we cache the results @@ -321,7 +331,7 @@ class ComicVineTalker: params = { "api_key": self.api_key, "format": "json", - "field_list": "name,id,start_year,publisher,count_of_issues", + "field_list": "name,id,start_year,publisher,count_of_issues,aliases", } cv_response = self.get_cv_content(volume_url, params) @@ -344,7 +354,7 @@ class ComicVineTalker: "api_key": self.api_key, "filter": "volume:" + str(series_id), "format": "json", - "field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description", + "field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description,aliases", "offset": 0, } cv_response = self.get_cv_content(self.api_base_url + "/issues/", params) @@ -388,7 +398,7 @@ class ComicVineTalker: params: dict[str, str | int] = { "api_key": self.api_key, "format": "json", - "field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description", + "field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description,aliases", "filter": flt, } diff --git a/comictaggerlib/resulttypes.py b/comictaggerlib/resulttypes.py index 31062d4..0f53ae8 100644 --- a/comictaggerlib/resulttypes.py +++ b/comictaggerlib/resulttypes.py @@ -93,6 +93,7 @@ class CVIssuesResults(TypedDict): name: str site_detail_url: str volume: NotRequired[CVVolume] + aliases: str class CVPublisher(TypedDict, total=False): @@ -110,6 +111,7 @@ class CVVolumeResults(TypedDict): publisher: CVPublisher start_year: str resource_type: NotRequired[str] + aliases: NotRequired[str | None] class CVCredits(TypedDict): diff --git a/testing/comicdata.py b/testing/comicdata.py index 5033dfa..b4f20b6 100644 --- a/testing/comicdata.py +++ b/testing/comicdata.py @@ -15,6 +15,7 @@ search_results = [ name="test", publisher=comictaggerlib.resulttypes.CVPublisher(name="test"), start_year="", # This is currently submitted as a string and returned as an int + aliases=None, ), comictaggerlib.resulttypes.CVVolumeResults( count_of_issues=1, @@ -24,6 +25,7 @@ search_results = [ name="test 2", publisher=comictaggerlib.resulttypes.CVPublisher(name="test"), start_year="", # This is currently submitted as a string and returned as an int + aliases=None, ), ] diff --git a/testing/comicvine.py b/testing/comicvine.py index 32bf460..419bda4 100644 --- a/testing/comicvine.py +++ b/testing/comicvine.py @@ -98,6 +98,7 @@ cv_volume_result: dict[str, Any] = { "count_of_issues": 6, "id": 23437, "name": "Cory Doctorow's Futuristic Tales of the Here and Now", + "aliases": None, "publisher": { "api_detail_url": "https://comicvine.gamespot.com/api/publisher/4010-1190/", "id": 1190, diff --git a/tests/comicvinetalker_test.py b/tests/comicvinetalker_test.py index 7b8f35f..b6e19c2 100644 --- a/tests/comicvinetalker_test.py +++ b/tests/comicvinetalker_test.py @@ -39,7 +39,7 @@ def test_fetch_issues_by_volume_issue_num_and_year(comicvine_api): cv_expected = testing.comicvine.cv_issue_result["results"].copy() testing.comicvine.filter_field_list( cv_expected, - {"params": {"field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description"}}, + {"params": {"field_list": "id,volume,issue_number,name,image,cover_date,site_detail_url,description,aliases"}}, ) assert cv[0] == cv_expected