]> git.mxchange.org Git - fba.git/blob - fba/federation/peertube.py
Continued:
[fba.git] / fba / federation / peertube.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 fba
19 from fba import instances
20 from fba import network
21
22 def fetch_peers(domain: str) -> list:
23     # DEBUG: 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'")
26     elif domain == "":
27         raise ValueError("Parameter 'domain' is empty")
28
29     # DEBUG: print(f"DEBUG: domain='{domain}' is a PeerTube, fetching JSON ...")
30     peers = list()
31     start = 0
32     for mode in ["followers", "following"]:
33         # DEBUG: print(f"DEBUG: domain='{domain}',mode='{mode}'")
34         while True:
35             try:
36                 response = network.fetch_response(
37                     domain,
38                     "/api/v1/server/{mode}?start={start}&count=100",
39                     network.api_headers,
40                     (config.get("connection_timeout"), config.get("read_timeout"))
41                 )
42
43                 data = fba.json_from_response(response)
44                 # DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code='{response.status_code}',data[]='{type(data)}'")
45                 if response.ok and isinstance(data, dict):
46                     # DEBUG: print("DEBUG: Success, data:", len(data))
47                     if "data" in data:
48                         # DEBUG: print(f"DEBUG: Found {len(data['data'])} record(s).")
49                         for record in data["data"]:
50                             # DEBUG: print(f"DEBUG: record()={len(record)}")
51                             if mode in record and "host" in record[mode]:
52                                 # DEBUG: print(f"DEBUG: Found host={record[mode]['host']}, adding ...")
53                                 peers.append(record[mode]["host"])
54                             else:
55                                 print(f"WARNING: record from '{domain}' has no '{mode}' or 'host' record: {record}")
56
57                         if len(data["data"]) < 100:
58                             # DEBUG: print("DEBUG: Reached end of JSON response:", domain)
59                             break
60
61                     # Continue with next row
62                     start = start + 100
63
64             except BaseException as exception:
65                 print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(exception)}]:'{str(exception)}'")
66
67     # DEBUG: print(f"DEBUG: Adding '{len(peers)}' for domain='{domain}'")
68     instances.set_data("total_peers", domain, len(peers))
69
70     # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
71     instances.update_last_instance_fetch(domain)
72
73     # DEBUG: print("DEBUG: Returning peers[]:", type(peers))
74     return peers