1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
24 def fetch_instances(domain: str, origin: str, software: str, path: str = None):
25 # NOISY-DEBUG: print("DEBUG: domain,origin,software,path:", domain, origin, software, path)
26 if not fba.is_instance_registered(domain):
27 # NOISY-DEBUG: print("DEBUG: Adding new domain:", domain, origin)
28 fba.add_instance(domain, origin, sys.argv[0], path)
30 # NOISY-DEBUG: print("DEBUG: Fetching instances for domain:", domain, software)
31 peerlist = fba.get_peers(domain, software)
33 if (peerlist is None):
34 print("ERROR: Cannot fetch peers:", domain)
36 elif fba.has_pending_nodeinfos(domain):
37 # NOISY-DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo data, flushing ...")
38 fba.update_nodeinfos(domain)
40 print(f"INFO: Checking {len(peerlist)} instances from {domain} ...")
41 for instance in peerlist:
42 # NOISY-DEBUG: print("DEBUG: BEFORE instance:", instance)
43 instance = fba.tidyup(instance)
44 # NOISY-DEBUG: print("DEBUG: AFTER instance:", instance)
47 print("WARNING: Empty instance after tidyup(), domain:", domain)
49 elif not validators.domain(instance.split("/")[0]):
50 print(f"WARNING: Bad instance='{instance}' from domain='{domain}',origin='{origin}',software='{software}'")
52 elif fba.is_blacklisted(instance):
53 # NOISY-DEBUG: print("DEBUG: instance is blacklisted:", instance)
56 # NOISY-DEBUG: print("DEBUG: Handling instance:", instance)
58 if not fba.is_instance_registered(instance):
59 # NOISY-DEBUG: print("DEBUG: Adding new instance:", instance, domain)
60 fba.add_instance(instance, domain, sys.argv[0])
61 except BaseException as e:
62 print(f"ERROR: instance='{instance}',exception:'{str(e)}'")
65 instance = sys.argv[1]
68 fetch_instances(instance, None, None)
70 # Loop through some instances
72 "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"]]
75 rows = fba.cursor.fetchall()
76 print(f"INFO: Checking {len(rows)} entries ...")
78 # NOISY-DEBUG: print("DEBUG: domain:", row[0])
79 if fba.is_blacklisted(row[0]):
80 print("WARNING: domain is blacklisted:", row[0])
83 print(f"INFO: Fetching instances for instance '{row[0]}'('{row[2]}') of origin '{row[1]}',nodeinfo_url='{row[3]}'")
84 fetch_instances(row[0], row[1], row[2], row[3])
86 fba.connection.close()