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:
-
One, heavily inspired by this post, which appends the current number of tabs open (across all windows, of both librewolf and firefox) and the current time to a .csv file. This runs every 10 minutes, thanks to cron. Which is, frankly, too often.
- Script number two, uses `curl` to and the Bunny Storage API to purge the cache, delete the old csv and upload the new one. This runs every 3 hours. Which is also, too often.
- This page, runs a bit of javascript, and uses d3js to plot the timeseries.
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