Skip to content

Commit

Permalink
Merge pull request #15 from PTST/ruddersdal-fix
Browse files Browse the repository at this point in the history
Handle errors on finding image urls
  • Loading branch information
PTST authored Nov 6, 2024
2 parents 99313dd + 0101365 commit 5b64c65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions custom_components/danish_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
COMMON_LOGIN_BASE_URL,
COMMON_LOGIN_HEADERS,
COVER_BASE_URL,
DEFAULT_IMAGE_URL,
FBS_OPEN_PLATFORM_BASE_URL,
INFO_BASE_URL,
INFO_GRAPG_QL_QUERY,
Expand Down Expand Up @@ -314,10 +315,11 @@ async def get_ereolen_info(

@reauth_on_fail
async def get_image_cover(self, identifier: str, id_type: str):

params = {
"type": id_type,
"identifiers": identifier,
"sizes": "small",
"sizes": "small,medium,large",
}
image_headers = {"Authorization": self.library_bearer_token}
image_response = await self.session.get(
Expand All @@ -328,8 +330,22 @@ async def get_image_cover(self, identifier: str, id_type: str):
timeout=None,
)
image_response.raise_for_status()
image_json = image_response.json()[0]
return image_json["imageUrls"]["small"]["url"]
results = image_response.json()
if len(results) == 0:
LOGGER.debug("No image results found for title")
LOGGER.debug(image_response.request.__dict__)
return DEFAULT_IMAGE_URL
image_json = results[0]
image_urls = image_json["imageUrls"]
if "small" in image_urls.keys() and "url" in image_urls["small"].keys():
return image_json["imageUrls"]["small"]["url"]
if "medium" in image_urls.keys() and "url" in image_urls["medium"].keys():
return image_json["imageUrls"]["medium"]["url"]
if "large" in image_urls.keys() and "url" in image_urls["large"].keys():
return image_json["imageUrls"]["large"]["url"]
LOGGER.debug("No images returned for title")
LOGGER.debug(image_response.request.__dict__)
return DEFAULT_IMAGE_URL

async def unpack_results(self, tasks):
if len(tasks) == 0:
Expand Down
1 change: 1 addition & 0 deletions custom_components/danish_libraries/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@

LOGGER = logging.getLogger(__package__)
PUBHUB_BASE_URL = "https://pubhub-openplatform.dbc.dk"
DEFAULT_IMAGE_URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/No-Image-Placeholder.svg/128px-No-Image-Placeholder.svg.png"

0 comments on commit 5b64c65

Please sign in to comment.