2023-04-21 09:51:44 +02:00
|
|
|
#!python3
|
2023-04-14 10:27:02 +02:00
|
|
|
import os
|
2023-04-20 17:02:14 +02:00
|
|
|
import shutil
|
2023-04-20 17:22:03 +02:00
|
|
|
import sys
|
2023-04-21 09:51:44 +02:00
|
|
|
from pprint import pprint
|
|
|
|
# from peewee import *
|
2023-04-20 17:02:14 +02:00
|
|
|
import pymysql
|
2023-04-21 09:51:44 +02:00
|
|
|
from slugify import slugify
|
2023-04-14 10:27:02 +02:00
|
|
|
import yaml
|
|
|
|
|
2023-04-20 17:22:03 +02:00
|
|
|
outputDir = "output"
|
2023-04-20 17:02:14 +02:00
|
|
|
# Clean the output dir & create a new
|
2023-04-20 17:22:03 +02:00
|
|
|
shutil.rmtree(outputDir, True)
|
|
|
|
os.mkdir(outputDir)
|
2023-04-20 17:02:14 +02:00
|
|
|
|
2023-04-14 10:27:02 +02:00
|
|
|
# Connect to the MySQL database
|
|
|
|
db = pymysql.connect(
|
2023-04-20 17:22:21 +02:00
|
|
|
host="localhost",
|
|
|
|
db="spip",
|
|
|
|
user="spip",
|
|
|
|
password="password",
|
2023-04-14 10:27:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Query the database to retrieve all data
|
|
|
|
cursor = db.cursor()
|
2023-04-20 17:22:21 +02:00
|
|
|
cursor.execute("SELECT * FROM spip_articles ORDER BY date DESC")
|
2023-04-14 10:27:02 +02:00
|
|
|
|
|
|
|
# Loop through the results and format data into Markdown files
|
2023-04-17 12:06:27 +02:00
|
|
|
# Columns:
|
|
|
|
# 2: titre
|
|
|
|
# 7: texte
|
|
|
|
# 9: date
|
2023-04-20 17:22:03 +02:00
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
if int(sys.argv[1]) > 0:
|
2023-04-21 09:51:44 +02:00
|
|
|
fetch = cursor.fetchmany(int(sys.argv[1]))
|
2023-04-20 17:22:03 +02:00
|
|
|
else:
|
|
|
|
fetch = cursor.fetchall()
|
|
|
|
else:
|
2023-04-21 09:51:44 +02:00
|
|
|
fetch = cursor.fetchmany(3)
|
|
|
|
print("--- {} articles will be converted to Markdown files wihth YAML metadata ---\n".format(len(fetch)))
|
|
|
|
pprint(fetch)
|
2023-04-20 17:22:03 +02:00
|
|
|
|
|
|
|
for row in fetch:
|
2023-04-20 17:22:21 +02:00
|
|
|
frontmatter = {"title": row[2], "date": row[9]}
|
2023-04-21 09:51:44 +02:00
|
|
|
content = "---\n{}---\n{}\n\n{}".format(yaml.dump(frontmatter), row[2], row[7])
|
|
|
|
path = "{}/{}.md".format(outputDir, slugify("{}-{}".format(row[0], row[2])))
|
2023-04-20 17:22:21 +02:00
|
|
|
with open(path, "w") as f:
|
2023-04-17 12:06:27 +02:00
|
|
|
f.write(content)
|
2023-04-14 10:27:02 +02:00
|
|
|
|
|
|
|
# Close the database connection
|
|
|
|
db.close()
|
2023-04-21 09:51:44 +02:00
|
|
|
|
|
|
|
# Announce the end of the script
|
|
|
|
print("\n--- End of script ---")
|