]> git.mxchange.org Git - fba.git/blob - fba/networks/peertube.py
Notation applied:
[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 import logging
18
19 from fba import csrf
20
21 from fba.helpers import config
22 from fba.helpers import domain as domain_helper
23
24 from fba.http import network
25
26 from fba.models import instances
27
28 logging.basicConfig(level=logging.INFO)
29 logger = logging.getLogger(__name__)
30
31 def fetch_peers(domain: str) -> list:
32     logger.debug("domain='%s' - CALLED!", domain)
33     domain_helper.raise_on(domain)
34
35     logger.debug("domain='%s' is a PeerTube, fetching JSON ...", domain)
36     peers   = list()
37     start   = 0
38
39     # No CSRF by default, you don't have to add network.api_headers by yourself here
40     headers = tuple()
41
42     try:
43         logger.debug("Checking CSRF for domain='%s'", domain)
44         headers = csrf.determine(domain, dict())
45     except network.exceptions as exception:
46         logger.warning("Exception '%s' during checking CSRF (fetch_peers,%s) - EXIT!", type(exception), __name__)
47         instances.set_last_error(domain, exception)
48         return list()
49
50     for mode in ["followers", "following"]:
51         logger.debug("domain='%s',mode='%s'", domain, mode)
52         while True:
53             data = network.get_json_api(
54                 domain,
55                 f"/api/v1/server/{mode}?start={start}&count=100",
56                 headers,
57                 (config.get("connection_timeout"), config.get("read_timeout"))
58             )
59
60             logger.debug("data[]='%s'", type(data))
61             if "error_message" not in data:
62                 logger.debug("Success, data[json]()=%d", len(data['json']))
63                 instances.set_success(domain)
64
65                 if "data" in data["json"]:
66                     rows = data["json"]["data"]
67
68                     logger.debug("Found %d record(s).", len(rows))
69                     for record in rows:
70                         logger.debug("record()=%d", len(record))
71                         for mode2 in ["follower", "following" ]:
72                             logger.debug("mode2='%s'", mode2)
73                             if mode2 in record and "host" in record[mode2]:
74                                 logger.debug("Found mode2='%s',host='%s', adding ...", mode2, record[mode2]['host'])
75                                 peers.append(record[mode2]["host"])
76                             else:
77                                 logger.warning("Record from domain='%s' has no mode2='%s' or 'host' record[]='%s", domain, mode2, type(record))
78
79                     if len(rows) < 100:
80                         logger.debug("Reached end of JSON response, domain='%s'", domain)
81                         break
82
83                 # Continue with next row
84                 start = start + 100
85             else:
86                 logger.warning("domain='%s' causes error during API query: '%s' - SKIPPED!", domain, data['error_message'])
87                 break
88
89     logger.debug("Returning peers[]='%s' - EXIT!", type(peers))
90     return peers