]> git.mxchange.org Git - fba.git/blob - api.py
also use hash to find blocks
[fba.git] / api.py
1 from fastapi import FastAPI
2 import sqlite3
3 from hashlib import sha256
4
5 base_url = ""
6 app = FastAPI(docs_url=base_url+"/docs", redoc_url=base_url+"/redoc")
7
8 def get_hash(domain: str) -> str:
9     return sha256(domain.encode("utf-8")).hexdigest()
10
11 @app.get(base_url+"/info")
12 def info():
13     conn = sqlite3.connect("blocks.db")
14     c = conn.cursor()
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()
17     c.close()
18     return {
19         "known_instances": known,
20         "indexed_instances": indexed,
21         "blocks_recorded": blocks,
22         "source_code": "https://gitlab.com/EnjuAihara/fedi-block-api",
23     }
24
25 @app.get(base_url+"/domain/{domain}")
26 def blocked(domain: str):
27     conn = sqlite3.connect("blocks.db")
28     c = conn.cursor()
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)))
31     blocks = c.fetchall()
32     conn.close()
33
34     result = {}
35     reasons = {}
36
37     for domain, block_level, reason in blocks:
38         if block_level in result:
39             result[block_level].append(domain)
40         else:
41             result[block_level] = [domain]
42             
43         if reason != "":
44             if block_level in reasons:
45                 reasons[block_level][domain] = reason
46             else:
47                 reasons[block_level] = {domain: reason}
48
49     return {"blocks": result, "reasons": reasons}
50