summaryrefslogtreecommitdiff
path: root/htmlcache.py
blob: 2dc29b4dfdd88f2d3a8579c1d7d4500321781993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os

import aiohttp
from aiofile import async_open

import main


class TexSourceGenerationError(Exception):
    pass


class HtmlCache:

    def __init__(self, cache_path: str):
        self._cache_path = os.path.abspath(cache_path)
        try:
            os.mkdir(self._cache_path)
        except FileExistsError:
            pass

    async def get_utaten_tex_source(self, item_id: str) -> main.LyricInfo:
        cache_file_path = os.path.join(self._cache_path, f'{item_id}.html')
        if os.path.isfile(cache_file_path):
            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:
                    if not r.ok:
                        raise TexSourceGenerationError('HTTP request failed when reading page source')
                    html = await r.text()
                    try:
                        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_extract_lyric_info(html)