]> git.mxchange.org Git - fba.git/commitdiff
RSS feed for latest blocks
authorMint <>
Sun, 7 May 2023 21:56:48 +0000 (00:56 +0300)
committerMint <>
Sun, 7 May 2023 21:56:48 +0000 (00:56 +0300)
api.py
rss.xml [new file with mode: 0644]

diff --git a/api.py b/api.py
index d12068856aec4af77fc4806f864a0b4636b0737b..e3ae0a22304854f2724e123946f4b9fabc978c58 100644 (file)
--- 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 (file)
index 0000000..eecfa31
--- /dev/null
+++ b/rss.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+<channel>
+ <title>fedi-block-api{% if domain %} {{domain}}{% endif %}</title>
+ <description>Feed of latest blocks{% if domain %} for {{domain}}{% endif %} from fedi-block-api</description>
+ <pubDate>{{timestamp}}</pubDate>
+ <ttl>1800</ttl>
+ {% for block in blocks %}
+ <item>
+  <title>{{block['blocker']}} has applied '{{block['block_level']}}' restriction to {{block['blocked']}}</title>
+  <description>{{block['reason']}}</description>
+  <link>https://fba.ryona.agency/?reverse={{block['blocker']}}</link>
+  <pubDate>{{block['first_added']}}</pubDate>
+ </item>
+ {% endfor %}
+</channel>
+</rss>