From cee2b9301f3d30e186596a125fe2ab8419b3f222 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 30 Sep 2021 11:50:06 +0300 Subject: [PATCH] Fix cases where xkcd image URL returns non-200 status soon after release --- xkcd.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/xkcd.py b/xkcd.py index d109c47..9ec24a9 100644 --- a/xkcd.py +++ b/xkcd.py @@ -192,7 +192,7 @@ def get_latest_xkcd(self) -> Awaitable[XKCDInfo]: def get_xkcd(self, num: int) -> Awaitable[XKCDInfo]: return self._get_xkcd_info(f"https://xkcd.com/{num}/info.0.json") - async def _get_media_info(self, image_url: str) -> MediaCache: + async def _get_media_info(self, image_url: str) -> Optional[MediaCache]: cache = self.media_cache.query.get(image_url) if cache is not None: return cache @@ -213,6 +213,10 @@ async def _get_media_info(self, image_url: str) -> MediaCache: self.db.add(cache) self.db.commit() return cache + else: + self.log.error(f"Getting media info for {image_url} returned {resp.status}: " + f"{await resp.text()}") + return None async def send_xkcd(self, room_id: RoomID, xkcd: XKCDInfo) -> None: try: @@ -222,6 +226,8 @@ async def send_xkcd(self, room_id: RoomID, xkcd: XKCDInfo) -> None: async def _send_xkcd(self, room_id: RoomID, xkcd: XKCDInfo) -> None: info = await self._get_media_info(xkcd.img) + if not info: + return if self.config["inline"]: content = TextMessageEventContent( msgtype=MessageType.TEXT, format=Format.HTML, @@ -283,8 +289,10 @@ async def _poll_xkcd(self) -> None: except Exception: self.log.exception("Failed to get latest xkcd") if latest.num > self.latest_id: - self.latest_id = latest.num - await self.broadcast(latest) + info = await self._get_media_info(latest.img) + if info: + self.latest_id = latest.num + await self.broadcast(latest) await asyncio.sleep(self.config["poll_interval"], loop=self.loop) @command.new(name=lambda self: self.config["base_command"],