]> git.mxchange.org Git - fba.git/blob - fetch_instances.py
fd217efcf7ffd9f24641ed9da4ccbfde15029a0d
[fba.git] / fetch_instances.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
5 # Copyright (C) 2023 Free Software Foundation
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published
9 # by the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20 import sqlite3
21 import sys
22 import json
23 import time
24 import validators
25 import fba
26
27 def fetch_instances(domain: str, origin: str, software: str, path: str = None):
28     # DEBUG: print("DEBUG: domain,origin,software,path:", domain, origin, software, path)
29     if not fba.is_instance_registered(domain):
30         # DEBUG: print("DEBUG: Adding new domain:", domain, origin)
31         fba.add_instance(domain, origin, sys.argv[0], path)
32
33     # DEBUG: print("DEBUG: Fetching instances for domain:", domain, software)
34     peerlist = fba.get_peers(domain, software)
35
36     if (peerlist is None):
37         print("ERROR: Cannot fetch peers:", domain)
38         return
39     elif fba.has_pending_nodeinfos(domain):
40         # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo data, flushing ...")
41         fba.update_nodeinfos(domain)
42
43     print(f"INFO: Checking {len(peerlist)} instances from {domain} ...")
44     for instance in peerlist:
45         if peerlist == None:
46             # Skip "None" types as tidup() cannot parse them
47             continue
48
49         # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - BEFORE")
50         instance = fba.tidyup(instance)
51         # DEBUG: print(f"DEBUG: instance[{type(instance}]={instance} - AFTER")
52
53         if instance == "":
54             print("WARNING: Empty instance after tidyup(), domain:", domain)
55             continue
56         elif not validators.domain(instance.split("/")[0]):
57             print(f"WARNING: Bad instance='{instance}' from domain='{domain}',origin='{origin}',software='{software}'")
58             continue
59         elif fba.is_blacklisted(instance):
60             # DEBUG: print("DEBUG: instance is blacklisted:", instance)
61             continue
62
63         # DEBUG: print("DEBUG: Handling instance:", instance)
64         try:
65             if not fba.is_instance_registered(instance):
66                 # DEBUG: print("DEBUG: Adding new instance:", instance, domain)
67                 fba.add_instance(instance, domain, sys.argv[0])
68         except BaseException as e:
69             print(f"ERROR: instance='{instance}',exception[{type(e)}]:'{str(e)}'")
70             continue
71
72 instance = sys.argv[1]
73
74 # Initial fetch
75 fetch_instances(instance, None, None)
76
77 # Loop through some instances
78 fba.cursor.execute(
79     "SELECT domain, origin, software, nodeinfo_url FROM instances WHERE software IN ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial', 'bookwyrm', 'takahe', 'lemmy') AND (last_instance_fetch IS NULL OR last_instance_fetch < ?) ORDER BY rowid DESC", [time.time() - fba.config["recheck_instance"]]
80 )
81
82 rows = fba.cursor.fetchall()
83 print(f"INFO: Checking {len(rows)} entries ...")
84 for row in rows:
85     # DEBUG: print("DEBUG: domain:", row[0])
86     if fba.is_blacklisted(row[0]):
87         print("WARNING: domain is blacklisted:", row[0])
88         continue
89
90     print(f"INFO: Fetching instances for instance '{row[0]}' ('{row[2]}') of origin '{row[1]}',nodeinfo_url='{row[3]}'")
91     fetch_instances(row[0], row[1], row[2], row[3])
92
93 fba.connection.close()