RSS feed for latest blocks

This commit is contained in:
Mint 2023-05-08 00:56:48 +03:00
parent 901e0f2fec
commit df90944351
2 changed files with 45 additions and 1 deletions

29
api.py
View file

@ -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")

17
rss.xml Normal file
View file

@ -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>