-- Leo's gemini proxy

-- Connecting to ainent.xyz:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini; charset=utf-8; lang=en

Lagrange Hook: Gemtext Data Consumption


I'd been working on a little script to try to keep track of Gemtext data consumption. However, between my limited free time and numerous hobbies, I had actually forgotten all about this.


It's not the most useful data to track, but who cares, it sounded fun! It's not done, but I like where it's going. Once I finish this, I'll post the finished code.


Note that not only is this still a rough draft... and my Python skills are novice.


import sys

def utf8len(s):
    return len(s.encode('utf-8'))

def append_to_flatfile(date, response_size):
    # TODO: Need to create file programatically if not exists already
    # TODO: This doesn't work when ran from Lagrange hook, only when running 'locally'
    with open('gemtext_sizes.csv', 'a') as csv_file:
        new_entry = str(date) + ',' + str(response_size) + '\n'
        csv_file.write(new_entry)

# TODO: Refactor this to 'createHeader` and let the caller do the `print`ing
def printHeader(response):
    # TODO: Exclude localhost
    # TODO: read csv file
    # TODO: sum total data since ever
    # TODO: sum total data this month
    # TODO: convert to kilobytes

    this_response_size = utf8len(response)
    #append_to_flatfile(date.today(), this_response_size)
    data_this_month = 10 # temporary placeholder
    data_total = 100 # temporary placeholder

    print('```Table listing local Gemini data downloaded.')
    print('+--------------------------------------------------+')
    print('| File size:', this_response_size, '                                 |')
    print('| Gemini data downloaded this month:', data_this_month, '           |')
    print('| Gemini data downloaded since script install:', data_total, '|')
    print('+--------------------------------------------------+')
    print('```')

response = sys.stdin.read()
response_size = utf8len(response)

print('20 text/gemini\r\n')
printHeader(response)
# TODO: Move the header down below the first heading
print(response)

Limitations


Cannot catch all of Lagrange's data usage because it only runs on Gemtext served over gemini://

Cannot catch anything served via spartan://, gopher://, nor finger://

It also won't account for data from fetching feed subscription updates.

I've also ran into a bug in Lagrange where even having mimehooks enabled causes feed refreshing to hang, making this infeasible for me for the time being. I have reported this defect to @skyjake and he kindly agreed to bump it up on his priority list.


All in all, hooks are a pretty neat concept. I may spend some time thinking up more.

-- Response ended

-- Page fetched on Tue May 21 20:09:08 2024