]> git.mxchange.org Git - fba.git/blob - fetch_instances.py
63a8fa793fac486f3933c2e142fc113334907fad
[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     # 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)
32
33     # NOISY-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         # NOISY-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         # NOISY-DEBUG: print("DEBUG: BEFORE instance:", instance)
46         instance = fba.tidyup(instance)
47         # NOISY-DEBUG: print("DEBUG: AFTER instance:", instance)
48
49         if instance == "":
50             print("WARNING: Empty instance after tidyup(), domain:", domain)
51             continue
52         elif not validators.domain(instance.split("/")[0]):
53             print(f"WARNING: Bad instance='{instance}' from domain='{domain}',origin='{origin}',software='{software}'")
54             continue
55         elif fba.is_blacklisted(instance):
56             # NOISY-DEBUG: print("DEBUG: instance is blacklisted:", instance)
57             continue
58
59         # NOISY-DEBUG: print("DEBUG: Handling instance:", instance)
60         try:
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)}'")
66             continue
67
68 instance = sys.argv[1]
69
70 # Initial fetch
71 fetch_instances(instance, None, None)
72
73 # Loop through some instances
74 fba.cursor.execute(
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"]]
76 )
77
78 rows = fba.cursor.fetchall()
79 print(f"INFO: Checking {len(rows)} entries ...")
80 for row in rows:
81     # NOISY-DEBUG: print("DEBUG: domain:", row[0])
82     if fba.is_blacklisted(row[0]):
83         print("WARNING: domain is blacklisted:", row[0])
84         continue
85
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])
88
89 fba.connection.close()