add radio-play option - annna - Annna the nice friendly bot. HTML git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/annna/ DIR Log DIR Files DIR Refs DIR Tags DIR README --- DIR commit 43ac6c7ed634ca428ff807b06a9206ecf556c67f DIR parent 73e548850a2dc3dc7288f60679b7f468874b57b5 HTML Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Fri, 9 Aug 2024 16:58:22 +0200 add radio-play option Signed-off-by: Annna Robert-Houdin <annna@bitreich.org> Diffstat: M annna-message-common | 7 +++++++ A ytdl-mpd | 171 +++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 0 deletions(-) --- DIR diff --git a/annna-message-common b/annna-message-common @@ -542,6 +542,13 @@ case "${text}" in suri="$(printf "%s\n" "${word}" | bitreich-speak)" annna-say -s "${server}" -c "${channel}" "${suri}" ;; +"${ircuser}, please radio play "*) + url="$(printf "%s\n" "${text}" | cut -c 25- | sed 's,\t, ,g')" + { + ytdl-mpd -a -m -q "$url" + annna-say -s "${server}" -c "${channel}" "Please listen on gophers://bitreich.org/9/radio/listen" + } & + ;; "${ircuser}, please radio say "*) word="$(printf "%s\n" "${text}" | cut -c 25- | sed 's,\t, ,g')" annna-say -s "${server}" -c "${channel}" "Please listen on gophers://bitreich.org/9/radio/listen" DIR diff --git a/ytdl-mpd b/ytdl-mpd @@ -0,0 +1,171 @@ +#!/bin/sh +# fetch a media file, tag it with metadata, and add it to mpd queue +# requirements: yt-dlp, id3tag + +set -e + +musicroot="/br/radio/music" + +die() { + printf '%s: error: %s\n' "${0##*/}" "$1" >&2 + exit 1 +} + +show_help() { + printf 'usage: %s [OPTIONS] [URL ..]\n' "${0##*/}" + echo "downloads music from the web and tags it. Playlists are supported." + echo "If no URLs are supplied as arguments, they are expected as stdin." + echo + echo "OPTIONS are one or more of the following:" + echo " -a add metadata without interaction" + echo " -h show this message" + echo " -m move output music file to $musicroot" + echo " -q add to mpd queue (requires -m)" + echo " -t download through torsocks(1)" + echo " -- do not consider any following arguments as options" +} + +ytdl() { + $prefix yt-dlp \ + --extract-audio \ + --audio-quality 0 \ + --audio-format mp3 \ + --add-metadata \ + --output "%(playlist_index)s - %(title)s.%(ext)s" \ + "$1" +} + +handle_url() { + oldpwd="$PWD" + outputdir="$(mktemp -d)" + mkdir -p "$outputdir" + cd "$outputdir" + + album="$(ytdl "$1" | tee /dev/tty | grep 'Finished downloading' | sed 's/.*: //')" + + if [ $? -ne 0 ]; then + die "youtube-dl error" + fi + + if [ "$(ls *.mp3 2>/dev/null | wc -l)" -lt 1 ]; then + printf "youtube-dl error" "$1" + if [ -n "$DISPLAY" ]; then + notify -u CRITICAL "youtube-dl error" "$1" + fi + exit 1 + fi + + if [ "$auto" = 0 ]; then + printf "add metadata? [Y/n] " + read + else + REPLY=y + fi + + case "$REPLY" in + N|n) + mv ./*.mp3 "$oldpwd/" + cd - + exit 0;; + esac + + artist="Unknown Artist" + album="${album:-Unknown Album}" + track=1 + + # Loop over files with spaces + SAVEIFS=$IFS + IFS=$(printf '\n\b') + for f in *.mp3; do + + [ "$track" = 1 ] && \ + artist="$(printf '%s' "$f" | \ + awk -F'-' '{gsub(/^ +/, "", $2); gsub(/ +$/, "", $2); print $2}')" + + printf 'file: %s\n' "$f" + + song=$(printf '%s' "$f" | sed 's/.* - //; s/^ //; s/\.mp3//') + + if [ "$auto" = 0 ]; then + printf 'song [%s]: ' "$song" + read + song="${REPLY:-$song}" + + printf 'track [%s]: ' "$track" + read + track="${REPLY:-$track}" + + printf 'album [%s]: ' "$album" + read + album="${REPLY:-$album}" + + printf 'artist [%s]: ' "$artist" + read + artist="${REPLY:-$artist}" + fi + + id3tag --artist="$artist" --album="$album" \ + --song="$song" --track="$track" \ + "$f" + + track=$(( track + 1 )) + done + + IFS=$SAVEIFS + + if [ "$move_music" = 1 ]; then + outdir="$musicroot/$artist/$album" + mkdir -p "$outdir" + mv ./*.mp3 "$outdir" + mpc update --wait >/dev/null + if [ "$queue" = 1 ]; then + mpc findadd \ + artist "$artist" \ + album "$album" \ + title "$song" + fi + else + mv ./*.mp3 "$oldpwd/" + fi + + rmdir "$outputdir" + cd - >/dev/null +} + +prefix="" +auto=0 +move_music=0 +queue=0 +while :; do + case "$1" in + -a) + auto=1;; + -h) + show_help + exit 0;; + -m) + move_music=1;; + -q) + queue=1;; + -t) + prefix="torsocks";; + --) + shift + break;; + -?*) + die "unknown option specified: $1";; + *) + break + esac + shift +done + +if [ $# -lt 1 ]; then + urls="$(cat)" +else + urls="$@" +fi + +for u in "$urls"; do + handle_url "$u" +done