Select Git revision
-
Nadja Geisler authoredNadja Geisler authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
utils.py 3.15 KiB
import json
import logging
import os
import textwrap
from io import TextIOWrapper
from typing import Optional
import requests
logger = logging.getLogger("shortlinks.utils")
def export_vanity_map_json(out_path: str, link_mappings: dict[str, str]) -> None:
serialized_map = json.dumps(link_mappings)
os.makedirs(out_path, exist_ok=True)
with open(os.path.join(out_path, "vanitymap.json"), "w") as file:
file.write(serialized_map)
def check_url(url: str, ignore_list: list[str]) -> bool:
try:
r = requests.get(url, timeout=30)
if not r.ok and url not in ignore_list:
logger.warning(f"Unexpected HTTP status {r.status_code} for URL {url}")
return False
return True
except requests.exceptions.RequestException as e:
logger.warning(f"Encountered {e} for URL {url}")
return False
def read_file(file: TextIOWrapper) -> dict[str, str]:
_link_mappings = {}
for line in file:
if line.startswith("#") or len(line.strip()) == 0:
continue
_path, _url, *_ = line.split()
if _path in _link_mappings:
raise ValueError(f"Redirect target for {_path} already defined")
_link_mappings[_path] = _url
return _link_mappings
def flatten_links(link_mappings: dict[str, str]) -> dict[str, Optional[str]]:
_flat_mappings: dict[str, Optional[str]] = link_mappings.copy() # type: ignore
_remaining_rounds = 10
while _remaining_rounds != 0:
found = False
for key, value in _flat_mappings.items():
if value and value.startswith("/"):
found = True
logger.info(f"Detected internal link: {key} -> {value}")
if value not in _flat_mappings.keys():
logger.error(f"Target {value} not found!")
_flat_mappings[key] = None
else:
logger.info(f"Replacing {value} with {_flat_mappings[value]}")
_flat_mappings[key] = _flat_mappings[value]
_remaining_rounds = _remaining_rounds - 1
if not found:
logger.info("No (remaining) internal links found, quitting loop")
_remaining_rounds = 0
return _flat_mappings
def write_redirect_html(out_path: str, src_link: str, target_url: str) -> None:
_out_path = os.path.join(out_path, src_link.lstrip(os.path.sep), "index.html")
os.makedirs(os.path.dirname(_out_path), exist_ok=True)
with open(_out_path, "w") as file:
content = f"""\
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Du wirst weitergeleitet…</title>
<link rel="canonical" href="{target_url}">
<script>location="{target_url}"</script>
<meta http-equiv="refresh" content="0; url={target_url}">
<meta name="robots" content="noindex">
</head>
<body>
<h1>Du wirst weitergeleitet…</h1>
Du wirst zu <a href="{target_url}">{target_url}</a> weitergeleitet.
</body>
</html>
"""
file.write(textwrap.dedent(content))