commit 4cc32d32001d951001d335aa258d5af9cb9b48f4 Author: deadvey Date: Mon Mar 24 18:08:06 2025 +0000 initial diff --git a/.create_pages.py.swp b/.create_pages.py.swp new file mode 100644 index 0000000..a59ea52 Binary files /dev/null and b/.create_pages.py.swp differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4acd06b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.py diff --git a/README.md b/README.md new file mode 100755 index 0000000..abede60 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +I've written a basic program in python called glogger, it is a customisable, statically generated gemini blogging frontend, it's being used right now. It works by having two mysql tables called " "users" which contains just two fields: userID and username, and "posts" which contains 6 fields: postID (PK), userID (FK), title, content, pubData and editDate, whenever you create a new post, it will add a new record to the posts table and write that post to the gemini frontend. It has a few options in the configuration, to allow for differing formatting to change how the whole thing is generated. I am wanting to add a better text input field instead of the basic python() input, probably consisting of a vim buffer that gets sent to the python program, that could be great! I also want to add passwords for different users and modification of previous posts. + +# Setup +## Requirements: +mysql-connector-python +mariadb-client +mariadb-server +## Database setup +``` CREATE DATABASE glogger; +USE glogger; +CREATE TABLE users ( userID INT PRIMARY KEY AUTO_INCREMENT NOT NULL, username VARCHAR(255) ); +CREATE TABLE posts ( postID INT PRIMARY KEY AUTO_INCREMENT NOT NULL, userID INT, FOREIGN KEY(userID) REFERENCES users(userID), title VARCHAR(255), content TEXT, pubDate VARCHAR(255), editDate VARCHAR(255) ); +``` diff --git a/__pycache__/config.cpython-311.pyc b/__pycache__/config.cpython-311.pyc new file mode 100755 index 0000000..9c56a01 Binary files /dev/null and b/__pycache__/config.cpython-311.pyc differ diff --git a/__pycache__/config.cpython-313.pyc b/__pycache__/config.cpython-313.pyc new file mode 100644 index 0000000..1411d93 Binary files /dev/null and b/__pycache__/config.cpython-313.pyc differ diff --git a/__pycache__/create_pages.cpython-311.pyc b/__pycache__/create_pages.cpython-311.pyc new file mode 100755 index 0000000..06aaa98 Binary files /dev/null and b/__pycache__/create_pages.cpython-311.pyc differ diff --git a/__pycache__/create_pages.cpython-313.pyc b/__pycache__/create_pages.cpython-313.pyc new file mode 100644 index 0000000..8eb9054 Binary files /dev/null and b/__pycache__/create_pages.cpython-313.pyc differ diff --git a/__pycache__/newpost.cpython-311.pyc b/__pycache__/newpost.cpython-311.pyc new file mode 100755 index 0000000..64255b7 Binary files /dev/null and b/__pycache__/newpost.cpython-311.pyc differ diff --git a/__pycache__/newpost.cpython-313.pyc b/__pycache__/newpost.cpython-313.pyc new file mode 100644 index 0000000..e1fec25 Binary files /dev/null and b/__pycache__/newpost.cpython-313.pyc differ diff --git a/__pycache__/parse_post.cpython-311.pyc b/__pycache__/parse_post.cpython-311.pyc new file mode 100755 index 0000000..6e46a0c Binary files /dev/null and b/__pycache__/parse_post.cpython-311.pyc differ diff --git a/__pycache__/parse_post.cpython-313.pyc b/__pycache__/parse_post.cpython-313.pyc new file mode 100644 index 0000000..b7b14da Binary files /dev/null and b/__pycache__/parse_post.cpython-313.pyc differ diff --git a/__pycache__/rebuild.cpython-311.pyc b/__pycache__/rebuild.cpython-311.pyc new file mode 100755 index 0000000..467e88e Binary files /dev/null and b/__pycache__/rebuild.cpython-311.pyc differ diff --git a/__pycache__/rebuild.cpython-313.pyc b/__pycache__/rebuild.cpython-313.pyc new file mode 100644 index 0000000..028a3b8 Binary files /dev/null and b/__pycache__/rebuild.cpython-313.pyc differ diff --git a/create_pages.py b/create_pages.py new file mode 100755 index 0000000..7885c9e --- /dev/null +++ b/create_pages.py @@ -0,0 +1,52 @@ +# File tree: +# +# / -- index.gmi +# | +# |- post/ -- 1.gmi +# | | +# | |- 2.gmi +# | | +# | |- 3.gmi +# | +# |- user/ -- deadvey.gmi +# | +# |- max.gmi +# + +import config +import parse_post +from datetime import datetime + +def create_user_page(userID, username, db): + with open(f"{config.webroot}/user/{username}.gmi", "w") as userfile: + cursor = db.cursor() + cursor.execute(f"SELECT * FROM posts WHERE userID = {userID} ORDER BY postID Desc") + userfile.write(f"# {username}:\n") + for x in cursor: + post = config.user_page_post_format + post = parse_post.parse_post_format(post, x, username) + userfile.write(post) + +def create_post_page(postID, db): + with open(f"{config.webroot}/post/{postID}.gmi", "w") as postfile: + cursor = db.cursor() + cursor.execute(f"SELECT * FROM posts WHERE postID = {postID}") + cursor2 = db.cursor() + cursor2.execute('SELECT * FROM users') + post = config.post_page_post_format + post = parse_post.parse_post_format(post, x, cursor2[cursor[0]-1][1]) + postfile.write(post) + +def create_timeline(db): + with open(f"{config.webroot}/index.gmi", "w") as timeline_file: + users = [] + cursor = db.cursor() + cursor.execute(f"SELECT userName FROM users") + for x in cursor: + users += x + cursor.execute(f"SELECT * FROM posts ORDER BY postID Desc LIMIT {config.posts_in_timeline}") + for x in cursor: + username = users[x[1]-1] + post = config.timeline_post_format + post = parse_post.parse_post_format(post, x, username) + timeline_file.write(post) diff --git a/example.config.py b/example.config.py new file mode 100755 index 0000000..7a1481f --- /dev/null +++ b/example.config.py @@ -0,0 +1,44 @@ +### General ### +site_url = "gemini://example.com" +webroot = "/path/to/root" +date_format = "%d/%m/%Y at %H:%M" # The date that is displayed on the page +post_seperator = "---------------------------------------------" +posts_in_timeline = 100 + +### SQL settings ### +host = "localhost" +user = "root" +password = "password" +database = "glogger" + +### Format ### +# The syntax for this is pretty simple +# %S - post seperator as defined by post_seperator +# %T - Title +# %D - Published date in the format specified by date_format +# %E - Edited date in the format specified by date_format +# %C - Post content +# %L - URL Permanent link to the post +# %U - URL the the user (poster) +# %N - the username of the user (poster) +user_page_post_format = ''' +## %T +%C +=> %L permalink +%S +''' +post_page_post_format = ''' +# Posted by %N +## %T +### Published: %D +%C +=> %U %N +Published: %D +Last Edited: %E +''' +timeline_post_format = ''' +## %T +%C +=> %U %N +%S +''' diff --git a/glogger.py b/glogger.py new file mode 100755 index 0000000..cfc997c --- /dev/null +++ b/glogger.py @@ -0,0 +1,38 @@ +import mysql.connector +import config +import newpost +import rebuild +from datetime import datetime + +db = mysql.connector.connect( + host=config.host, + user=config.user, + password=config.password, + database=config.database +) + +cursor = db.cursor() +cursor.execute("SELECT * FROM users") + + +username = input("Username: ").lower() + +# Check if this user exists +user_present = False +for x in cursor: + if x[1] == username: + user_present = True + userID = x[0] +if user_present == True: + print('''What do you want to do? + 1. Create (N)ew post + 2. (R)ebuild''') + + answer = input() + if answer == 'N' or answer == '1': + newpost.newpost(db, userID, username, datetime) + if answer == 'R' or answer == '2': + rebuild.rebuild(db) +else: + print('Sorry, that account does not exist, If it should, please ask the webadmin to add this account') +db.commit() diff --git a/newpost.py b/newpost.py new file mode 100755 index 0000000..2076a3b --- /dev/null +++ b/newpost.py @@ -0,0 +1,47 @@ +# users: +# +----------+--------------+------+-----+---------+----------------+ +# | Field | Type | Null | Key | Default | Extra | +# +----------+--------------+------+-----+---------+----------------+ +# | userID | int(11) | NO | PRI | NULL | auto_increment | +# | userName | varchar(255) | NO | | NULL | | +# +----------+--------------+------+-----+---------+----------------+ +# +# posts: +# +----------+--------------+------+-----+---------+----------------+ +# | Field | Type | Null | Key | Default | Extra | +# +----------+--------------+------+-----+---------+----------------+ +# | postID | int(11) | NO | PRI | NULL | auto_increment | +# | userID | int(11) | YES | MUL | NULL | | +# | title | varchar(255) | YES | | NULL | | +# | content | text | YES | | NULL | | +# | pubDate | varchar(255) | YES | | NULL | | +# | editDate | varchar(255) | YES | | NULL | | +# +----------+--------------+------+-----+---------+----------------+ + +import mysql.connector +from datetime import datetime +import click # Used to write post content, it launches a text editor to type into + +# Other python files # +import create_pages +import config + +def newpost(db, userID, username, datetime): + title = input("Title: ") + content = click.edit() + content = content.replace("'", "'") + print(content) + datetime = datetime.now().strftime("%d%m%YZ%H%M%ST") + + cursor = db.cursor() + cursor.execute(f"INSERT INTO posts (userID, title, content, pubDate, editDate) VALUES({userID}, '{title}', '{content}', '{datetime}', '{datetime}')") + cursor.execute(f"SELECT * FROM posts ORDER BY postID Desc LIMIT 1") + for x in cursor: + postID = x[0] + db.commit() + ## Write to user page ## + create_pages.create_user_page(userID, username, db) + create_pages.create_post_page(postID, username, db) + create_pages.create_timeline(db) + + diff --git a/parse_post.py b/parse_post.py new file mode 100755 index 0000000..166bfb5 --- /dev/null +++ b/parse_post.py @@ -0,0 +1,15 @@ +import config +from datetime import datetime + +def parse_post_format(post, record, username): + post = post.replace("%S", config.post_seperator) + post = post.replace("%T", record[2]) + post = post.replace("%D", datetime.strptime(str(record[4]),"%d%m%YZ%H%M%ST").strftime(config.date_format)) + post = post.replace("%E", datetime.strptime(str(record[5]),"%d%m%YZ%H%M%ST").strftime(config.date_format)) + post = post.replace("%C", record[3]) + post = post.replace("%L", f"{config.site_url}/post/{record[0]}.gmi") + post = post.replace("%U", f"{config.site_url}/user/{username}.gmi") + post = post.replace("%N", username) + + return post + diff --git a/rebuild.py b/rebuild.py new file mode 100755 index 0000000..cef252b --- /dev/null +++ b/rebuild.py @@ -0,0 +1,11 @@ +import config +import create_pages +def rebuild(db): + cursor = db.cursor() + cursor.execute('SELECT * FROM users') + for x in cursor: + create_pages.create_user_page(x[0], x[1], db) + create_pages.create_timeline(db) + cursor.execute('SELECT * FROM posts') + for x in cursor: + create_pages.create_post_page(x[0], db)