From ef33a7e8adbc8c43891ee8f280e8a449badd9576 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 19 May 2023 18:45:19 +0200 Subject: [PATCH] Continued: - more formatting --- api.py | 34 +++++++++++++++++++++++----------- fba.py | 3 ++- fetch_blocks.py | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/api.py b/api.py index 30e06bd..f9605e3 100644 --- a/api.py +++ b/api.py @@ -1,10 +1,11 @@ -import uvicorn from fastapi import FastAPI, Request, HTTPException, responses, Query -import sqlite3 from fastapi.templating import Jinja2Templates from requests import get from datetime import datetime from email import utils + +import uvicorn +import sqlite3 import re import fba @@ -55,17 +56,23 @@ def blocked(domain: str = None, reason: str = None, reverse: str = None): if domain != None: wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):]) punycode = domain.encode('idna').decode('utf-8') - fba.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 asc", + fba.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 ASC", (domain, "*." + domain, wildchar, fba.get_hash(domain), punycode, "*." + punycode)) elif reverse != None: - fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE blocker = ? ORDER BY first_added asc", (reverse,)) + fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE blocker = ? ORDER BY first_added ASC", (reverse,)) else: - fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE reason like ? AND reason != '' ORDER BY first_added asc", ("%"+reason+"%",)) + fba.c.execute("SELECT blocker, blocked, block_level, reason, first_added, last_seen FROM blocks WHERE reason like ? AND reason != '' ORDER BY first_added ASC", ("%"+reason+"%",)) blocks = fba.c.fetchall() result = {} for blocker, blocked, block_level, reason, first_added, last_seen in blocks: - entry = {"blocker": blocker, "blocked": blocked, "reason": reason, "first_added": first_added, "last_seen": last_seen} + entry = { + "blocker": blocker, + "blocked": blocked, + "reason": reason, + "first_added": first_added, + "last_seen": last_seen + } if block_level in result: result[block_level].append(entry) else: @@ -159,19 +166,24 @@ def rss(request: Request, domain: str = None): else: reason = "Provided reason: '" + reason + "'" - result.append( - { + 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"}) + 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=fba.config["port"], log_level=fba.config["log_level"]) diff --git a/fba.py b/fba.py index 188fb70..c18c623 100644 --- a/fba.py +++ b/fba.py @@ -1,5 +1,6 @@ from bs4 import BeautifulSoup from hashlib import sha256 + import reqto import re import sqlite3 @@ -32,7 +33,7 @@ def get_peers(domain: str) -> str: res = reqto.get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=5) return res.json() except: - print("WARNING: Cannot fetch peers:", domain) + print("WARNING: Cannot fetch peers:", domain, res.status_code) return None def get_type(instdomain: str) -> str: diff --git a/fetch_blocks.py b/fetch_blocks.py index 343b67b..cf9d97c 100644 --- a/fetch_blocks.py +++ b/fetch_blocks.py @@ -6,7 +6,7 @@ import itertools import re fba.c.execute( - "SELECT domain, software FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')" + "SELECT domain, software FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial') ORDER BY rowid DESC" ) for blocker, software in fba.c.fetchall(): -- 2.39.5