parametric number of output articles

This commit is contained in:
Guilhem Fauré 2023-04-20 15:22:03 +00:00
parent dbf7a53cde
commit 7e1a4cfdd7

25
main.py
View File

@ -1,11 +1,14 @@
import os
import shutil
import sys
import pymysql
from peewee import *
import yaml
outputDir = "output"
# Clean the output dir & create a new
shutil.rmtree("output/", true)
os.mkdir("output/", 777)
shutil.rmtree(outputDir, True)
os.mkdir(outputDir)
# Connect to the MySQL database
db = pymysql.connect(
@ -17,18 +20,26 @@ db = pymysql.connect(
# Query the database to retrieve all data
cursor = db.cursor()
cursor.execute('SELECT * FROM spip_articles')
cursor.execute('SELECT * FROM spip_articles ORDER BY date DESC')
# Loop through the results and format data into Markdown files
# Columns:
# 2: titre
# 7: texte
# 9: date
# for row in cursor.fetchall():
for row in cursor.fetchmany(5):
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:
frontmatter = {'title': row[2], 'date': row[9]}
content = f'---\n{yaml.dump(frontmatter)}---\n{row[7]}'
path = f'markdown/{row[2]}.md'
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:
f.write(content)