]> git.mxchange.org Git - fba.git/blob - fba/federation/lemmy.py
Continued:
[fba.git] / fba / federation / lemmy.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 sys
18
19 from fba import config
20 from fba import fba
21 from fba import instances
22
23 def get_peers(domain: str) -> list:
24     # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software={software} - CALLED!")
25     if type(domain) != str:
26         raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
27     elif domain == "":
28         raise ValueError(f"Parameter 'domain' is empty")
29
30     # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
31     peers = list()
32     try:
33         response = fba.get_response(domain, "/api/v3/site", fba.api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
34
35         data = fba.json_from_response(response)
36
37         # DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code='{response.status_code}',data[]='{type(data)}'")
38         if not response.ok or response.status_code >= 400:
39             print("WARNING: Could not reach any JSON API:", domain)
40             fba.update_last_error(domain, response)
41         elif response.ok and isinstance(data, list):
42             print(f"UNSUPPORTED: domain='{domain}' returned a list: '{data}'")
43             sys.exit(255)
44         elif "federated_instances" in data:
45             # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'")
46             peers = peers + fba.add_peers(data["federated_instances"])
47             # DEBUG: print("DEBUG: Added instance(s) to peers")
48         else:
49             print("WARNING: JSON response does not contain 'federated_instances':", domain)
50             fba.update_last_error(domain, response)
51
52     except BaseException as e:
53         print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
54
55     # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
56     instances.set("total_peers", domain, len(peers))
57
58     # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
59     instances.update_last_instance_fetch(domain)
60
61     # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
62     return peers