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