import datetime
from pathlib import Path
import json
from urllib.parse import urlparse
from lazymyles.download_file import download_file
Myles’ Life Vercel Redirects
python
vercel
I recently change the domain for my Microblog from myles.life
to myles.social
and need to redirect the older URLs using Vercel.
= Path.cwd()
ROOT_PATH = ROOT_PATH / "data"
DATA_PATH = DATA_PATH / "source"
SOURCE_PATH = DATA_PATH / "output" OUTPUT_PATH
= download_file(
FEED_JSON_PATH "https://raw.githubusercontent.com/myles/myles.social/master/feed.json",
=SOURCE_PATH,
output_path=f"feed-{datetime.date.today()}.json",
file_name )
with FEED_JSON_PATH.open() as file_obj:
= file_obj.read()
raw_feed_json
= json.loads(raw_feed_json)
feed_json "title"] feed_json[
'myles.social'
= {} vercel_config
def feed_json_item_to_vercel_redirect(item: dict) -> dict:
= urlparse(item["url"])
url return {
"source": url.path,
"destination": f"https://myles.social{url.path}",
"permanent": True,
}
feed_json_item_to_vercel_redirect("url": "https://myles.life/2022/11/01/204923.html"}
{ )
{'source': '/2022/11/01/204923.html',
'destination': 'https://myles.social/2022/11/01/204923.html',
'permanent': True}
"redirects"] = [
vercel_config[for item in feed_json["items"]
feed_json_item_to_vercel_redirect(item) ]
= json.dumps(vercel_config, sort_keys=True, indent=4)
raw_vercel_config
= OUTPUT_PATH / "vercel.json"
VERCEL_CONFIG_PATH
with VERCEL_CONFIG_PATH.open("w+") as file_obj:
file_obj.write(raw_vercel_config)
Made by Myles Braithwaite with ❤️ in Toronto.