From ab9778ff76580cb17aa79d3e654466a9b1e06728 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 29 May 2023 20:58:44 +0200 Subject: [PATCH] Continued: - splitted "scoreboard.html" out of index.html - added route + template for "robots.txt request - renamed "app" to "router" --- api.py | 31 ++++++---- templates/index.html | 40 +------------ templates/robots.txt | 2 + templates/scoreboard.html | 117 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 49 deletions(-) create mode 100644 templates/robots.txt create mode 100644 templates/scoreboard.html diff --git a/api.py b/api.py index fdf5b07..7b18ead 100644 --- a/api.py +++ b/api.py @@ -15,6 +15,7 @@ # along with this program. If not, see . from fastapi import Request, HTTPException, responses, Query +from fastapi.responses import PlainTextResponse from fastapi.templating import Jinja2Templates from datetime import datetime from email import utils @@ -25,10 +26,10 @@ import requests import re import fba -app = fastapi.FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc") +router = fastapi.FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc") templates = Jinja2Templates(directory="templates") -@app.get(fba.config["base_url"] + "/api/info") +@router.get(fba.config["base_url"] + "/api/info") def info(): fba.cursor.execute("SELECT (SELECT COUNT(domain) FROM instances), (SELECT COUNT(domain) FROM instances WHERE software IN ('pleroma', 'mastodon', 'misskey', 'gotosocial', 'friendica', 'bookwyrm', 'takahe', 'peertube')), (SELECT COUNT(blocker) FROM blocks), (SELECT COUNT(domain) FROM instances WHERE last_status_code IS NOT NULL)") known, indexed, blocks, errorous = fba.cursor.fetchone() @@ -41,7 +42,7 @@ def info(): "slogan" : fba.config["slogan"] } -@app.get(fba.config["base_url"] + "/api/top") +@router.get(fba.config["base_url"] + "/api/top") def top(blocked: int = None, blockers: int = None, reference: int = None, software: int = None): if blocked != None: if blocked > 500: @@ -74,7 +75,7 @@ def top(blocked: int = None, blockers: int = None, reference: int = None, softwa return scoreboard -@app.get(fba.config["base_url"] + "/api") +@router.get(fba.config["base_url"] + "/api") def blocked(domain: str = None, reason: str = None, reverse: str = None): if domain == None and reason == None and reverse == None: raise HTTPException(status_code=400, detail="No filter specified") @@ -112,7 +113,7 @@ def blocked(domain: str = None, reason: str = None, reverse: str = None): return result -@app.get(fba.config["base_url"] + "/scoreboard") +@router.get(fba.config["base_url"] + "/scoreboard") def index(request: Request, blockers: int = None, blocked: int = None, reference: int = None, software: int = None): scores = None @@ -134,6 +135,7 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference return templates.TemplateResponse("scoreboard.html", { "base_url" : fba.config["base_url"], + "slogan" : fba.config["slogan"], "request" : request, "scoreboard": True, "blockers" : blockers, @@ -143,7 +145,7 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference "scores" : res.json() }) -@app.get(fba.config["base_url"] + "/") +@router.get(fba.config["base_url"] + "/") def index(request: Request, domain: str = None, reason: str = None, reverse: str = None): if domain == "" or reason == "" or reverse == "": return responses.RedirectResponse("/") @@ -183,7 +185,7 @@ def index(request: Request, domain: str = None, reason: str = None, reverse: str "info" : info }) -@app.get(fba.config["base_url"] + "/api/mutual") +@router.get(fba.config["base_url"] + "/api/mutual") def mutual(domains: list[str] = Query()): """Return 200 if federation is open between the two, 4xx otherwise""" fba.cursor.execute( @@ -207,7 +209,7 @@ def mutual(domains: list[str] = Query()): # No known blocks return responses.JSONResponse(status_code=200, content={}) -@app.get(fba.config["base_url"] + "/rss") +@router.get(fba.config["base_url"] + "/rss") def rss(request: Request, domain: str = None): if domain != None: wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) @@ -243,8 +245,17 @@ def rss(request: Request, domain: str = None): "domain" : domain, "blocks" : result }, headers={ - "Content-Type": "application/rss+xml" + "Content-Type": "routerlication/rss+xml" + }) + +@router.get(fba.config["base_url"] + "/robots.txt", response_class=PlainTextResponse) +def robots(request: Request): + return templates.TemplateResponse("robots.txt", { + "request" : request, + "base_url": fba.config["base_url"] + }, headers={ + "Content-Type": "text/plain" }) if __name__ == "__main__": - uvicorn.run("api:app", host=fba.config["host"], port=fba.config["port"], log_level=fba.config["log_level"]) + uvicorn.run("api:router", host=fba.config["host"], port=fba.config["port"], log_level=fba.config["log_level"]) diff --git a/templates/index.html b/templates/index.html index f25d00f..39fcfcc 100644 --- a/templates/index.html +++ b/templates/index.html @@ -21,13 +21,6 @@ margin: auto; margin-top: 10px; } - .scoreboard { - background-color: #1c1c3c; - width: 40em; - padding: 5px; - margin: auto; - margin-top: 10px; - } table { width: 100%; background-color: #2d2d4d; @@ -84,38 +77,7 @@ - {% if scoreboard %} - {% if blockers %} -

Top {{blockers}} defederating instances

- {% elif blocked %} -

Top {{blocked}} defederated instances

- {% elif reference %} -

Top {{reference}} referencing instances

- {% elif software %} -

Top {{software}} used software

- {% endif %} -
- - - - - {% for entry in scores %} - - - - - - {% endfor %} -
№{% if software %}Software{% else %}Instance{% endif %}{% if reference %}References{% elif software %}Total{% else %}Blocks{% endif %}
{{loop.index}} - {% if software %} - {{entry['domain']}} - {% else %} - {{entry['domain']}}  - ↗ - {% endif %} - {{entry['highscore']}}
-
- {% elif reason or domain or reverse %} + {% if reason or domain or reverse %} {% if reason %}

Instances that use "{{reason}}" in their reason

{% elif reverse %} diff --git a/templates/robots.txt b/templates/robots.txt new file mode 100644 index 0000000..6fdd266 --- /dev/null +++ b/templates/robots.txt @@ -0,0 +1,2 @@ +User-Agent: * +Disallow {{base_url}}/api/ diff --git a/templates/scoreboard.html b/templates/scoreboard.html new file mode 100644 index 0000000..83e1f08 --- /dev/null +++ b/templates/scoreboard.html @@ -0,0 +1,117 @@ + + + fedi-block-api + + + + + {% if blockers %} +

Top {{blockers}} defederating instances

+ {% elif blocked %} +

Top {{blocked}} defederated instances

+ {% elif reference %} +

Top {{reference}} referencing instances

+ {% elif software %} +

Top {{software}} used software

+ {% endif %} +
+ + + + + {% for entry in scores %} + + + + + + {% endfor %} +
№{% if software %}Software{% else %}Instance{% endif %}{% if reference %}References{% elif software %}Total{% else %}Blocks{% endif %}
{{loop.index}} + {% if software %} + {{entry['domain']}} + {% else %} + {{entry['domain']}}  + ↗ + {% endif %} + {{entry['highscore']}}
+
+
+ source code: git.mxchange.org

+ {{slogan}} +
+ + -- 2.39.5