diff --git a/.gitignore b/.gitignore index 2e1a812..ab00065 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ config.py __pycache__ *.swp log +.git/ +users.py +posts.py diff --git a/README.md b/README.md index e74ba62..0bbd4a3 100755 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ mariadb-server ``` 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) ); +CREATE TABLE posts ( postID INT PRIMARY KEY AUTO_INCREMENT NOT NULL, userID INT, FOREIGN KEY(userID) REFERENCES users(userID), title VARCHAR(255), content VARCHAR(MAX), pubDate VARCHAR(255), editDate VARCHAR(255) ); ``` # TO DO * Add RSS and/or ATOM support diff --git a/create_pages.py b/create_pages.py index 49d5086..9fb18f8 100644 --- a/create_pages.py +++ b/create_pages.py @@ -16,38 +16,47 @@ import config import parse_post import output +import initialise +try: + import posts +except: + print("No posts database") +try: + import users +except: + print("No users database") from datetime import datetime -def create_user_page(userID, username, db): - with open(f"{config.webroot}/user/{username}.gmi", "w") as userfile: - output.log("Writing posts to 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_user_page(userID, username): + try: + with open(f"{config.webroot}/user/{username}.gmi", "w") as userfile: + output.log("Writing posts to userfile") + userfile.write(f"# {username}:\n") + for post_index in range(len(posts.posts)-1,-1,-1): + post_content = config.user_page_post_format + post_content = parse_post.parse_post_format(post_content, post_index, username) + userfile.write(post_content) + except: + print(f"Unable to open {username}.gmi") + initialise.initialise() -def create_post_page(post, db): - with open(f"{config.webroot}/post/{post[0]}.gmi", "w") as postfile: - output.log("Writing post to postfile") - output.log(post) - postfile_content = config.post_page_post_format - postfile_content = parse_post.parse_post_format(postfile_content, post, post[1]) - postfile.write(postfile_content) +def create_post_page(post_index): + try: + with open(f"{config.webroot}/post/{post_index}.gmi", "w") as postfile: + output.log("Writing post to postfile") + output.log(posts.posts[post_index]) + postfile_content = config.post_page_post_format + postfile_content = parse_post.parse_post_format(postfile_content, post_index, users.users[posts.posts[post_index]["userID"]]) + postfile.write(postfile_content) + except: + print(f"Unable to open {post_index}.gmi") + initialise.initialise() -def create_timeline(db): +def create_timeline(): with open(f"{config.webroot}/index.gmi", "w") as timeline_file: output.log("Writing posts to timeline") - 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] + for current_post_index in range(len(posts.posts)-1,-1,-1): + username = users.users[posts.posts[current_post_index]["userID"]] post = config.timeline_post_format - post = parse_post.parse_post_format(post, x, username) + post = parse_post.parse_post_format(post, current_post_index, username) timeline_file.write(post) diff --git a/example.config.py b/example.config.py index ee6dad2..d46357a 100755 --- a/example.config.py +++ b/example.config.py @@ -5,12 +5,6 @@ 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 = "username" -password = "password" -database = "glogger" - ### Logging ### logfile = "/path/to/logfile" verbose = False diff --git a/glogger.py b/glogger.py index cfc997c..66c95e8 100755 --- a/glogger.py +++ b/glogger.py @@ -1,28 +1,22 @@ -import mysql.connector +import initialise +try: + import users +except: + print("No users database") + initialise.initialise() 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: +for current_user in range(len(users.users)): + if users.users[current_user] == username: user_present = True - userID = x[0] + userID = current_user if user_present == True: print('''What do you want to do? 1. Create (N)ew post @@ -30,9 +24,8 @@ if user_present == True: answer = input() if answer == 'N' or answer == '1': - newpost.newpost(db, userID, username, datetime) + newpost.newpost(userID, username, datetime) if answer == 'R' or answer == '2': - rebuild.rebuild(db) + rebuild.rebuild() else: print('Sorry, that account does not exist, If it should, please ask the webadmin to add this account') -db.commit() diff --git a/initialise.py b/initialise.py new file mode 100644 index 0000000..fa9ca03 --- /dev/null +++ b/initialise.py @@ -0,0 +1,35 @@ +import os +try: + import config +except: + print("You have no configuration file, please move example.config.py to config.py and modify the options to your usage") + exit() + +def initialise(): + # Create the posts db + try: + import posts + except: + with open("posts.py","w") as posts_db: + posts_db.write("posts = []") + # Create the users db + try: + import users + except: + users = [] + print("What users do you want to have? You can always change this later by editing users.py (0 to finish)") + while True: + user_input = input() + if user_input == "0": + break + else: + users.append(user_input) + with open("users.py", "w") as users_db: + users_db.write(f"users = {users}") + # Create folders in the web directory + if not os.path.exists(f"{config.webroot}/post"): + os.makedirs(f"{config.webroot}/post") + if not os.path.exists(f"{config.webroot}/user"): + os.makedirs(f"{config.webroot}/user") + + print("Successfully initialised Glogger") diff --git a/newpost.py b/newpost.py index fa6dae8..88031fe 100644 --- a/newpost.py +++ b/newpost.py @@ -18,7 +18,6 @@ # | 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 @@ -26,20 +25,25 @@ import click # Used to write post content, it launches a text editor to type int import create_pages import config import rebuild +try: + import posts +except: + print("No posts database") +try: + import users +except: + print("No users database") +import output -def newpost(db, userID, username, datetime): - title = input("Title: ") - content = click.edit() - content = content.replace("'", "'") - content = content.replace("\n", "\\n") - print(content) - datetime = datetime.now().strftime("%d%m%YZ%H%M%ST") +def newpost(userID, username, datetime): + title = input("Title: ") + content = click.edit() + content = content.replace("'", "'") + content = content.replace("\n", "\n") + output.log(content) + datetime = datetime.now().strftime("%d%m%YZ%H%M%ST") + posts.posts.append({'userID': userID, 'title': title, 'content': content, 'pubdate': datetime, 'editdate': datetime}) + with open("posts.py", "w") as posts_file: + posts_file.write(f"posts = {posts.posts}") - 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() - # Rebuild the config - rebuild.rebuild(db) + rebuild.rebuild() diff --git a/parse_post.py b/parse_post.py index 166bfb5..01022e5 100644 --- a/parse_post.py +++ b/parse_post.py @@ -1,15 +1,17 @@ import config from datetime import datetime +import posts -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) +def parse_post_format(post, post_index, username): + post_data = posts.posts[post_index] + post = post.replace("%S", config.post_seperator) + post = post.replace("%T", post_data["title"]) + post = post.replace("%D", datetime.strptime(str(post_data["pubdate"]),"%d%m%YZ%H%M%ST").strftime(config.date_format)) + post = post.replace("%E", datetime.strptime(str(post_data["editdate"]),"%d%m%YZ%H%M%ST").strftime(config.date_format)) + post = post.replace("%C", post_data["content"]) + post = post.replace("%L", f"{config.site_url}/post/{post_index}.gmi") + post = post.replace("%U", f"{config.site_url}/user/{username}.gmi") + post = post.replace("%N", username) - return post + return post diff --git a/rebuild.py b/rebuild.py index 11dd9e9..1f579be 100755 --- a/rebuild.py +++ b/rebuild.py @@ -1,11 +1,16 @@ 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 posts.postID, users.username, posts.title, posts.content, posts.pubDate, posts.editDate FROM posts JOIN users ON posts.userID=users.userID') - for x in cursor: - create_pages.create_post_page(x, db) +try: + import users +except: + print("No users database") +try: + import posts +except: + print("No posts database") +def rebuild(): + create_pages.create_timeline() + for current_user_index in range(len(users.users)): + create_pages.create_user_page(current_user_index, users.users[current_user_index]) + for current_post_index in range(len(posts.posts)): + create_pages.create_post_page(current_post_index)