]> git.mxchange.org Git - fba.git/blob - fba/networks/lemmy.py
Continued:
[fba.git] / fba / networks / 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 from fba import config
18 from fba import csrf
19 from fba import federation
20 from fba import instances
21 from fba import network
22
23 def fetch_peers(domain: str) -> list:
24     # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software='lemmy' - CALLED!")
25     if not isinstance(domain, str):
26         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
27     elif domain == "":
28         raise ValueError("Parameter 'domain' is empty")
29
30     peers = list()
31
32     # No CSRF by default, you don't have to add network.api_headers by yourself here
33     headers = tuple()
34
35     try:
36         # DEBUG: print(f"DEBUG: Checking CSRF for domain='{domain}'")
37         headers = csrf.determine(domain, dict())
38     except network.exceptions as exception:
39         print(f"WARNING: Exception '{type(exception)}' during checking CSRF (fetch_peers,{__name__}) - EXIT!")
40         return peers
41
42     try:
43         # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
44         data = network.get_json_api(
45             domain,
46             "/api/v3/site",
47             headers,
48             (config.get("connection_timeout"), config.get("read_timeout"))
49         )
50
51         # DEBUG: print(f"DEBUG: data['{type(data)}']='{data}'")
52         if "error_message" in data:
53             print("WARNING: Could not reach any JSON API:", domain)
54             instances.update_last_error(domain, data)
55         elif "federated_instances" in data["json"]:
56             # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'")
57             peers = peers + federation.add_peers(data["json"]["federated_instances"])
58             # DEBUG: print("DEBUG: Added instance(s) to peers")
59         else:
60             print("WARNING: JSON response does not contain 'federated_instances':", domain)
61             instances.update_last_error(domain, data)
62
63     except BaseException as exception:
64         print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
65
66     # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
67     instances.set_data("total_peers", domain, len(peers))
68
69     # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
70     instances.update_last_instance_fetch(domain)
71
72     # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
73     return peers