summaryrefslogtreecommitdiff
path: root/web.py
blob: 7aadac4a300266e6ecd084a062a296ce06aa015b (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
import re

import aiohttp
from fastapi import FastAPI, Response

import main

app = FastAPI()

utaten_pattern = re.compile(r'[a-z0-9]+')


@app.get("/utaten/{item_id}/pdf")
async def get_utaten_lyric_pdf(item_id: str):
    raise NotImplementedError


@app.get("/utaten/{item_id}.tex")
async def get_utaten_lyric_pdf(item_id: str, resp: Response):
    async with aiohttp.ClientSession() as ses:
        async with ses.get(f'https://utaten.com/lyric/{item_id}/') as r:
            if not r.ok:
                resp.status_code = 503
                return
            html = await r.text()
            tex = main.html_to_tex(html)
            return Response(content=tex, media_type='application/x-tex')