]> git.mxchange.org Git - fba.git/blob - fba/networks/friendica.py
f723c710f01a0368c21e93ec672b5408ada5eea3
[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 logging
18
19 import bs4
20
21 from fba.helpers import blacklist
22 from fba.helpers import config
23 from fba.helpers import domain as domain_helper
24 from fba.helpers import tidyup
25
26 from fba.http import network
27
28 from fba.models import instances
29
30 logging.basicConfig(level=logging.INFO)
31 logger = logging.getLogger(__name__)
32 #logger.setLevel(logging.DEBUG)
33
34 def fetch_blocks(domain: str) -> list:
35     logger.debug("domain='%s' - CALLED!", domain)
36     domain_helper.raise_on(domain)
37
38     if blacklist.is_blacklisted(domain):
39         raise Exception(f"domain='{domain}' is blacklisted but function is invoked.")
40     elif not instances.is_registered(domain):
41         raise Exception(f"domain='{domain}' is not registered but function is invoked.")
42
43     blocklist = list()
44     block_tag = None
45
46     try:
47         logger.debug("Fetching friendica blocks from domain='%s'", domain)
48         raw = network.fetch_response(
49             domain,
50             "/friendica",
51             network.web_headers,
52             (config.get("connection_timeout"), config.get("read_timeout"))
53         ).text
54         logger.debug("Parsing %d Bytes ...", len(raw))
55
56         doc = bs4.BeautifulSoup(raw, "html.parser",)
57         logger.debug("doc[]='%s'", type(doc))
58
59         block_tag = doc.find(id="about_blocklist")
60         logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag)
61     except network.exceptions as exception:
62         logger.warning("Exception '%s' during fetching instances from domain='%s'", type(exception), domain)
63         instances.set_last_error(domain, exception)
64
65         logger.debug("Returning empty list ... - EXIT!")
66         return list()
67
68     logger.debug("block_tag[%s]='%s'", type(block_tag), block_tag)
69     if block_tag is None:
70         logger.debug("Instance has no block list: domain='%s' - EXIT!", domain)
71         return list()
72
73     table = block_tag.find("table")
74
75     logger.debug("table[]='%s'", type(table))
76     if table is None:
77         logger.warning("domain='%s' has no table tag - EXIT !", domain)
78         return list()
79     elif table.find("tbody"):
80         rows = table.find("tbody").find_all("tr")
81     else:
82         rows = table.find_all("tr")
83
84     logger.debug("Found rows()=%d", len(rows))
85     for line in rows:
86         logger.debug("line='%s'", line)
87         blocked = line.find_all("td")[0].text
88         logger.debug("blocked='%s'", blocked)
89
90         blocked = tidyup.domain(blocked) if blocked != "" else None
91         reason  = tidyup.reason(line.find_all("td")[1].text)
92         logger.debug("blocked='%s',reason='%s' - AFTER!", blocked, reason)
93
94         if blocked in [None, ""]:
95             logger.warning("line[]='%s' returned empty blocked domain - SKIPPED!", type(line))
96             continue
97         elif not domain_helper.is_wanted(blocked):
98             logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
99             continue
100
101         logger.debug("Appending blocked='%s',reason='%s'", blocked, reason)
102         blocklist.append({
103             "blocker"    : domain,
104             "blocked"    : blocked,
105             "reason"     : reason,
106             "block_level": "reject",
107         })
108
109     logger.debug("blocklist()=%d - EXIT!", len(blocklist))
110     return blocklist