]> git.mxchange.org Git - fba.git/blob - fba/networks/friendica.py
Continued:
[fba.git] / fba / networks / friendica.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 bs4
18
19 from fba import config
20 from fba import network
21
22 from fba.helpers import tidyup
23
24 from fba.models import instances
25
26 def fetch_blocks(domain: str) -> dict:
27     # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
28     if not isinstance(domain, str):
29         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
30     elif domain == "":
31         raise ValueError("Parameter 'domain' is empty")
32
33     blocked = list()
34     blocklist = None
35
36     try:
37         # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
38         doc = bs4.BeautifulSoup(
39             network.fetch_response(
40                 domain,
41                 "/friendica",
42                 network.web_headers,
43                 (config.get("connection_timeout"), config.get("read_timeout"))
44             ).text,
45             "html.parser",
46         )
47         # DEBUG: print(f"DEBUG: doc[]='{type(doc)}'")
48
49         blocklist = doc.find(id="about_blocklist")
50     except network.exceptions as exception:
51         print(f"WARNING: Exception '{type(exception)}' during fetching instances (friendica) from domain='{domain}'")
52         instances.update_last_error(domain, exception)
53
54         if instances.has_pending(domain):
55             instances.update_data(domain)
56
57         return dict()
58
59     # Prevents exceptions:
60     if blocklist is None:
61         # DEBUG: print("DEBUG: Instance has no block list:", domain)
62         return dict()
63
64     table = blocklist.find("table")
65
66     # DEBUG: print(f"DEBUG: table[]='{type(table)}'")
67     if table.find("tbody"):
68         rows = table.find("tbody").find_all("tr")
69     else:
70         rows = table.find_all("tr")
71
72     # DEBUG: print(f"DEBUG: Found rows()={len(rows)}")
73     for line in rows:
74         # DEBUG: print(f"DEBUG: line='{line}'")
75         blocked.append({
76             "domain": tidyup.domain(line.find_all("td")[0].text),
77             "reason": tidyup.reason(line.find_all("td")[1].text)
78         })
79         # DEBUG: print("DEBUG: Next!")
80
81     # DEBUG: print("DEBUG: Returning blocklist() for domain:", domain, len(blocklist))
82     return {
83         "reject": blocked
84     }