diff options
author | Keuin <[email protected]> | 2023-06-02 00:15:30 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-06-02 00:15:30 +0800 |
commit | ed540d68256f3f05e86b7880858249a1e8a17c42 (patch) | |
tree | e991424128a0a39578634f00f68e8e3bf5dd4f8b /htmlcache.py | |
parent | 77c5ef83402aa28fc6d3a9d2982ccb03fea70d2f (diff) |
Cache HTML source on disk
Diffstat (limited to 'htmlcache.py')
-rw-r--r-- | htmlcache.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/htmlcache.py b/htmlcache.py new file mode 100644 index 0000000..2f727a7 --- /dev/null +++ b/htmlcache.py @@ -0,0 +1,33 @@ +import os + +import aiohttp + +import main + + +class TexSourceGenerationError(Exception): + pass + + +class HtmlCache: + + def __init__(self, cache_path: str): + self._cache_path = os.path.abspath(cache_path) + + 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() + else: + async with aiohttp.ClientSession() as ses: + async with ses.get(f'https://utaten.com/lyric/{item_id}/') as r: + if not r.ok: + 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) + except IOError as e: + print(f'Failed to update cache for song `{item_id}`: {e}') + return main.html_to_tex(html) |