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/>.
17 from fba import config
18 from fba import federation
19 from fba import instances
20 from fba import network
22 def fetch_peers(domain: str) -> list:
23 # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software='lemmy' - CALLED!")
24 if not isinstance(domain, str):
25 raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
27 raise ValueError("Parameter 'domain' is empty")
31 # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
32 data = network.get_json_api(
35 (config.get("connection_timeout"), config.get("read_timeout"))
38 # DEBUG: print(f"DEBUG: data['{type(data)}']='{data}'")
39 if "error_message" in data:
40 print("WARNING: Could not reach any JSON API:", domain)
41 instances.update_last_error(domain, response)
42 elif "federated_instances" in data:
43 # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'")
44 peers = peers + federation.add_peers(data["federated_instances"])
45 # DEBUG: print("DEBUG: Added instance(s) to peers")
47 print("WARNING: JSON response does not contain 'federated_instances':", domain)
48 instances.update_last_error(domain, data)
50 except BaseException as exception:
51 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
53 # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
54 instances.set_data("total_peers", domain, len(peers))
56 # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
57 instances.update_last_instance_fetch(domain)
59 # DEBUG: print("DEBUG: Returning peers[]:", type(peers))