fix articles with the same name merged & slugification & pretty print of data prior to save files
This commit is contained in:
parent
6c880668c7
commit
88b448eb3c
18
main.py
Normal file → Executable file
18
main.py
Normal file → Executable file
@ -1,8 +1,11 @@
|
|||||||
|
#!python3
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
from pprint import pprint
|
||||||
|
# from peewee import *
|
||||||
import pymysql
|
import pymysql
|
||||||
from peewee import *
|
from slugify import slugify
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
outputDir = "output"
|
outputDir = "output"
|
||||||
@ -30,18 +33,23 @@ cursor.execute("SELECT * FROM spip_articles ORDER BY date DESC")
|
|||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
if int(sys.argv[1]) > 0:
|
if int(sys.argv[1]) > 0:
|
||||||
fetch = cursor.fetchmany(int(sys.argv[1]) + 1)
|
fetch = cursor.fetchmany(int(sys.argv[1]))
|
||||||
else:
|
else:
|
||||||
fetch = cursor.fetchall()
|
fetch = cursor.fetchall()
|
||||||
else:
|
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:
|
for row in fetch:
|
||||||
frontmatter = {"title": row[2], "date": row[9]}
|
frontmatter = {"title": row[2], "date": row[9]}
|
||||||
content = f"---\n{yaml.dump(frontmatter)}---\n{row[2]}\n\n{row[7]}"
|
content = "---\n{}---\n{}\n\n{}".format(yaml.dump(frontmatter), row[2], row[7])
|
||||||
path = f"{outputDir}/{row[2]}.md"
|
path = "{}/{}.md".format(outputDir, slugify("{}-{}".format(row[0], row[2])))
|
||||||
with open(path, "w") as f:
|
with open(path, "w") as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
# Close the database connection
|
# Close the database connection
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
# Announce the end of the script
|
||||||
|
print("\n--- End of script ---")
|
||||||
|
Loading…
Reference in New Issue
Block a user