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
19 from fba import instances
20 from fba import network
22 def fetch_peers(domain: str) -> list:
23 print(f"DEBUG: domain({len(domain)})={domain},software='peertube' - 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")
29 print(f"DEBUG: domain='{domain}' is a PeerTube, fetching JSON ...")
34 print(f"DEBUG: Checking CSRF for domain='{domain}'")
36 headers = csrf.determine(domain, dict())
37 except network.exceptions as exception:
38 print(f"WARNING: Exception '{type(exception)}' during checking CSRF - EXIT!")
41 for mode in ["followers", "following"]:
42 print(f"DEBUG: domain='{domain}',mode='{mode}'")
44 data = network.get_json_api(
46 "/api/v1/server/{mode}?start={start}&count=100",
48 (config.get("connection_timeout"), config.get("read_timeout"))
51 print(f"DEBUG: data['{type(data)}']='{data}'")
52 if "error_message" not in data:
53 print("DEBUG: Success, data[json]:", len(data["json"]))
54 if "data" in data["json"]:
55 print(f"DEBUG: Found {len(data['data'])} record(s).")
56 for record in data["json"]["data"]:
57 print(f"DEBUG: record()={len(record)}")
58 if mode in record and "host" in record[mode]:
59 print(f"DEBUG: Found host={record[mode]['host']}, adding ...")
60 peers.append(record[mode]["host"])
62 print(f"WARNING: record from '{domain}' has no '{mode}' or 'host' record: {record}")
64 if len(data["json"]["data"]) < 100:
65 print("DEBUG: Reached end of JSON response:", domain)
68 # Continue with next row
71 print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
72 instances.set_data("total_peers", domain, len(peers))
74 print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
75 instances.update_last_instance_fetch(domain)
77 print("DEBUG: Returning peers[]:", type(peers))