From 96002f70b0ed1ccd14fd09f6fe33d45091524c1e Mon Sep 17 00:00:00 2001 From: n Date: Sun, 28 Feb 2021 10:13:45 +0100 Subject: [PATCH 1/5] first try at gemlog --- .gitignore | 2 ++ blog/index.gen.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ index.gmi | 3 ++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100755 blog/index.gen.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..78ea454 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +!blog/index.gen.py +blog/ diff --git a/blog/index.gen.py b/blog/index.gen.py new file mode 100755 index 0000000..d3ac4b9 --- /dev/null +++ b/blog/index.gen.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +''' 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 ''' + +import html2markdown +import mysql.connector +import re +import os +from urllib.parse import quote + +path_regex = re.compile('\d+/\d+/\d+') +date_regex = re.compile('\d+-\d+-\d+') +filename_regex = re.compile('\d+/\d+/\d+/(.*)') + +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','(.*)'") + +f = open("/home/www/dotclear/inc/config.php", "r") +c = f.read() +f.close() + +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() +mycursor.execute("SELECT post_url, post_title, post_content_xhtml FROM dc_post WHERE post_url LIKE '%/%/%/%' ORDER BY post_id DESC") +myresult = mycursor.fetchall() + +f = open("index.gmi", "w") +f.close() + +for x in myresult: + path = path_regex.findall(x[0])[0] + filename = filename_regex.findall(x[0])[0] + + f = open("index.gmi", "a") + f.write("=> %s/%s.gmi %s %s\n" % (path, filename, path, x[1])) + f.close() + + try: + if not os.path.exists(path): + os.makedirs(path) + except OSError: + print ("Creation of the directory %s failed" % path) + + print("creating %s/%s.gmi" % (path,quote(filename))) + f = open("%s/%s.gmi" % (path,quote(filename)), "w") + f.write("# %s\n\n" % x[1]) + f.write("Publié le %s\n\n" % path) + f.write(html2markdown.convert(x[2])) + f.write("\n\n=> /blog/ Retour au menu du blog") + f.close() + diff --git a/index.gmi b/index.gmi index 81bab34..286c9d3 100644 --- a/index.gmi +++ b/index.gmi @@ -2,4 +2,5 @@ Have a sit and enjoy the following content: -=> freebsd.gmi Installing vger on FreeBSD, using inetd and nginx +=> freebsd.gmi howto: installing vger on FreeBSD, using inetd and nginx +=> blog/ a markdown export of my www blog (experimental, buggy and furthermore, in French) From c5876330f3f722b48a6814f8d4b642989773c473 Mon Sep 17 00:00:00 2001 From: n Date: Mon, 8 Mar 2021 12:32:51 +0100 Subject: [PATCH 2/5] gemini://gemini.circumlunar.space/docs/companion/subscription.gmi --- blog/index.gen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blog/index.gen.py b/blog/index.gen.py index d3ac4b9..ae1eff5 100755 --- a/blog/index.gen.py +++ b/blog/index.gen.py @@ -41,7 +41,7 @@ for x in myresult: filename = filename_regex.findall(x[0])[0] f = open("index.gmi", "a") - f.write("=> %s/%s.gmi %s %s\n" % (path, filename, path, x[1])) + f.write("=> %s/%s.gmi %s - %s\n" % (path, filename, path.replace("/","-"), x[1])) f.close() try: From 0d5744900442d32d9e482272b5d3887fd53f3e4c Mon Sep 17 00:00:00 2001 From: n Date: Mon, 8 Mar 2021 13:47:52 +0100 Subject: [PATCH 3/5] header&footer --- blog/index.gen.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/blog/index.gen.py b/blog/index.gen.py index ae1eff5..155af01 100755 --- a/blog/index.gen.py +++ b/blog/index.gen.py @@ -34,6 +34,7 @@ mycursor.execute("SELECT post_url, post_title, post_content_xhtml FROM dc_post W myresult = mycursor.fetchall() f = open("index.gmi", "w") +f.write("# Tourmentine's blog-to-gemlog\n\n"); f.close() for x in myresult: @@ -58,3 +59,6 @@ for x in myresult: f.write("\n\n=> /blog/ Retour au menu du blog") f.close() +f = open("index.gmi", "a") +f.write("\n=> Retour") +f.close() From 753cdc5ce30af988dda2102b6f4e33ec0df67b97 Mon Sep 17 00:00:00 2001 From: n Date: Mon, 8 Mar 2021 13:55:49 +0100 Subject: [PATCH 4/5] do not overwrite existing file(s) --- blog/index.gen.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/blog/index.gen.py b/blog/index.gen.py index 155af01..87675a3 100755 --- a/blog/index.gen.py +++ b/blog/index.gen.py @@ -51,13 +51,14 @@ for x in myresult: except OSError: print ("Creation of the directory %s failed" % path) - print("creating %s/%s.gmi" % (path,quote(filename))) - f = open("%s/%s.gmi" % (path,quote(filename)), "w") - f.write("# %s\n\n" % x[1]) - f.write("Publié le %s\n\n" % path) - f.write(html2markdown.convert(x[2])) - f.write("\n\n=> /blog/ Retour au menu du blog") - f.close() + if not os.path.isfile('%s/%s.gmi' % (path,quote(filename))): + print("creating %s/%s.gmi" % (path,quote(filename))) + f = open("%s/%s.gmi" % (path,quote(filename)), "w") + f.write("# %s\n\n" % x[1]) + f.write("Publié le %s\n\n" % path) + f.write(html2markdown.convert(x[2])) + f.write("\n\n=> /blog/ Retour au menu du blog") + f.close() f = open("index.gmi", "a") f.write("\n=> Retour") From 6c37955a4d5faac523603371583f14d99de376aa Mon Sep 17 00:00:00 2001 From: n Date: Mon, 8 Mar 2021 22:59:04 +0100 Subject: [PATCH 5/5] eat tofu --- freebsd.gmi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/freebsd.gmi b/freebsd.gmi index eef5df2..d71b995 100644 --- a/freebsd.gmi +++ b/freebsd.gmi @@ -103,7 +103,7 @@ The following miniamlistic script can be used to check for capsule availability ``` #!/bin/sh -errorOutput=$(echo -n "gemini://$1/\r\n" | /path/to/gnutls-cli -p 1965 $1 2>&1 > /dev/null) +errorOutput=$(echo -n "gemini://$1/\r\n" | /path/to/gnutls-cli --port 1965 --tofu $1 2>&1 > /dev/null) errorCode=$? if [ $errorCode -gt 0 ] @@ -116,6 +116,8 @@ else fi ``` +Here we use "trust on first use authentication" (--tofu), so don't forget to manually launch gnutls-cli first. And if security is not your primary concern, you can use the more permissive --no-ca-verification flag instead. + # Greetings Many many thanks to @solene@bsd.network for writing that wonderful little piece of software thas is vger, and @hucste@framapiaf.org for pointing it to me.