diff options
| author | Pinapelz <yukais@pinapelz.com> | 2025-09-17 14:58:54 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2025-09-17 15:03:55 -0700 |
| commit | 668ca32331d2c8d4487c36b00bd3419e6bea89c4 (patch) | |
| tree | 5a5c293978c250e56cb15db543fdb06f93dfea5c | |
| parent | bd650f563a07b7d0d5941ca080b5a9247aaa01ab (diff) | |
create new cache file if tl_cache becomes corrupted
| -rw-r--r-- | constants.py | 2 | ||||
| -rw-r--r-- | generate.py | 2 | ||||
| -rw-r--r-- | translate.py | 43 |
3 files changed, 30 insertions, 17 deletions
diff --git a/constants.py b/constants.py index c252eab..5c510d2 100644 --- a/constants.py +++ b/constants.py @@ -3,7 +3,7 @@ from enum import Enum DAYS_LIMIT=14 SOUND_VOLTEX_EXCEED_GEAR_NEWS_SITE ="https://p.eagate.573.jp/game/sdvx/vi/news/index.html" -IIDX_PINKY_CRUSH_NEWS_SITE="https://p.eagate.573.jp/game/2dx/32/info/index.html" +IIDX_PINKY_CRUSH_NEWS_SITE="https://p.eagate.573.jp/game/2dx/32/info/index.html" # legacy should not be used, eamuse feed is more verbose DDR_WORLD_NEWS_SITE="https://p.eagate.573.jp/game/ddr/ddrworld/info/index.html" POLARIS_CHORD_NEWS_SITE="https://p.eagate.573.jp/game/polarischord/pc/news/index.html" diff --git a/generate.py b/generate.py index 900777d..967c214 100644 --- a/generate.py +++ b/generate.py @@ -74,7 +74,7 @@ def generate_news_file(filename, url, version=None): def generate_iidx_news_file(eamuse_feed: bool=False): if eamuse_feed: return generate_news_file("iidx_news", constants.EAMUSE_APP_FEED, constants.IIDX_EAMUSE_APP_ID) - else: + else: # legacy should not be used, use eamuse app feed above return generate_news_file("iidx_news", constants.IIDX_PINKY_CRUSH_NEWS_SITE) def generate_sdvx_news_file(): diff --git a/translate.py b/translate.py index c3783a0..877872a 100644 --- a/translate.py +++ b/translate.py @@ -36,16 +36,23 @@ def _decode_links(raw_text: str, links: list) -> str: raw_text = raw_text.replace(link[0], link[1]) return raw_text -def _load_translation_cache() -> list: +def _load_translation_cache() -> dict: cache_file = "tl_cache.json" tl_map = {} if os.path.exists(cache_file): - with open(cache_file, "r", encoding="utf-8") as file: - entries = json.load(file) - for entry in entries: - key = hashlib.sha256((entry["source_lang"] + entry["target_lang"] + entry["source_txt"]).encode('utf-8')).hexdigest() - tl_map[key] = entry["result_txt"] - return tl_map + try: + with open(cache_file, "r", encoding="utf-8") as file: + entries = json.load(file) + for entry in entries: + key = hashlib.sha256((entry["source_lang"] + entry["target_lang"] + entry["source_txt"]).encode('utf-8')).hexdigest() + tl_map[key] = entry["result_txt"] + return tl_map + except (UnicodeDecodeError, json.JSONDecodeError, KeyError) as e: + print(f"Translation cache corrupted ({e}), deleting and starting fresh...") + os.remove(cache_file) + with open(cache_file, "w", encoding="utf-8") as file: + json.dump([], file, ensure_ascii=False, indent=4) + return {} else: with open(cache_file, "w", encoding="utf-8") as file: json.dump([], file, ensure_ascii=False, indent=4) @@ -59,14 +66,20 @@ def _add_to_translation_cache(source_lang: str, target_lang: str, source_txt: st "source_txt": source_txt, "result_txt": result_txt } - if os.path.exists(cache_file): - with open(cache_file, "r", encoding="utf-8") as file: - cache = json.load(file) - else: - cache = [] - cache.append(cache_entry) - with open(cache_file, "w", encoding="utf-8") as file: - json.dump(cache, file, ensure_ascii=False, indent=4) + try: + if os.path.exists(cache_file): + with open(cache_file, "r", encoding="utf-8") as file: + cache = json.load(file) + else: + cache = [] + cache.append(cache_entry) + with open(cache_file, "w", encoding="utf-8") as file: + json.dump(cache, file, ensure_ascii=False, indent=4) + except (UnicodeDecodeError, json.JSONDecodeError) as e: + print(f"Translation cache corrupted during write ({e}), starting fresh...") + cache = [cache_entry] + with open(cache_file, "w", encoding="utf-8") as file: + json.dump(cache, file, ensure_ascii=False, indent=4) def request_google_translate(text: str, source: str="ja", target="en", translation_cache=None) -> tuple: """ |
