gemini/blog/index.gen.py

65 lines
2.1 KiB
Python
Raw Normal View History

2021-02-28 10:13:45 +01:00
#!/usr/bin/env python
2024-02-15 21:12:22 +01:00
'''fetch (almost) all blog posts from Dotclear database and export them in Markdown format
TODO: use rss/atom feed instead
TODO: convert to proper Gemtext'''
2021-02-28 10:13:45 +01:00
import os
2024-02-15 21:12:22 +01:00
import re
2021-02-28 10:13:45 +01:00
from urllib.parse import quote
2024-02-15 21:12:22 +01:00
import html2markdown
import mysql.connector
2021-02-28 10:13:45 +01:00
2024-02-15 21:12:22 +01:00
path_regex = re.compile('\\d+/\\d+/\\d+')
date_regex = re.compile('\\d+-\\d+-\\d+')
filename_regex = re.compile('\\d+/\\d+/\\d+/(.*)')
2021-02-28 10:13:45 +01:00
db_password_regex = re.compile("'DC_DBPASSWORD','(.*)'")
db_host_regex = re.compile("'DC_DBHOST','(.*)'")
db_name_regex = re.compile("'DC_DBNAME','(.*)'")
db_user_regex = re.compile("'DC_DBUSER','(.*)'")
2024-02-15 21:12:22 +01:00
with open("/home/www/dotclear/inc/config.php", "r", encoding="utf-8") as f:
c = f.read()
2021-02-28 10:13:45 +01:00
mydb = mysql.connector.connect(
host=db_host_regex.findall(c)[0],
user=db_user_regex.findall(c)[0],
password=db_password_regex.findall(c)[0],
database=db_name_regex.findall(c)[0]
)
mycursor = mydb.cursor()
2024-02-15 21:12:22 +01:00
mycursor.execute("SELECT post_url, post_title, post_content_xhtml \
FROM dc_post WHERE post_url LIKE '%/%/%/%' ORDER BY post_id DESC")
2021-02-28 10:13:45 +01:00
myresult = mycursor.fetchall()
2022-02-20 14:36:24 +01:00
mydb.close()
2021-02-28 10:13:45 +01:00
2024-02-15 21:12:22 +01:00
with open("index.gmi", "w", encoding="utf-8") as f:
f.write("# Tourmentine's blog-to-gemlog\n\n")
2021-02-28 10:13:45 +01:00
for x in myresult:
path = path_regex.findall(x[0])[0]
2024-02-15 21:12:22 +01:00
path_with_dashes = path.replace("/","-")
2024-07-20 19:02:19 +02:00
title = filename_regex.findall(x[0])[0]
filename = re.sub(r'\W+','', title)
2021-02-28 10:13:45 +01:00
2024-02-15 21:12:22 +01:00
with open("index.gmi", "a", encoding="utf-8") as f:
f.write(f'=> {path}/{filename}.gmi {path_with_dashes} - {x[1]}\n')
2021-02-28 10:13:45 +01:00
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError:
2024-02-15 21:12:22 +01:00
print (f"Creation of the directory {path} failed")
2024-07-20 19:02:19 +02:00
if not os.path.isfile(f'{path}/{filename}.gmi'):
print(f"creating {path}/{filename}.gmi")
with open(f'{path}/{filename}.gmi', "w", encoding="utf-8") as f:
2024-02-15 21:12:22 +01:00
f.write(f"# {x[1]}\n\n")
f.write(f"Publié le {path}\n\n")
f.write(html2markdown.convert(x[2]))
f.write("\n\n=> /blog/ Retour au menu du blog")
with open("index.gmi", "a", encoding="utf-8") as f:
f.write("\n=> Retour")