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