diff options
author | Keuin <[email protected]> | 2023-06-03 23:26:11 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-06-03 23:26:11 +0800 |
commit | 18af87c1e3048e96ca66d64c475e832c858316b4 (patch) | |
tree | 60b7d9fea7506b7f571bd43ca0a9805e14ba15d2 | |
parent | 35b5f627a86c43389b928e0d03e6642082bf5a61 (diff) |
htmlcache: use async file IO
-rw-r--r-- | htmlcache.py | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/htmlcache.py b/htmlcache.py index 2f727a7..922ab09 100644 --- a/htmlcache.py +++ b/htmlcache.py @@ -1,6 +1,7 @@ import os import aiohttp +from aiofile import async_open import main @@ -17,8 +18,8 @@ class HtmlCache: async def get_utaten_tex_source(self, item_id: str) -> str: cache_file_path = os.path.join(self._cache_path, f'{item_id}.html') if os.path.isfile(cache_file_path): - with open(cache_file_path, 'r', encoding='utf-8') as f: - html = f.read() + async with async_open(cache_file_path, 'r', encoding='utf-8') as f: + html = await f.read() else: async with aiohttp.ClientSession() as ses: async with ses.get(f'https://utaten.com/lyric/{item_id}/') as r: @@ -26,8 +27,8 @@ class HtmlCache: raise TexSourceGenerationError('HTTP request failed when reading page source') html = await r.text() try: - with open(cache_file_path, 'w', encoding='utf-8') as f: - f.write(html) + async with async_open(cache_file_path, 'w', encoding='utf-8') as f: + await f.write(html) except IOError as e: print(f'Failed to update cache for song `{item_id}`: {e}') return main.html_to_tex(html) |