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