commit acb98060b39068bf62721f6a5031c3b3efff65f7 Author: Alex Lardner Date: Fri Jul 31 01:06:49 2026 -0700 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98e6ef6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.db diff --git a/blahblah/__init__.py b/blahblah/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blahblah/__pycache__/__init__.cpython-314.pyc b/blahblah/__pycache__/__init__.cpython-314.pyc new file mode 100644 index 0000000..9754ddf Binary files /dev/null and b/blahblah/__pycache__/__init__.cpython-314.pyc differ diff --git a/blahblah/__pycache__/bbdb.cpython-314.pyc b/blahblah/__pycache__/bbdb.cpython-314.pyc new file mode 100644 index 0000000..41f01da Binary files /dev/null and b/blahblah/__pycache__/bbdb.cpython-314.pyc differ diff --git a/blahblah/__pycache__/db.cpython-314.pyc b/blahblah/__pycache__/db.cpython-314.pyc new file mode 100644 index 0000000..03bc9c8 Binary files /dev/null and b/blahblah/__pycache__/db.cpython-314.pyc differ diff --git a/blahblah/__pycache__/main.cpython-314.pyc b/blahblah/__pycache__/main.cpython-314.pyc new file mode 100644 index 0000000..9dbfd2e Binary files /dev/null and b/blahblah/__pycache__/main.cpython-314.pyc differ diff --git a/blahblah/bbdb.py b/blahblah/bbdb.py new file mode 100644 index 0000000..bac476b --- /dev/null +++ b/blahblah/bbdb.py @@ -0,0 +1,62 @@ +import sqlite3, datetime + +def initBlog(db_file): + db = sqlite3.connect(db_file) + db.execute(""" + CREATE TABLE IF NOT EXISTS posts ( + id INTEGER PRIMARY KEY, + title TEXT, + body TEXT NOT NULL, + timestamp INTEGER + ) + """) + + db.commit() + return db + +def newPost(db, title, body): + now = datetime.datetime.now() + time = int(now.timestamp() * 1000) + db.execute( + "INSERT INTO posts (title, body, timestamp) VALUES (?, ?, ?)", + (title, body, time) + ) + db.commit() + +def getPost(db, index: int): + db.row_factory = sqlite3.Row + cur = db.cursor() + cur.execute("SELECT * FROM posts WHERE id = (?)", (index,)) + return cur.fetchone() + + +def getAllPosts(db): + db.row_factory = sqlite3.Row + return db.execute("SELECT * FROM posts ORDER BY timestamp DESC").fetchall() + +def deletePost(db, index: int): + db.execute( + "DELETE FROM posts WHERE id = ?", + (index,) + ) + db.commit() + +def updateTitle(db, index: int, title): + db.execute( + "UPDATE posts SET title = (?) WHERE id = (?)", + (title, index,) + ) + db.commit() + +def updateBody(db, index: int, body): + db.execute( + "UPDATE posts SET body = (?) WHERE id = (?)", + (body, index,) + ) + db.commit() + +#if __name__ == "__main__": + #db = initBlog("blog.db") + + #db.close() + diff --git a/blahblah/main.py b/blahblah/main.py new file mode 100644 index 0000000..89b8e55 --- /dev/null +++ b/blahblah/main.py @@ -0,0 +1,155 @@ +from blahblah import bbdb +import tempfile +import subprocess +import os + +def mainMenu(): + print(""" + _ _ _ _ _ _ + | |__ | | __ _| |__ | |__ | | __ _| |__ + | '_ \\| |/ _` | '_ \\| '_ \\| |/ _` | '_ \\ + | |_) | | (_| | | | | |_) | | (_| | | | | + |_.__/|_|\\__,_|_| |_|_.__/|_|\\__,_|_| |_| + + 1. [N]ew Post + 2. [E]dit Post + 3. [D]elete Post + 4. [L]ist Posts + + 5. [G]enerate HTML + + 6. [Q]uit + """) + choice = input("Choose an option: ") + return choice + +def writeNew(db): + print(""" + + Write New Post + + 1. [N]ew Micropost + 2. [L]oad Post From File + """) + + choice = input("Choose an option: ") + + if choice == "n" or choice == "N" or choice == "1": + post = input("Enter your post: ") + print('You entered: "' + post + '"') + confirmPost = input("Publish this micropost (Y/N)? ") + if confirmPost == 'y' or confirmPost == 'Y': + bbdb.newPost(db, None, post) + +def editPost(db): + try: + postID = int(input("Enter the numerical ID of the post to edit or [Q]uit: ")) + except ValueError: + print("Cancelled edit.") + return + + try: + post = bbdb.getPost(db, postID) + except: + print("Error finding post.") + return + else: + print(post["id"], post["title"]) + print(post["body"]) + + print(""" + + 1. Edit [T]itle + 2. Edit [B]ody + + """) + + repeat = True + + while (repeat): + choice = input("Choose which field to edit or [Q]uit: ") + if choice == 't' or choice == 'T' or choice == "1": + newTitle = input("Enter a new title: ") + print('Your new title is "' + newTitle + '"') + confirmEdit = input("Publish this edited title [Y/N]? ") + if confirmEdit == 'y' or confirmEdit == 'Y': + repeat = False + try: + bbdb.updateTitle(db, postID, newTitle) + except: + print("Error updating title.") + else: + print("Updated title.") + elif choice == 'b' or choice == 'B' or choice == "2": + editor = os.environ.get("EDITOR", os.environ.get("VISUAL", "vi")) + + with tempfile.NamedTemporaryFile(suffix=".tmp", mode="w+", delete=False) as tf: + tf.write(post["body"]) + path = tf.name + + try: + subprocess.call([editor, path]) + with open(path, "r") as f: + newBody = f.read() + finally: + os.unlink(path) + + print('Your new body text is:\n\n' + newBody + "\n") + confirmEdit = input("Publish this edited post [Y/N]? ") + if confirmEdit == 'y' or confirmEdit == 'Y': + repeat = False + try: + bbdb.updateBody(db, postID, newBody) + except: + print("Error updating post body.") + else: + print("Updated post body.") + + elif choice == 'q' or choice == 'Q': + repeat = False + + +def listPosts(db): + for p in bbdb.getAllPosts(db): + print("***") + print(p["id"], p["title"]) + print(p["body"]) + print("\n") + +def confirmDelete(db): + try: + post = int(input("Enter the ID of the post to PERMANENTLY DELETE or [Q]uit: ")) + except ValueError: + print("Cancelled delete.") + return + + try: + bbdb.deletePost(db, post) + except: + print(post) + print("Error deleting post") + else: + print("Deleted post ", post) + +if __name__ == "__main__": + db = bbdb.initBlog("blog.db") + + running = True; + foo = 0; + + while(running): + choice = mainMenu() + + if choice == 'q' or choice == 'Q' or choice == "6": + running = False + elif choice == 'n' or choice == 'N' or choice == "1": + writeNew(db) + elif choice == 'e' or choice == 'E' or choice == "2": + editPost(db) + elif choice == 'd' or choice == 'D' or choice == "3": + confirmDelete(db) + elif choice == 'l' or choice == 'L' or choice == "4": + listPosts(db) + + db.close() + diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..5f52513 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +python3 -m blahblah.main