2 # -*- coding: utf-8 -*-
4 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
5 # Copyright (C) 2023 Free Software Foundation
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.
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.
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/>.
27 def fetch_instances(domain: str, origin: str, software: str, path: str = None):
28 # NOISY-DEBUG: print("DEBUG: domain,origin,software,path:", domain, origin, software, path)
29 if not fba.is_instance_registered(domain):
30 # NOISY-DEBUG: print("DEBUG: Adding new domain:", domain, origin)
31 fba.add_instance(domain, origin, sys.argv[0], path)
33 # NOISY-DEBUG: print("DEBUG: Fetching instances for domain:", domain, software)
34 peerlist = fba.get_peers(domain, software)
36 if (peerlist is None):
37 print("ERROR: Cannot fetch peers:", domain)
39 elif fba.has_pending_nodeinfos(domain):
40 # NOISY-DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo data, flushing ...")
41 fba.update_nodeinfos(domain)
43 print(f"INFO: Checking {len(peerlist)} instances from {domain} ...")
44 for instance in peerlist:
45 # NOISY-DEBUG: print("DEBUG: BEFORE instance:", instance)
46 instance = fba.tidyup(instance)
47 # NOISY-DEBUG: print("DEBUG: AFTER instance:", instance)
50 print("WARNING: Empty instance after tidyup(), domain:", domain)
52 elif not validators.domain(instance.split("/")[0]):
53 print(f"WARNING: Bad instance='{instance}' from domain='{domain}',origin='{origin}',software='{software}'")
55 elif fba.is_blacklisted(instance):
56 # NOISY-DEBUG: print("DEBUG: instance is blacklisted:", instance)
59 # NOISY-DEBUG: print("DEBUG: Handling instance:", instance)
61 if not fba.is_instance_registered(instance):
62 # NOISY-DEBUG: print("DEBUG: Adding new instance:", instance, domain)
63 fba.add_instance(instance, domain, sys.argv[0])
64 except BaseException as e:
65 print(f"ERROR: instance='{instance}',exception:'{str(e)}'")
68 instance = sys.argv[1]
71 fetch_instances(instance, None, None)
73 # Loop through some instances
75 "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"]]
78 rows = fba.cursor.fetchall()
79 print(f"INFO: Checking {len(rows)} entries ...")
81 # NOISY-DEBUG: print("DEBUG: domain:", row[0])
82 if fba.is_blacklisted(row[0]):
83 print("WARNING: domain is blacklisted:", row[0])
86 print(f"INFO: Fetching instances for instance '{row[0]}'('{row[2]}') of origin '{row[1]}',nodeinfo_url='{row[3]}'")
87 fetch_instances(row[0], row[1], row[2], row[3])
89 fba.connection.close()