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