|
@@ -23,13 +23,13 @@ ROOT_PATH = Path(__file__).parent
|
|
|
app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None)
|
|
|
app.mount("/static", StaticFiles(directory=ROOT_PATH / "static"), name="static")
|
|
|
templates = Jinja2Templates(directory=ROOT_PATH / "templates")
|
|
|
-session: aiohttp.ClientSession | None = None
|
|
|
+session: aiohttp.ClientSession = None
|
|
|
|
|
|
|
|
|
def remove_classes(node: Node) -> Node:
|
|
|
"""Recursively remove all classes from all nodes."""
|
|
|
if "class" in node.attributes:
|
|
|
- del node.attrs["class"]
|
|
|
+ del node.attrs["class"]
|
|
|
for child in node.iter():
|
|
|
remove_classes(child)
|
|
|
return node
|
|
@@ -45,7 +45,7 @@ async def catch_all(response: Request):
|
|
|
|
|
|
async with session.get(url) as dict_response:
|
|
|
if dict_response.history:
|
|
|
- return RedirectResponse(dict_response.url.relative(), status_code=301)
|
|
|
+ return RedirectResponse(str(dict_response.url.relative()), status_code=301)
|
|
|
html = await dict_response.text()
|
|
|
|
|
|
results = []
|
|
@@ -54,7 +54,7 @@ async def catch_all(response: Request):
|
|
|
try:
|
|
|
thumbs_api_url = (
|
|
|
f'https://api.urbandictionary.com/v0/uncacheable?ids='
|
|
|
- f'{",".join(d.attributes["data-defid"] for d in definitions)}'
|
|
|
+ f'{",".join(d.attributes["data-defid"] or "-1" for d in definitions)}'
|
|
|
)
|
|
|
async with session.get(thumbs_api_url) as thumbs_response:
|
|
|
thumbs_json = await thumbs_response.json()
|
|
@@ -67,7 +67,7 @@ async def catch_all(response: Request):
|
|
|
meaning = remove_classes(definition.css_first("div.meaning")).html
|
|
|
example = remove_classes(definition.css_first("div.example")).html
|
|
|
contributor = remove_classes(definition.css_first("div.contributor")).html
|
|
|
- definition_id = int(definition.attributes["data-defid"])
|
|
|
+ definition_id = int(definition.attributes["data-defid"] or "-1")
|
|
|
definition_thumbs = thumbs_data.get(definition_id, {})
|
|
|
thumbs_up = definition_thumbs.get("up")
|
|
|
thumbs_down = definition_thumbs.get("down")
|
|
@@ -76,7 +76,7 @@ async def catch_all(response: Request):
|
|
|
)
|
|
|
if (pagination := parser.css_first("div.pagination")) is not None:
|
|
|
pagination = remove_classes(pagination)
|
|
|
- pagination.attrs["class"] = "pagination"
|
|
|
+ pagination.attrs["class"] = "pagination"
|
|
|
pagination = pagination.html
|
|
|
|
|
|
return templates.TemplateResponse(
|