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