Myles’ Life Vercel Redirects

python
vercel
Author

Myles Braithwaite

Published

December 20, 2022

I recently change the domain for my Microblog from myles.life to myles.social and need to redirect the older URLs using Vercel.

import datetime
from pathlib import Path
import json
from urllib.parse import urlparse
from lazymyles.download_file import download_file
ROOT_PATH = Path.cwd()
DATA_PATH = ROOT_PATH / "data"
SOURCE_PATH = DATA_PATH / "source"
OUTPUT_PATH = DATA_PATH / "output"
FEED_JSON_PATH = download_file(
    "https://raw.githubusercontent.com/myles/myles.social/master/feed.json",
    output_path=SOURCE_PATH,
    file_name=f"feed-{datetime.date.today()}.json",
)
with FEED_JSON_PATH.open() as file_obj:
    raw_feed_json = file_obj.read()

feed_json = json.loads(raw_feed_json)
feed_json["title"]
'myles.social'
vercel_config = {}
def feed_json_item_to_vercel_redirect(item: dict) -> dict:
    url = urlparse(item["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}
vercel_config["redirects"] = [
    feed_json_item_to_vercel_redirect(item) for item in feed_json["items"]
]
raw_vercel_config = json.dumps(vercel_config, sort_keys=True, indent=4)

VERCEL_CONFIG_PATH = OUTPUT_PATH / "vercel.json"

with VERCEL_CONFIG_PATH.open("w+") as file_obj:
    file_obj.write(raw_vercel_config)

Made by Myles Braithwaite with ❤️ in Toronto.