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