diff options
| author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-12-28 01:18:54 +0100 | 
|---|---|---|
| committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-12-28 01:18:54 +0100 | 
| commit | 9017f48371ecdb1408f793b0771d5f5f0fe04acf (patch) | |
| tree | 3ac7ea2171783021fbaa70c744acc58e7e29f46d | |
| parent | 6f158c70803c3486f263f67619166c697832df72 (diff) | |
| download | jnfilter-9017f48371ecdb1408f793b0771d5f5f0fe04acf.tar.gz jnfilter-9017f48371ecdb1408f793b0771d5f5f0fe04acf.tar.bz2 jnfilter-9017f48371ecdb1408f793b0771d5f5f0fe04acf.zip | |
feat: Move to flask
In my current setup flask is easier to deploy.
FastAPI has not yet hit alpine repositories, and I don't want to
maintain it myself.
Also flask should be more than enough to handle those requests.
| -rw-r--r-- | jnfilter/__init__.py | 43 | ||||
| -rw-r--r-- | setup.py | 3 | 
2 files changed, 19 insertions, 27 deletions
| diff --git a/jnfilter/__init__.py b/jnfilter/__init__.py index 2c7641a..35be6bd 100644 --- a/jnfilter/__init__.py +++ b/jnfilter/__init__.py @@ -1,15 +1,13 @@  import re  import httpx -import uvicorn  from functools import reduce  from typing import List, Iterator, Union  from xml.etree.ElementTree import ElementTree, fromstring, tostring, register_namespace -from fastapi import FastAPI -from starlette.responses import Response, PlainTextResponse +from flask import Flask, Response, request -app = FastAPI() +app =  Flask(__name__)  URL = "https://jovemnerd.com.br/feed-nerdcast/" @@ -33,9 +31,6 @@ register_namespace("itunes", ITUNES)  register_namespace("atom", ATOM) -class XMLResponse(Response): -    media_type = "application/xml" -  def match(title: str, series: List[str]) -> bool:      def _match(s): @@ -74,36 +69,34 @@ def filter_titles_xml(xml_str) -> Iterator[str]:          yield item.find("title").text -async def load_and_filter(series: str, tag: Union[bool, None] = False) -> str: +def load_and_filter(series: str, tag: Union[bool, None] = False) -> str:      series = series or 'nerdcast'      series = series.split(',') -    async with httpx.AsyncClient() as client: -        response = await client.get(URL) +    with httpx.Client() as client: +        response =client.get(URL)          xml_str = response.content          return filter_xml(xml_str, series, tag) -async def load_titles() -> Iterator[str]: -    async with httpx.AsyncClient() as client: -        response = await client.get(URL) +def load_titles() -> Iterator[str]: +    with httpx.Client() as client: +        response = client.get(URL)          xml_str = response.content          return filter_titles_xml(xml_str) -@app.head("/") -@app.get("/", response_class=XMLResponse) -async def root(q: str = '', tag: Union[bool, None] = False): -    return await load_and_filter(q, tag) +@app.route("/", methods=['GET', 'HEAD']) +def root(q: str = '', tag: Union[bool, None] = False): +    q = request.args.get("q", "") +    tag = request.args.get("tag", False) +    return load_and_filter(q, tag), 200,  {'Content-Type': 'application/xml'} -@app.get("/titles", response_class=PlainTextResponse) -async def titles(): -    titles = await load_titles() +@app.route("/titles", methods=['GET']) +def titles(): +    titles = load_titles()      return "\n".join(titles) -@app.get("/series") -async def titles(): +@app.route("/series", methods=['GET']) +def series():      return [i[0] for i in RegexCollection.items()] - -def run(): -    uvicorn.run(app=app, host="0.0.0.0", port=32000) @@ -2,8 +2,7 @@ from setuptools import setup  requirements = [      'httpx==0.21.1', -    'fastapi==0.70.0', -    'uvicorn==0.15.0' +    'flask==2.2.2',  ] | 
