From 9017f48371ecdb1408f793b0771d5f5f0fe04acf Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Wed, 28 Dec 2022 01:18:54 +0100 Subject: 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. --- jnfilter/__init__.py | 43 ++++++++++++++++++------------------------- 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) diff --git a/setup.py b/setup.py index 07add82..7692d27 100644 --- a/setup.py +++ b/setup.py @@ -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', ] -- cgit v1.2.3