# along with this program. If not, see <https://www.gnu.org/licenses/>.
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
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()
"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:
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")
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
return templates.TemplateResponse("scoreboard.html", {
"base_url" : fba.config["base_url"],
+ "slogan" : fba.config["slogan"],
"request" : request,
"scoreboard": True,
"blockers" : blockers,
"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("/")
"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(
# 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("."):])
"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"])
margin: auto;
margin-top: 10px;
}
- .scoreboard {
- background-color: #1c1c3c;
- width: 40em;
- padding: 5px;
- margin: auto;
- margin-top: 10px;
- }
table {
width: 100%;
background-color: #2d2d4d;
</style>
</head>
<body>
- {% if scoreboard %}
- {% if blockers %}
- <h1>Top {{blockers}} defederating instances</h1>
- {% elif blocked %}
- <h1>Top {{blocked}} defederated instances</h1>
- {% elif reference %}
- <h1>Top {{reference}} referencing instances</h1>
- {% elif software %}
- <h1>Top {{software}} used software</h1>
- {% endif %}
- <div class="scoreboard">
- <table>
- <th>№</th>
- <th>{% if software %}Software{% else %}Instance{% endif %}</th>
- <th>{% if reference %}References{% elif software %}Total{% else %}Blocks{% endif %}</th>
- {% for entry in scores %}
- <tr>
- <td>{{loop.index}}</td>
- <td>
- {% if software %}
- {{entry['domain']}}
- {% else %}
- <a href="{{base_url}}/?{% if blockers %}reverse{% elif blocked or reference %}domain{% endif %}={{entry['domain']}}" rel="nofollow noopener noreferrer">{{entry['domain']}}</a>
- <a class="listlink" href="https://{{entry['domain']}}" rel="external" target="_blank">↗</a>
- {% endif %}
- </td>
- <td>{{entry['highscore']}}</td>
- </tr>
- {% endfor %}
- </table>
- </div>
- {% elif reason or domain or reverse %}
+ {% if reason or domain or reverse %}
{% if reason %}
<h1>Instances that use "{{reason}}" in their reason</h1>
{% elif reverse %}
--- /dev/null
+<!DOCTYPE html>
+<head>
+ <title>fedi-block-api</title>
+ <link rel="alternate" type="application/rss+xml" title="RSS Feed for latest blocked instances" href="{{base_url}}/rss" />
+ <style>
+ body {
+ background-color: #000022;
+ color: #ffffff;
+ text-align: center;
+ font-family: sans;
+ }
+ .block_level {
+ background-color: #1c1c3c;
+ width: 80em;
+ padding: 5px;
+ margin: auto;
+ margin-top: 10px;
+ }
+ .scoreboard {
+ background-color: #1c1c3c;
+ width: 40em;
+ padding: 5px;
+ margin: auto;
+ margin-top: 10px;
+ }
+ table {
+ width: 100%;
+ background-color: #2d2d4d;
+ border-spacing: 0px;
+ }
+ table tr:nth-of-type(2n) {
+ background-color: #1c1c3c;
+ }
+ table td {
+ padding: 4px;
+ }
+ .block_level table td:nth-of-type(1), .block_level table td:nth-of-type(2),
+ .block_level table td:nth-of-type(4), .block_level table td:nth-of-type(5) {
+ white-space: nowrap;
+ }
+ .block {
+ background-color: #2d2d4d;
+ padding: 5px;
+ margin: 5px;
+ }
+ a {
+ color: #ffffff;
+ }
+ a.listlink {
+ text-decoration: none;
+ font-size: 0.8em;
+ }
+ .info {
+ margin-top: 25px;
+ }
+ input[type="text"], input[type="submit"] {
+ padding: 5px;
+ border-radius: 5px;
+ color: white;
+ background: #445;
+ font-size: 16px;
+ }
+
+ input[type="text"]:hover {
+ border-color: #f08;
+ }
+
+ input[type="submit"] {
+ cursor: pointer;
+ }
+
+ input[type="submit"]:hover {
+ border-color: #f08;
+ }
+
+ span[title] {
+ text-decoration: underline dotted;
+ }
+ </style>
+</head>
+<body>
+ {% if blockers %}
+ <h1>Top {{blockers}} defederating instances</h1>
+ {% elif blocked %}
+ <h1>Top {{blocked}} defederated instances</h1>
+ {% elif reference %}
+ <h1>Top {{reference}} referencing instances</h1>
+ {% elif software %}
+ <h1>Top {{software}} used software</h1>
+ {% endif %}
+ <div class="scoreboard">
+ <table>
+ <th>№</th>
+ <th>{% if software %}Software{% else %}Instance{% endif %}</th>
+ <th>{% if reference %}References{% elif software %}Total{% else %}Blocks{% endif %}</th>
+ {% for entry in scores %}
+ <tr>
+ <td>{{loop.index}}</td>
+ <td>
+ {% if software %}
+ {{entry['domain']}}
+ {% else %}
+ <a href="{{base_url}}/?{% if blockers %}reverse{% elif blocked or reference %}domain{% endif %}={{entry['domain']}}" rel="nofollow noopener noreferrer">{{entry['domain']}}</a>
+ <a class="listlink" href="https://{{entry['domain']}}" rel="external" target="_blank">↗</a>
+ {% endif %}
+ </td>
+ <td>{{entry['highscore']}}</td>
+ </tr>
+ {% endfor %}
+ </table>
+ </div>
+ <div class="info">
+ source code: <a href="https://git.mxchange.org/?p=fba.git;a=summary" rel="source" target="_blank">git.mxchange.org</a><br /><br />
+ {{slogan}}
+ </div>
+</body>
+</html>