diff options
author | tdback <tyler@tdback.net> | 2025-01-27 22:45:38 -0500 |
---|---|---|
committer | tdback <tyler@tdback.net> | 2025-01-27 22:45:38 -0500 |
commit | 303aa81d25df5f96ce980f2d38efb4a8ffc8faf7 (patch) | |
tree | 9bf4a0330986a6b37549b1da8fd5a7bd77fcfa94 |
initial commit
-rw-r--r-- | README.md | 13 | ||||
-rwxr-xr-x | background | 14 | ||||
-rwxr-xr-x | fman | 5 | ||||
-rwxr-xr-x | geoip | 66 | ||||
-rwxr-xr-x | mpc_update | 25 | ||||
-rwxr-xr-x | record | 23 | ||||
-rwxr-xr-x | snapshot | 37 | ||||
-rwxr-xr-x | testzed | 24 | ||||
-rwxr-xr-x | weather | 10 |
9 files changed, 217 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..cc9cc16 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Scripts +A collection of helpful scripts. + +## Requirements +Some scripts require the following to be installed and in your system path: +- curl +- feh +- ffmpeg (with Xcb support) +- fzf +- ImageMagick +- python3 (with requests library) +- zpool +- $EDITOR environment variable set diff --git a/background b/background new file mode 100755 index 0000000..6729237 --- /dev/null +++ b/background @@ -0,0 +1,14 @@ +#!/bin/sh + +# background: Set the background using `feh`. + +IMAGES="$HOME/.local/backgrounds" + +if [ "$1" = "-r" ]; then + feh --bg-scale --randomize "$IMAGES" + exit +fi + +SELECTED=$(find "$IMAGES" -type f -printf '%f\n' | fzf) + +[ -n "$SELECTED" ] && feh --bg-scale "$IMAGES/$SELECTED" @@ -0,0 +1,5 @@ +#!/bin/sh + +# fman: Fuzzy find man pages! Requires `fzf`. + +apropos . | fzf --prompt "Select program: " | cut -d" " -f 1 | xargs man @@ -0,0 +1,66 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python3 --pure -p 'python3.withPackages (p: [ p.requests ])' + +import argparse +import requests +import socket +import sys + +API = "http://ip-api.com/json" + + +def valid_ip(address: str) -> bool: + """Check if the given address is a valid IPv4 address.""" + try: + socket.inet_pton(socket.AF_INET, address) + except AttributeError: # No inet_pton. + try: + socket.inet_aton(address) + except socket.error: + return False + return address.count(".") == 3 + except socket.error: # Not a valid address. + return False + return True + + +def main(): + parser = argparse.ArgumentParser( + prog="geoip", + description="Geolocate an IPv4 address.", + ) + + parser.add_argument(dest="address", help="IPv4 address to locate") + + args = parser.parse_args() + + if not valid_ip(args.address): + print(f"'{args.address}' is not a valid IPv4 address", file=sys.stderr) + exit(1) + + res = requests.get(f"{API}/{args.address}") + if res.status_code != 200: + print( + f"Something went wrong: returned {res.status_code}", + file=sys.stderr, + ) + + data = res.json() + if data["status"] == "fail": + print(f"Querying '{data['query']}' failed", file=sys.stderr) + exit(1) + + # Not every address will have a zip code, so adjust the format accordingly. + loc = ( + f"{data['query']} is located in {data['city']}, {data['regionName']}" + + (f", {data['zip']} " if data["zip"] else " ") + + data["country"] + ) + + print(loc) + + +if __name__ == "__main__": + main() + +# vim:ft=python diff --git a/mpc_update b/mpc_update new file mode 100755 index 0000000..f576097 --- /dev/null +++ b/mpc_update @@ -0,0 +1,25 @@ +#!/bin/sh + +# mpc_update: Update an mpd playlist to include new songs in the music +# directory. + +die() { echo "$0: $*" >&2; exit 1; } +try() { "$@" || die "cannot $*"; } + +[ "$#" -ne 2 ] && die "usage: mpc_update <playlist> <music directory>" + +PLAYLIST="$1" +MUSIC_DIR="$2" + +[ -d "$MUSIC_DIR" ] && cd "$MUSIC_DIR" || + die "music directory '$MUSIC_DIR' does not exist!" + +# Clear the current playlist to prevent song duplication in mpd. +[ -e "$HOME/.local/share/mpd/playlists/${PLAYLIST}.m3u" ] && + try mpc clear >/dev/null && + try mpc rm "$PLAYLIST" + +# Add every song in the music directory. +try mpc add -- * + +try mpc save "$PLAYLIST" @@ -0,0 +1,23 @@ +#!/bin/sh + +# record: Record an mp4 video using ffmpeg. By default it will output the +# video to './out.mp4'. Requires ffmpeg to be built with Xcb support on NixOS. +# Screen resolution can be specified, but defaults to 1080p. + +die() { echo "$0: $*" >&2; exit 1; } +try() { "$@" || die "cannot $*"; } + +while getopts ":r:h" args; do + case "$args" in + r) RESOLUTION="$OPTARG" ;; + h) die "record [-r RESOLUTION] FILE" ;; + *) die "-$OPTARG is not an option." ;; + esac +done +shift $((OPTIND - 1)) + +FILE="${1:-out}" +: "${RESOLUTION:=1920x1080}" + +try ffmpeg -y -f x11grab -s "$RESOLUTION" -framerate 60 -i :0.0 -f alsa \ + -i default -nostats -hide_banner -loglevel quiet "${FILE}.mp4" diff --git a/snapshot b/snapshot new file mode 100755 index 0000000..380051a --- /dev/null +++ b/snapshot @@ -0,0 +1,37 @@ +#!/bin/sh + +# snapshot: Take a screenshot using ImageMagick. Assumes `dunst` daemon is +# running when sending notifications. + +set -eu + +SCREENSHOT_DIR="$HOME/.local/screenshots" +SCREENSHOT_NAME="$SCREENSHOT_DIR/$(date '+%F_%R-%S' | sed 's/:/-/g').png" + +_end() { + dunstify "Screenshot captured:\n$SCREENSHOT_NAME" + display "$SCREENSHOT_NAME" + [ -t 1 ] && echo "$SCREENSHOT_NAME" + exit 0 +} + +region() { + import "$SCREENSHOT_NAME" + _end +} + +full() { + import -window root "$SCREENSHOT_NAME" + _end +} + +[ -d "$SCREENSHOT_DIR" ] || mkdir -p "$SCREENSHOT_DIR" + +[ "$#" -eq 0 ] && region + +while getopts ":f" arg; do + case "$arg" in + f) full ;; + *) printf "Invalid option: -%s\n" "$OPTARG" >&2 && exit 1 ;; + esac +done @@ -0,0 +1,24 @@ +#!/bin/sh + +# testzed: A useful utility for testing ZFS ZED configurations. + +if [ "$(id -u)" -ne 0 ]; then + echo "script requires root privileges" >&2 + exit 1 +fi + +FILE=/tmp/testzed_file +POOL="test" + +# Create throwaway pool. +dd if=/dev/zero of=$FILE bs=1 count=0 seek=512M +zpool create $POOL $FILE + +# Scrub the pool (which will finish instantly) to send out a notification. +# Ensure that ZED is configured with the option `ZED_NOTIFY_VERBOSE=1` to +# receive an email even if no errors occurred. +zpool scrub $POOL + +# Clean up after ourselves. +zpool export $POOL +rm $FILE @@ -0,0 +1,10 @@ +#!/bin/sh + +# weather: Quickly check the weather forecast. + +DEFAULT="grand+rapids" + +CITY=$(echo "$1" | tr ' ' '+') +: "${CITY:=$DEFAULT}" + +curl -s "https://wttr.in/$CITY" | head -n -2 |