From df9094435128ae289b191153d0df23aef00b99a2 Mon Sep 17 00:00:00 2001 From: Mint <> Date: Mon, 8 May 2023 00:56:48 +0300 Subject: [PATCH] RSS feed for latest blocks --- api.py | 29 ++++++++++++++++++++++++++++- rss.xml | 17 +++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 rss.xml diff --git a/api.py b/api.py index d120688..e3ae0a2 100644 --- a/api.py +++ b/api.py @@ -7,6 +7,7 @@ from requests import get from json import loads from re import sub from datetime import datetime +from email import utils with open("config.json") as f: config = loads(f.read()) @@ -154,6 +155,32 @@ def mutual(domains: list[str] = Query()): # No known blocks return responses.JSONResponse(status_code=200, content={}) +@app.get(base_url+"/rss") +def rss(request: Request, domain: str = None): + conn = sqlite3.connect("blocks.db") + c = conn.cursor() + if domain != None: + wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) + punycode = domain.encode('idna').decode('utf-8') + c.execute("select blocker, blocked, block_level, reason, first_added, last_seen from blocks where blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ? or blocked = ? order by first_added desc limit 50", + (domain, "*." + domain, wildchar, get_hash(domain), punycode, "*." + punycode)) + else: + c.execute("select blocker, blocked, block_level, reason, first_added, last_seen from blocks order by first_added desc limit 50") + blocks = c.fetchall() + c.close() + + result = [] + for blocker, blocked, block_level, reason, first_added, last_seen in blocks: + first_added = utils.format_datetime(datetime.fromtimestamp(first_added)) + if reason == None or reason == '': + reason = "No reason provided." + else: + reason = "Provided reason: '" + reason + "'" + result.append({"blocker": blocker, "blocked": blocked, "block_level": block_level, "reason": reason, "first_added": first_added}) + + timestamp = utils.format_datetime(datetime.now()) + + return templates.TemplateResponse("rss.xml", {"request": request, "timestamp": timestamp, "domain": domain, "blocks": result}, headers={"Content-Type": "application/rss+xml"}) + if __name__ == "__main__": uvicorn.run("api:app", host="127.0.0.1", port=port, log_level="info") - diff --git a/rss.xml b/rss.xml new file mode 100644 index 0000000..eecfa31 --- /dev/null +++ b/rss.xml @@ -0,0 +1,17 @@ + + + + fedi-block-api{% if domain %} {{domain}}{% endif %} + Feed of latest blocks{% if domain %} for {{domain}}{% endif %} from fedi-block-api + {{timestamp}} + 1800 + {% for block in blocks %} + + {{block['blocker']}} has applied '{{block['block_level']}}' restriction to {{block['blocked']}} + {{block['reason']}} + https://fba.ryona.agency/?reverse={{block['blocker']}} + {{block['first_added']}} + + {% endfor %} + + -- 2.39.5