Skip to content

Instantly share code, notes, and snippets.

View pirate's full-sized avatar
🗃️
Archiving all the things!

Nick Sweeting pirate

🗃️
Archiving all the things!
View GitHub Profile
@pirate
pirate / pydantic_settings_with_defaults.py
Last active September 23, 2024 00:11
pydantic_settings BaseSettings model that allows using functions as that depend on other fields as default values
#!/usr/bin/env python
from typing import Callable
from pydantic import model_validator, TypeAdapter
from pydantic_settings import BaseSettings, SettingsConfigDict
class ModelWithDefaults(BaseSettings):
"""
This is an addon to pydantic_settings's BaseSettings that allows you to use
@pirate
pirate / create_random_uid_user.yml
Created September 22, 2024 23:21
Ansible playbook to create a new user with a random available uid & gid
- name: Determine available groups
getent:
database: group
- name: Add additional groups to user
user: name="{{user}}" groups="{{item}}" append=yes
when: item in ansible_facts.getent_group
with_items:
- sudo
- wheel
@pirate
pirate / mapmyride_downloader.sh
Created September 19, 2024 08:30
Download all your MapMyRide / MapMyFitness workout TCX files using your account data export CSV
#!/usr/bin/env bash
### Bash Environment Setup
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
# set -o xtrace
# set -x
# shopt -s nullglob
set -o errexit
set -o errtrace
@pirate
pirate / accellerated_django_paginator.py
Created September 12, 2024 03:59
A faster version of the default Django Paginator
# This is an improved Django Paginator that makes Admin list view pages load much faster.
# You can tell if you need this if you see a big slow COUNT() query
# in the django_debug_toolbar SQL panel on every admin list view.
# It improves performance by avoiding running a potentially costly DISTINCT query to
# count total rows on every pageload. Instead, when possible (when there are no filters)
# it does a significantly simpler SELECT COUNT(*) tablename to get the total.
from django.core.paginator import Paginator
@pirate
pirate / udm_fancontrol_setup.sh
Created June 4, 2024 10:34
Setup Unifi Dream Machine (UDM, UDM-SE, etc.) automatic fan speeds control to lower temps and increase hard drive lifespan.
#!/bin/bash
# Installs and configures fancontrol to automatically run Unifi Dream Machine (UDM) fans harder & lower temperatures.
#
# Context: Unifi equipment typically runs *very* hot from the factory unless you customize it.
# I don't want my hard drives to run as hot as they let them get by default (55ºC+), because it shortens lifespan (ideal temp is ~35-45C).
#
# Further Reading:
# - https://linux.die.net/man/8/fancontrol
# - https://community.ui.com/questions/Unifi-Dream-Router-fan-speed/1cb9cf18-e8a0-44b4-8402-ee9bac367bc0
# - https://www.reddit.com/r/Ubiquiti/comments/13imgqj/udm_pro_fan_speed_is_this_normal/
@pirate
pirate / pydantic_config.py
Last active May 15, 2024 12:40
Pydantic config loader and dumper with dynamic defaults based on previous values and support for TOML, INI, JSON, Env, and ArchiveBox schema formats.
import re
import os
import sys
import toml
import json
import orjson
import platform
import inspect
import tomllib
import ini2toml
@pirate
pirate / ini_to_toml.py
Created May 15, 2024 12:39
Convert an INI config string into a TOML config string with Python
from typing import Dict, Any, List
import configparser
import json
import ast
JSONValue = str | bool | int | None | List['JSONValue']
def load_ini_value(val: str) -> JSONValue:
"""Convert lax INI values into strict TOML-compliant (JSON) values"""
@pirate
pirate / add_debug_entitlement.sh
Created March 28, 2024 09:02 — forked from talaviram/add_debug_entitlement.sh
Simple Utility Script for allowing debug of hardened macOS apps.
#! /bin/bash
# Simple Utility Script for allowing debug of hardened macOS apps.
# This is useful mostly for plug-in developer that would like keep developing without turning SIP off.
# Credit for idea goes to (McMartin): https://forum.juce.com/t/apple-gatekeeper-notarised-distributables/29952/57?u=ttg
# Update 2022-03-10: Based on Fabian's feedback, add capability to inject DYLD for sanitizers.
#
# Please note:
# - Modern Logic (on M1s) uses `AUHostingService` which resides within the system thus not patchable and REQUIRES to turn-off SIP.
# - Some hosts uses separate plug-in scanning or sandboxing.
# if that's the case, it's required to patch those (if needed) and attach debugger to them instead.
const fs = require('fs');
const path = require('path');
const pathTo2captchaExtension = path.join(__dirname, '2captcha-solver');
const pathToPuppeteerStreamExtension = path.join(__dirname, 'puppeteer-stream-ext');
const { Cluster } = require('puppeteer-cluster');
const puppeteer = require("puppeteer-extra");
// add recaptcha plugin to solve captchas automatically
@pirate
pirate / docker_netcat_tunnel.sh
Last active January 29, 2024 08:07
Remote control a Docker container from another container in the same network using netcat, ssh, or pure python
# Three ways to control a docker container remotely from another docker container or host: Python, SSH, or ncat/nc/socat.
#
# Useful if you have two containers in a Docker Compose project and you want
# container1 to be able to run commands in container2 without having to share /var/run/docker.sock
#
# Further Reading: https://www.revshells.com/#bind
### 1. Using pure Python, the cleanest option
# supports dbus/x11/etc as it doesnt spawn a new login shell, correctly handles exiting after finished commands without hacky workarounds