aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md13
-rwxr-xr-xbackground14
-rwxr-xr-xfman5
-rwxr-xr-xgeoip66
-rwxr-xr-xmpc_update25
-rwxr-xr-xrecord23
-rwxr-xr-xsnapshot37
-rwxr-xr-xtestzed24
-rwxr-xr-xweather10
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"
diff --git a/fman b/fman
new file mode 100755
index 0000000..c07db14
--- /dev/null
+++ b/fman
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+# fman: Fuzzy find man pages! Requires `fzf`.
+
+apropos . | fzf --prompt "Select program: " | cut -d" " -f 1 | xargs man
diff --git a/geoip b/geoip
new file mode 100755
index 0000000..0378733
--- /dev/null
+++ b/geoip
@@ -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"
diff --git a/record b/record
new file mode 100755
index 0000000..051765d
--- /dev/null
+++ b/record
@@ -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
diff --git a/testzed b/testzed
new file mode 100755
index 0000000..ffab7e9
--- /dev/null
+++ b/testzed
@@ -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
diff --git a/weather b/weather
new file mode 100755
index 0000000..c49e3ae
--- /dev/null
+++ b/weather
@@ -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