-def send_bot_post(instance: str, blocklist: dict):
- # DEBUG: print(f"DEBUG: instance={instance},blocklist()={len(blocklist)} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
- elif domain == "":
- raise ValueError("Parameter 'domain' is empty")
- elif type(blocklist) != dict:
- raise ValueError(f"Parameter blocklist[]='{type(blocklist)}' is not 'dict'")
-
- message = instance + " has blocked the following instances:\n\n"
- truncated = False
-
- if len(blocklist) > 20:
- truncated = True
- blocklist = blocklist[0 : 19]
-
- # DEBUG: print(f"DEBUG: blocklist()={len(blocklist)}")
- for block in blocklist:
- # DEBUG: print(f"DEBUG: block['{type(block)}']={block}")
- if block["reason"] == None or block["reason"] == '':
- message = message + block["blocked"] + " with unspecified reason\n"
- else:
- if len(block["reason"]) > 420:
- block["reason"] = block["reason"][0:419] + "[…]"
-
- message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
-
- if truncated:
- message = message + "(the list has been truncated to the first 20 entries)"
-
- botheaders = {**api_headers, **{"Authorization": "Bearer " + config.get("bot_token")}}
-
- req = reqto.post(
- f"{config.get('bot_instance')}/api/v1/statuses",
- data={
- "status" : message,
- "visibility" : config.get('bot_visibility'),
- "content_type": "text/plain"
- },
- headers=botheaders,
- timeout=10
- ).json()
-
- return True
-
-def fetch_friendica_blocks(domain: str) -> dict:
- # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
- elif domain == "":
- raise ValueError(f"Parameter 'domain' is empty")
-
- # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
- blocked = list()
-
- try:
- doc = bs4.BeautifulSoup(
- get_response(domain, "/friendica", headers, (config.get("connection_timeout"), config.get("read_timeout"))).text,
- "html.parser",
- )
- except BaseException as e:
- print("WARNING: Failed to fetch /friendica from domain:", domain, e)
- instances.update_last_error(domain, e)
- return {}
-
- blocklist = doc.find(id="about_blocklist")
-
- # Prevents exceptions:
- if blocklist is None:
- # DEBUG: print("DEBUG: Instance has no block list:", domain)
- return {}
-
- table = blocklist.find("table")
-
- # DEBUG: print(f"DEBUG: table[]='{type(table)}'")
- if table.find("tbody"):
- rows = table.find("tbody").find_all("tr")
- else:
- rows = table.find_all("tr")
-
- # DEBUG: print(f"DEBUG: Found rows()={len(rows)}")
- for line in rows:
- # DEBUG: print(f"DEBUG: line='{line}'")
- blocked.append({
- "domain": tidyup_domain(line.find_all("td")[0].text),
- "reason": tidyup_reason(line.find_all("td")[1].text)
- })
- # DEBUG: print("DEBUG: Next!")
-
- # DEBUG: print("DEBUG: Returning blocklist() for domain:", domain, len(blocklist))
- return {
- "reject": blocked
- }
-