Skip to content

Instantly share code, notes, and snippets.

@jkoop
jkoop / autotitle.js
Last active October 14, 2024 05:32
Automatically generate and set a title for documents with a specific tag in paperless.ngx using ollama
#! bun run
/**
* The author disclaims copyright to this source code.
*/
const PAPERLESS_URL = process.env.PAPERLESS_URL ?? "";
const PAPERLESS_TOKEN = process.env.PAPERLESS_TOKEN ?? "";
const PAPERLESS_TAG_ID = process.env.PAPERLESS_TAG_ID ?? 0;
const OLLAMA_URL = process.env.OLLAMA_URL ?? "";
@jkoop
jkoop / PreventOverloading.php
Last active August 20, 2024 15:47
A Laravel Middleware that checks the system's current 15 minute loadavg and the number of CPUs, and if the resulting division is greater than 85%, returns 503 with an appropriate message.
<?php
// app/Http/Middleware/PreventOverloading.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
@jkoop
jkoop / PurifyHtml.php
Created March 8, 2024 15:03
Laravel 10: middleware: prevent HTML and Markdown attacks
<?php
namespace App\Http\Middleware;
use HTMLPurifier;
use HTMLPurifier_HTML5Config;
use Illuminate\Foundation\Http\Middleware\TransformsRequest;
/**
* Use HTMLPurifier to purify all inputs with names matching `/[-_]html\s*$/i`.
@jkoop
jkoop / SetLocale.php
Created March 5, 2024 21:23
Laravel 10; set App and User's locale based on browser's preferred language and server's `lang/` directory; @requires `punic/punic`
<?php
/**
* For Laravel 10.
* Automatically set App and User's locale based on browser's preferred
* language and server's `lang/` directory.
* @copyright 2024 Joe Koop
* @license MIT
*/
@jkoop
jkoop / SetLocale.php
Created February 29, 2024 20:36
Automatically set app locale based on browser's preferred language and server's `lang/` directory
<?php
/**
* For Laravel 10.
* Automatically set app locale based on browser's preferred language and server's `lang/` directory.
* @copyright 2024 Joe Koop
* @license MIT
*/
namespace App\Http\Middleware;
@jkoop
jkoop / numbers.h
Last active November 22, 2023 15:08
terse shorthands for numeric types in C
/**
* numbers.h - Terse shorthands for numeric types
* Copyright (c) 2023 Joe Koop
* License: The MIT License (MIT)
*/
#include <stdint.h>
// E for "exact"
typedef int8_t i8e;
@jkoop
jkoop / readable-dates.php
Last active March 15, 2024 15:34 — forked from scr4bble/readable-dates.php
Adminer plugin that replaces UNIX timestamps with human-readable dates.
<?php
/** This plugin replaces UNIX timestamps with human-readable dates in your local timezone.
* Mouse click on the date field reveals timestamp back.
*
* @link https://www.adminer.org/plugins/#use
* @author Anonymous
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
@jkoop
jkoop / wifi-geotracking-to-traccar.sh
Created May 1, 2023 13:46
Get location via WiFi (with Google API) and report it to your Traccar server
#!/bin/bash
# Run this as root
GOOGLE_API_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
TRACCAR_URL='https://traccar.example.com'
INTERFACE=wlan0
DEVICE_ID=123456
json=$(sudo iwlist $INTERFACE scan | egrep -o 'Address: .*|Channel:.*|Signal level=.*|Last beacon:.*' | sed -E 's/Address: (.*)/\{"macAddress":"\1",/g' | sed -E 's/Channel:(.*)/"channel":\1,/g' | sed -E 's/Signal level=(.*) dBm/"signalStrength":\1,/g' | sed -E 's/Last beacon: (.*)ms ago/"age":\1}/g')
json=$(echo "$json" | head -n 1; echo "$json" | sed -E 's/\{/,{/g' | tail -n +2)
@jkoop
jkoop / get-list-of-clients-from-aerohive-aps.php
Created April 11, 2023 02:30
Get a list of all Wifi clients from AeroHive APs
#!/usr/bin/php
<?php
/**
* This script SSHes into all of my AeroHive Wifi APs, gets a list of current
* connected clients from each if them, assembles the list into one, and writes
* that list to a JSON file.
*
* This is intended to be run as a minutely `cron` job. As such, the path of the
* output file is the date, formatted to not put a million files in one folder:
@jkoop
jkoop / where-clause.sql
Created March 27, 2023 21:33
13 digit ISBN checker for SQLite3
(10 - (
substr(isbn, 1, 1) * 1 +
substr(isbn, 2, 1) * 3 +
substr(isbn, 3, 1) * 1 +
substr(isbn, 4, 1) * 3 +
substr(isbn, 5, 1) * 1 +
substr(isbn, 6, 1) * 3 +
substr(isbn, 7, 1) * 1 +
substr(isbn, 8, 1) * 3 +
substr(isbn, 9, 1) * 1 +