💾 Archived View for gemini.smallweb.space › HOWTO › examples › tinylog-example.txt captured on 2024-05-26 at 14:43:07.

View Raw

More Information

⬅️ Previous capture (2023-09-08)

-=-=-=-=-=-=-

#!/usr/bin/env python3

import os,sys, fileinput, datetime, urllib.parse
sys.path.append('../lib/')
from helpers import *

qString = get_query_string()
pathInfo = get_path_info()
clientCert = get_client_cert(cert_reqd = True, restricted_auth = True) 
#ident=os.getenv("REMOTE_IDENT")
filePath="/home/gemini/gemini/gemini.smallweb.space/tinylog/tinylog.gmi"

if(qString == None):
    # Display the main page

    # Read and display gem file
    contents = read_file('tinylog-main.gmi')
    show_header_ok()
    print(contents)
    
if isinstance(qString, dict):
   
    if ('edit' in qString):
        print("10 Enter your log. \r\n")
    elif ('status' in qString):
        show_header_ok()
        print(qString['status'][0])
        print("=> tinylog Return")
    elif qString == {}:
        # The dict is empty which means there's probably a tinylog status in
        # the query string
        
        # get unparsed queryString
        userInput = get_query_string(unparsed = True)
        
        # insert new log into file
        mark = "author:"
        inserted = 0
        
        # date
        now = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC")
        data = urllib.parse.unquote(userInput)

        # write to file
        # TODO: catch errors and output
        for line in fileinput.FileInput(filePath, inplace=True):
            if inserted == 0 and mark in line:
                line += "\n## " + now + "\n" + data + "\n" 
                inserted = 1
            print(line, end="")
        
        # redirect back to this page with status update
        temp_redirect('tinylog?status=Tinylog Posted')