]> git.mxchange.org Git - fba.git/blob - fetch_instances.py
Forgot overwrite
[fba.git] / fetch_instances.py
1 from requests import get
2 from hashlib import sha256
3 import sqlite3
4 import sys
5 import json
6
7 domain = sys.argv[1]
8
9 headers = {
10     "user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0"
11 }
12
13
14 def get_hash(domain: str) -> str:
15     return sha256(domain.encode("utf-8")).hexdigest()
16
17
18 def get_peers(domain: str) -> str:
19     try:
20         res = get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=5)
21         return res.json()
22     except:
23         return None
24
25 peerlist = get_peers(domain)
26
27 def get_type(instdomain: str) -> str:
28     try:
29         res = get(f"https://{instdomain}/nodeinfo/2.1.json", headers=headers, timeout=5)
30         if res.status_code == 404:
31             res = get(f"https://{instdomain}/nodeinfo/2.0", headers=headers, timeout=5)
32         if res.status_code == 404:
33             res = get(f"https://{instdomain}/nodeinfo/2.0.json", headers=headers, timeout=5)
34         if res.ok and "text/html" in res.headers["content-type"]:
35             res = get(f"https://{instdomain}/nodeinfo/2.1", headers=headers, timeout=5)
36         if res.ok:
37             if res.json()["software"]["name"] == "akkoma":
38                 return "pleroma"
39             elif res.json()["software"]["name"] == "rebased":
40                 return "pleroma"
41             elif res.json()["software"]["name"] == "hometown":
42                 return "mastodon"
43             elif res.json()["software"]["name"] == "ecko":
44                 return "mastodon"
45             elif res.json()["software"]["name"] == "calckey":
46                 return "misskey"
47             else:
48                 return res.json()["software"]["name"]
49         elif res.status_code == 404:
50             res = get(f"https://{instdomain}/api/v1/instance", headers=headers, timeout=5)
51         if res.ok:
52             return "mastodon"
53     except:
54         return None
55
56
57 conn = sqlite3.connect("blocks.db")
58 c = conn.cursor()
59
60 c.execute(
61     "select domain from instances where 1"
62 )
63
64 for instance in peerlist:
65     instance = instance.lower()
66     print(instance)
67     try:
68         c.execute(
69             "select domain from instances where domain = ?", (instance,)
70         )
71         if c.fetchone() == None:
72             c.execute(
73                 "insert into instances select ?, ?, ?",
74                 (instance, get_hash(instance), get_type(instance)),
75             )
76         conn.commit()
77     except Exception as e:
78         print("error:", e, instance)
79 conn.close()