# tlohde

open tabs

a silly thing I made to show how many browser tabs I have open on my laptop. I promise you, there are orders of magnitude more on my phone.



how this works

I have two scripts:

Is there a better way to do this? Absolutely. Was this the first idea I had? Yes. Does it work? Seemingly. Did I learn something. Yeah, I think so.

As for why I've done this. Great question. No idea.


one

    
    #!/usr/bin/env -S uv run --script
    # /// script
    # dependencies = [
    #   "lz4",
    # ]
    # ///

    # inspired by & borrowing from
    # https://lazybea.rs/saving-tabs-in-a-text-file/

    from glob import glob
    import os
    import lz4.block
    import json
    from datetime import datetime

    paths = glob(
        f"{os.getenv('HOME')}/**/recovery.jsonlz4",
        recursive=True,
        include_hidden=True
    )

    n_tabs = 0
    for p in paths:
        with open(p, 'rb') as f:
            magic = f.read(8)
            if magic != b'mozLz40\x00':
                continue
            comp = f.read()
            decom = lz4.block.decompress(comp)
            js = json.loads(decom)
            n_tabs += sum(len(w['tabs']) for w in js['windows'])

    now = datetime.now().isoformat()

    with open(f"{os.getenv('HOME')}/outputs/tabcounts", 'a') as f:
        f.write(f"{now},{n_tabs}\n")
    
    

and the crontab for this: */10 * * * * /home/username/.local/bin/uv run --script /home/username/misc_scripts/tab_counter.py

two

    
    #!/usr/bin/bash

    . /etc/profile
    . /home/ed/.bash_profile
    . /home/ed/.bashrc

    # purge the cache
    curl --request POST \
   	--url "https://api.bunny.net/purge?url=https://mycdnurl.whatever/tabcounts.csv" \
   	--header "AccessKey: $BUNNYAPIKEY"

   	# delete the old one
    curl --request DELETE \
   	--url "https://storage.bunnycdn.com/mycdnurl.whatever/tabcounts.csv?allowRootDelete=true" \
   	--header "AccessKey: $BUNNYSTORAGEKEY"

   	# upload the new one
    curl --request PUT \
   	--url "https://storage.bunnycdn.com/mycdnurl.whatever/tabcounts.csv" \
   	--header "AccessKey: $BUNNYSTORAGEKEY" \
   	--header "Content-Type: text/plain" \
   	--upload-file "/home/outputs/tabcounts"
    
    

and the crontab for this: 0 */3 * * * /home/username/misc_scripts/tab_counter_upload