]> git.mxchange.org Git - fba.git/blob - api.py
API now only returns block levels that are used
[fba.git] / api.py
1 from fastapi import FastAPI
2 import sqlite3
3
4 base_url = ""
5 app = FastAPI(docs_url=base_url+"/docs", redoc_url=base_url+"/redoc")
6
7 @app.get(base_url+"/info")
8 def info():
9     conn = sqlite3.connect("blocks.db")
10     c = conn.cursor()
11     c.execute("select (select count(domain) from instances), (select count(domain) from instances where software in ('pleroma', 'mastodon')), (select count(blocker) from blocks)")
12     known, indexed, blocks = c.fetchone()
13     c.close()
14     return {
15         "known_instances": known,
16         "indexed_instances": indexed,
17         "blocks_recorded": blocks,
18         "source_code": "https://gitlab.com/EnjuAihara/fedi-block-api",
19     }
20
21 @app.get(base_url+"/domain/{domain}")
22 def blocked(domain: str):
23     conn = sqlite3.connect("blocks.db")
24     c = conn.cursor()
25     c.execute("select blocker, block_level, reason from blocks where blocked = ?", (domain,))
26     blocks = c.fetchall()
27     conn.close()
28
29     result = {}
30     reasons = {}
31
32     for domain, block_level, reason in blocks:
33         if block_level in result:
34             result[block_level].append(domain)
35         else:
36             result[block_level] = [domain]
37             
38         if reason != "":
39             if block_level in reasons:
40                 reasons[block_level][domain] = reason
41             else:
42                 reasons[block_level] = {domain: reason}
43
44     return {"blocks": result, "reasons": reasons}
45