demo-test / app.py
mugiwarafx
remove logging
7e6d5ed
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from bs4 import BeautifulSoup
from typing import List, Dict
app = FastAPI()
all_html_tags = {
"a", "abbr", "address", "area", "article", "aside", "audio", "b", "base", "bdi", "bdo", "blockquote", "body",
"br", "button", "canvas", "caption", "cite", "code", "col", "colgroup", "data", "datalist", "dd", "del", "details",
"dfn", "dialog", "div", "dl", "dt", "em", "embed", "fieldset", "figcaption", "figure", "footer", "form", "h1", "h2",
"h3", "h4", "h5", "h6", "head", "header", "hr", "html", "i", "iframe", "img", "input", "ins", "kbd", "label", "legend",
"li", "link", "main", "map", "mark", "meta", "meter", "nav", "noscript", "object", "ol", "optgroup", "option", "output",
"p", "param", "picture", "pre", "progress", "q", "rp", "rt", "ruby", "s", "samp", "script", "section", "select", "small",
"source", "span", "strong", "style", "sub", "summary", "sup", "table", "tbody", "td", "template", "textarea", "tfoot",
"th", "thead", "time", "title", "tr", "track", "u", "ul", "var", "video", "wbr"
}
class HTMLInput(BaseModel):
html_code: str
class HTMLOutput(BaseModel):
tags_used: List[str]
tags_not_used: List[str]
def extract_html_tags(html_code: str) -> Dict[str, List[str]]:
soup = BeautifulSoup(html_code, "html.parser")
tags_used = {tag.name for tag in soup.find_all()}
tags_not_used = all_html_tags - tags_used
return {
"tags_used": list(tags_used),
"tags_not_used": list(tags_not_used)
}
@app.post("/extract_tags", response_model=HTMLOutput)
async def extract_tags(input: HTMLInput):
try:
result = extract_html_tags(input.html_code)
return HTMLOutput(**result)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/extract_tags_raw", response_model=HTMLOutput)
async def extract_tags_raw(request: Request):
try:
html_code = await request.body()
html_code = html_code.decode("utf-8")
result = extract_html_tags(html_code)
return HTMLOutput(**result)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def root():
return {"message": "FastAPI application is running"}