File size: 2,314 Bytes
7e6d5ed
51cb5f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acde423
7e6d5ed
 
 
 
 
 
 
 
 
 
acde423
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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"}