diff --git a/cache.py b/cache.py index 1082945..be44178 100644 --- a/cache.py +++ b/cache.py @@ -3,6 +3,7 @@ import asyncio from datetime import datetime, timedelta, timezone import io +import json from typing import Dict, TypedDict, NewType, TYPE_CHECKING from utils import json_load, json_save @@ -37,8 +38,15 @@ class ImageCache: def __init__(self, manager: GUIManager) -> None: self._root = manager._root self._twitch = manager._twitch + cleanup: bool = False CACHE_PATH.mkdir(parents=True, exist_ok=True) - self._hashes: Hashes = json_load(CACHE_DB, default_database, merge=False) + try: + self._hashes: Hashes = json_load(CACHE_DB, default_database, merge=False) + except json.JSONDecodeError: + # if we can't load the mapping file, delete all existing files, + # then reinitialize the image cache anew + cleanup = True + self._hashes = default_database.copy() self._images: dict[ImageHash, Image] = {} self._photos: dict[tuple[ImageHash, ImageSize], PhotoImage] = {} self._lock = asyncio.Lock() @@ -60,12 +68,13 @@ class ImageCache: # hashes come with an extension already CACHE_PATH.joinpath(img_hash).unlink(missing_ok=True) # NOTE: The hashes are deleted from self._hashes above - # NOTE: This cleanups the cache folder from unused PNG files - # orphans = [ - # file.name for file in CACHE_PATH.glob("*.png") if file.name not in hash_counts - # ] - # for filename in orphans: - # CACHE_PATH.joinpath(filename).unlink(missing_ok=True) + if cleanup: + # This cleanups the cache folder from unused PNG files + orphans = [ + file.name for file in CACHE_PATH.glob("*.png") if file.name not in hash_counts + ] + for filename in orphans: + CACHE_PATH.joinpath(filename).unlink(missing_ok=True) def save(self, *, force: bool = False) -> None: if self._altered or force: diff --git a/twitch.py b/twitch.py index 5303e4a..36abc5a 100644 --- a/twitch.py +++ b/twitch.py @@ -657,8 +657,13 @@ class Twitch: chrome_agent = ClientType.WEB.USER_AGENT # load in cookies cookie_jar = aiohttp.CookieJar() - if COOKIES_PATH.exists(): - cookie_jar.load(COOKIES_PATH) + try: + if COOKIES_PATH.exists(): + cookie_jar.load(COOKIES_PATH) + except Exception: + # if loading in the cookies file ends up in an error, just ignore it + # clear the jar, just in case + cookie_jar.clear() # create session, limited to 50 connections at maximum connector = aiohttp.TCPConnector(limit=50) self._session = aiohttp.ClientSession(