1 from fastapi import FastAPI
3 from hashlib import sha256
6 app = FastAPI(docs_url=base_url+"/docs", redoc_url=base_url+"/redoc")
8 def get_hash(domain: str) -> str:
9 return sha256(domain.encode("utf-8")).hexdigest()
11 @app.get(base_url+"/info")
13 conn = sqlite3.connect("blocks.db")
15 c.execute("select (select count(domain) from instances), (select count(domain) from instances where software in ('pleroma', 'mastodon')), (select count(blocker) from blocks)")
16 known, indexed, blocks = c.fetchone()
19 "known_instances": known,
20 "indexed_instances": indexed,
21 "blocks_recorded": blocks,
22 "source_code": "https://gitlab.com/EnjuAihara/fedi-block-api",
25 @app.get(base_url+"/domain/{domain}")
26 def blocked(domain: str):
27 conn = sqlite3.connect("blocks.db")
29 wildchar = "*." + ".".join(domain.split(".")[-domain.count("."):])
30 c.execute("select blocker, block_level, reason from blocks where blocked = ? or blocked = ? or blocked = ?", (domain, wildchar, get_hash(domain)))
37 for domain, block_level, reason in blocks:
38 if block_level in result:
39 result[block_level].append(domain)
41 result[block_level] = [domain]
44 if block_level in reasons:
45 reasons[block_level][domain] = reason
47 reasons[block_level] = {domain: reason}
49 return {"blocks": result, "reasons": reasons}