linkedin-posts-scraper/propagate_title_change_to_slug.py

34 lines
803 B
Python
Raw Normal View History

2023-02-19 11:10:12 +00:00
# this script will look for each markdown with YAML frontmatter header and will rename the file name to the slug from the title in the header
from slugify import slugify
# https://pypi.org/project/python-frontmatter/
import os
import pyaml
import json
import frontmatter
INP_DIR = "./janco_website/true_content/posts"
from pathlib import Path
renamed = 0
for path in Path(INP_DIR).rglob('*.md'):
print(path)
post = frontmatter.load(path)
if post['title'].startswith("CHANGE_ME"):
continue
new_name = slugify.slugify(post['title']) + '.md'
if new_name == path:
print("same")
continue
print("rename")
os.rename(path, path.parent / ('/' + new_name))
renamed += 1
# path, slugify.slugify(post['title'])
print(f"{renamed} files renamed")