💾 Archived View for gemini.smallweb.space › tech-gemlog › attach › gemlog.titan.txt captured on 2023-12-28 at 15:26:34.

View Raw

More Information

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

#! /usr/bin/env python3

#########################################
# Title: Titan Gemlog Uploader          #
# Author: @gritty@gemini.smallweb.space #
# Date: 2023-12-17                      #
#########################################

# NOTE: if using GmCapsule, rename this file to something like:
# '<name>,titan'
# and make it executable:
# 'chmod +x <file>
# so that GmCapsule recognizes it as a Titan file
# access it by:
# titan://<path_to_cgi_scripts_on_your_capsule>/<titan_file>,titan
# note the comma

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

pathInfo = get_path_info()
clientCert = get_client_cert(cert_reqd = True, restricted_auth = True)
contentType = os.getenv('CONTENT_TYPE')

PATH = "../../../gemini.smallweb.space/gemlog/"     # path to gemlog folder
INDEXPATH = PATH + "index.gmi"                      # full path to the gemlog index

# Get the uploaded information and put into array
titan_data = sys.stdin.readlines()

# Process the gemlog
if (contentType == "text/plain"):
    # print gemini '10' header
    show_header_ok()

    # find first occurrence of a title line (starts with '#') or return 'None'
    # TODO: ignore matches for '##', '###', etc.
    firstTitle = next((x for x in titan_data if x.startswith('#')), None)
    originalTitle = firstTitle      # needed for index.gmi entry

    if firstTitle != None:
        
        # Properly format the title for the gemlog index file
        originalTitle = originalTitle.lstrip('#')           # pull out the leading '#'
        originalTitle = originalTitle.lstrip()              # pull out leading whitespace
        
        # Properly format the title for the file name
        firstTitle = re.sub(r'[^\w\s]', '', firstTitle)     # delete punctuation
        firstTitle = "-".join(firstTitle.split())           # replaces spaces with hyphen 

        # get today's date in YYYYMMDD format
        now = datetime.datetime.utcnow().strftime("%Y%m%d")
        
        # create file name: "YYYYMMDD - <title>.gmi"
        fullFileName = now + "-" + firstTitle + ".gmi"

        # full path with file name
        fullpath = PATH + fullFileName

        # write the file
        with open(fullpath, "w") as f:
            f.writelines(titan_data)

        # Setup terms for insertion into gemlog index
        mark = "## Posts"       # Term to match where gemlog entries start in the file
        inserted = 0            # flag

        # Date format for gemlog entries (YYYY-MM-DD)
        nowDashed = datetime.datetime.utcnow().strftime("%Y-%m-%d")
        
        # write to file
        # TODO: catch errors
        for line in fileinput.FileInput(INDEXPATH, inplace=True):
            if inserted == 0 and mark in line:
                # marked line was found so craft the entry and insert it into the file
                line += "=> " + fullFileName + " " + nowDashed + " - " +  originalTitle 
                inserted = 1
            print(line, end="")

        print("File written")
else:
    print("file not written.  Either there was a file error or no title line was found.")


print("=> gemini://gemini.smallweb.space Home")