]> git.mxchange.org Git - fba.git/blob - fetch_instances.py
Continued:
[fba.git] / fetch_instances.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
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.
8 #
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.
13 #
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/>.
16
17 import sqlite3
18 import sys
19 import json
20 import time
21 import validators
22 import fba
23
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)
29
30     # NOISY-DEBUG: print("DEBUG: Fetching instances for domain:", domain, software)
31     peerlist = fba.get_peers(domain, software)
32
33     if (peerlist is None):
34         print("ERROR: Cannot fetch peers:", domain)
35         return
36     elif domain in fba.nodeinfos["get_peers_url"]:
37         # NOISY-DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo data, flushing ...")
38         fba.update_nodeinfos(domain)
39
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)
45
46         if instance == "":
47             print("WARNING: Empty instance after tidyup(), domain:", domain)
48             continue
49         elif not validators.domain(instance.split("/")[0]):
50             print(f"WARNING: Bad instance='{instance}' from domain='{domain}',origin='{origin}',software='{software}'")
51             continue
52         elif fba.is_blacklisted(instance):
53             # NOISY-DEBUG: print("DEBUG: instance is blacklisted:", instance)
54             continue
55
56         # NOISY-DEBUG: print("DEBUG: Handling instance:", instance)
57         try:
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)}'")
63             continue
64
65 instance = sys.argv[1]
66
67 # Initial fetch
68 fetch_instances(instance, None, None)
69
70 # Loop through some instances
71 fba.cursor.execute(
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"]]
73 )
74
75 rows = fba.cursor.fetchall()
76 print(f"INFO: Checking {len(rows)} entries ...")
77 for row in rows:
78     # NOISY-DEBUG: print("DEBUG: domain:", row[0])
79     if fba.is_blacklisted(row[0]):
80         print("WARNING: domain is blacklisted:", row[0])
81         continue
82
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])
85
86 fba.connection.close()