microblog.pub/app/utils/privacy_replace.py

40 lines
974 B
Python
Raw Permalink Normal View History

2022-08-04 12:13:32 -05:00
from urllib.parse import urlparse
from bs4 import BeautifulSoup # type: ignore
from loguru import logger
from app.config import PRIVACY_REPLACE
def replace_content(content: str) -> str:
if not PRIVACY_REPLACE:
return content
soup = BeautifulSoup(content, "html5lib")
links = list(soup.find_all("a", href=True))
if not links:
return content
for link in links:
link.attrs["href"] = replace_url(link.attrs["href"])
return soup.find("body").decode_contents()
def replace_url(u: str) -> str:
if not PRIVACY_REPLACE:
return u
try:
parsed_href = urlparse(u)
2022-08-15 03:27:58 -05:00
if not parsed_href.hostname:
raise ValueError("Missing hostname")
2022-08-04 12:13:32 -05:00
except Exception:
logger.warning(f"Failed to parse url={u}")
return u
2022-08-15 03:27:58 -05:00
if new_netloc := PRIVACY_REPLACE.get(parsed_href.hostname.removeprefix("www.")):
2022-08-04 12:13:32 -05:00
return parsed_href._replace(netloc=new_netloc).geturl()
return u