Handle most cases of cookie and image cache loading errors
This commit is contained in:
1 parent
5b8b6c7fdd
commit
2d1dfe007c
2 files changed
+23
-9
No files matched your search
@@ -3,6 +3,7 @@ import asyncio
|
|||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
import json
|
||||||
from typing import Dict, TypedDict, NewType, TYPE_CHECKING
|
from typing import Dict, TypedDict, NewType, TYPE_CHECKING
|
||||||
|
|
||||||
from utils import json_load, json_save
|
from utils import json_load, json_save
|
||||||
@@ -37,8 +38,15 @@ class ImageCache:
|
|||||||
def __init__(self, manager: GUIManager) -> None:
|
def __init__(self, manager: GUIManager) -> None:
|
||||||
self._root = manager._root
|
self._root = manager._root
|
||||||
self._twitch = manager._twitch
|
self._twitch = manager._twitch
|
||||||
|
cleanup: bool = False
|
||||||
CACHE_PATH.mkdir(parents=True, exist_ok=True)
|
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._images: dict[ImageHash, Image] = {}
|
||||||
self._photos: dict[tuple[ImageHash, ImageSize], PhotoImage] = {}
|
self._photos: dict[tuple[ImageHash, ImageSize], PhotoImage] = {}
|
||||||
self._lock = asyncio.Lock()
|
self._lock = asyncio.Lock()
|
||||||
@@ -60,12 +68,13 @@ class ImageCache:
|
|||||||
# hashes come with an extension already
|
# hashes come with an extension already
|
||||||
CACHE_PATH.joinpath(img_hash).unlink(missing_ok=True)
|
CACHE_PATH.joinpath(img_hash).unlink(missing_ok=True)
|
||||||
# NOTE: The hashes are deleted from self._hashes above
|
# NOTE: The hashes are deleted from self._hashes above
|
||||||
# NOTE: This cleanups the cache folder from unused PNG files
|
if cleanup:
|
||||||
# orphans = [
|
# This cleanups the cache folder from unused PNG files
|
||||||
# file.name for file in CACHE_PATH.glob("*.png") if file.name not in hash_counts
|
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)
|
for filename in orphans:
|
||||||
|
CACHE_PATH.joinpath(filename).unlink(missing_ok=True)
|
||||||
|
|
||||||
def save(self, *, force: bool = False) -> None:
|
def save(self, *, force: bool = False) -> None:
|
||||||
if self._altered or force:
|
if self._altered or force:
|
||||||
|
|||||||
@@ -657,8 +657,13 @@ class Twitch:
|
|||||||
chrome_agent = ClientType.WEB.USER_AGENT
|
chrome_agent = ClientType.WEB.USER_AGENT
|
||||||
# load in cookies
|
# load in cookies
|
||||||
cookie_jar = aiohttp.CookieJar()
|
cookie_jar = aiohttp.CookieJar()
|
||||||
if COOKIES_PATH.exists():
|
try:
|
||||||
cookie_jar.load(COOKIES_PATH)
|
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
|
# create session, limited to 50 connections at maximum
|
||||||
connector = aiohttp.TCPConnector(limit=50)
|
connector = aiohttp.TCPConnector(limit=50)
|
||||||
self._session = aiohttp.ClientSession(
|
self._session = aiohttp.ClientSession(
|
||||||
|
|||||||
Reference in new issue
Block a user