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