From 4bbb849a30403a13c0556b2f29218722c2cf1621 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 29 Jun 2022 12:21:30 -0500 Subject: Initial commit --- COPYING | 7 ++++ Makefile | 25 ++++++++++++++ radio | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 COPYING create mode 100644 Makefile create mode 100755 radio diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..4d13a5b --- /dev/null +++ b/COPYING @@ -0,0 +1,7 @@ +Copyright (C) 2022 Case Duckworth + +Usage of the works is permitted provided that this instrument is retained with +the works, so that any entity that uses the works is notified of this +instrument. + +DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..66e6c52 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +# Radio + +DESTDIR = +PREFIX = /usr/local + +.PHONY: help +help: + @echo "radio : Play online radio" + @echo "(C) 2022 Case Duckworth " + @echo "Licensed under the Fair License; see COPYING for details." + @echo + @echo "TARGETS:" + @echo " install Install radio to $(DESTDIR)$(PREFIX)/radio." + @echo " An example configuration is at $(DESTDIR)$(PREFIX)/share/radio/stations." + @echo " uninstall Uninstall radio-related files." + +.PHONY: install +install: radio radio.stations + install -D radio $(PREFIX)/bin/radio + install -D radio.stations $(PREFIX)/share/radio/stations + +.PHONY: uninstall +uninstall: + rm $(PREFIX)/bin/radio + rm -r $(PREFIX)/share/radio/ diff --git a/radio b/radio new file mode 100755 index 0000000..dc1297a --- /dev/null +++ b/radio @@ -0,0 +1,116 @@ +#!/usr/bin/env sh +# -*- sh -*- + +: "${RADIO_STATIONS:=${XDG_CONFIG_HOME:-$HOME/.config}/radio/stations}" +: "${RADIO_PLAYER:=$(command -v mpv 2>/dev/null)}" + +_radio_url=1 +_radio_desc=2 +_radio_tags=3 + +RADIO_PID_FILE=/tmp/radio.pid + +usage() { + cat < " radio + if ! (echo "$radio" | grep -qE '^[0-9]+$'); then + exit 1 + fi + station="$(printf '%s\n' "$candidates" | head -n "$radio" | tail -n 1)" + else + station="$candidates" + fi + echo >&2 "Playing $(echo "$station" | cut -f $_radio_desc)..." + "$RADIO_PLAYER" "$(echo "$station" | cut -f $_radio_url)" & + echo $! >"$RADIO_PID_FILE" + exit +} + +_radio_kill() { + if ! xargs -a "$RADIO_PID_FILE" kill; then + echo >&2 "I don't think radio is running." + exit 1 + fi + exit +} + +_radio_edit() { + exec $EDITOR "$RADIO_STATIONS" +} + +_radio_list() { + sed 1d "$RADIO_STATIONS" | + grep "$1" | + awk ' + BEGIN { FS="\t"; } + $0 !~ /^#/ { + descstr = $'$_radio_desc'; + urlstr = $'$_radio_url'; + tagstr = $'$_radio_tags'; + printf "%-23s\t", substr(descstr,1,20) (length(descstr)>20?"...":""); + printf "%-23s\t", substr(tagstr,1,20) (length(tagstr)>20?"...":""); + printf "%-23s\n", substr(urlstr,1,20) (length(urlstr)>20?"...":""); + }' + exit +} + +main() { + while getopts :khel: opt; do + case $opt in + h) usage ;; + k) _radio_kill ;; + e) _radio_edit ;; + l) _radio_list "$OPTARG" ;; + :) + case "$OPTARG" in + l) _radio_list ;; + *) + echo >&2 "Option -$OPTARG requires an argument" + exit 1 + ;; + esac + ;; + \?) + echo >&2 "Uknown option: -$OPTARG" + exit 1 + ;; + esac + done + shift $((OPTIND - 1)) + _radio_play "$@" +} + +main "$@" -- cgit 1.4.1-21-gabe81