fix articles with the same name merged & slugification & pretty print of data prior to save files

This commit is contained in:
Guilhem Fauré 2023-04-21 07:51:44 +00:00
parent 6c880668c7
commit 88b448eb3c

18
main.py Normal file → Executable file
View File

@ -1,8 +1,11 @@
#!python3
import os
import shutil
import sys
from pprint import pprint
# from peewee import *
import pymysql
from peewee import *
from slugify import slugify
import yaml
outputDir = "output"
@ -30,18 +33,23 @@ cursor.execute("SELECT * FROM spip_articles ORDER BY date DESC")
if len(sys.argv) > 1:
if int(sys.argv[1]) > 0:
fetch = cursor.fetchmany(int(sys.argv[1]) + 1)
fetch = cursor.fetchmany(int(sys.argv[1]))
else:
fetch = cursor.fetchall()
else:
fetch = cursor.fetchmany(3 + 1)
fetch = cursor.fetchmany(3)
print("--- {} articles will be converted to Markdown files wihth YAML metadata ---\n".format(len(fetch)))
pprint(fetch)
for row in fetch:
frontmatter = {"title": row[2], "date": row[9]}
content = f"---\n{yaml.dump(frontmatter)}---\n{row[2]}\n\n{row[7]}"
path = f"{outputDir}/{row[2]}.md"
content = "---\n{}---\n{}\n\n{}".format(yaml.dump(frontmatter), row[2], row[7])
path = "{}/{}.md".format(outputDir, slugify("{}-{}".format(row[0], row[2])))
with open(path, "w") as f:
f.write(content)
# Close the database connection
db.close()
# Announce the end of the script
print("\n--- End of script ---")