spip2md/main.py

48 lines
1.0 KiB
Python
Raw Normal View History

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-20 17:02:14 +02:00
import pymysql
2023-04-20 17:22:03 +02:00
from peewee import *
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:
fetch = cursor.fetchmany(int(sys.argv[1]) + 1)
else:
fetch = cursor.fetchall()
else:
fetch = cursor.fetchmany(3 + 1)
for row in fetch:
2023-04-20 17:22:21 +02:00
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"
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()