URI: 
       ytdl-mpd - 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
       ---
       ytdl-mpd (3096B)
       ---
            1 #!/bin/sh
            2 # fetch a media file, tag it with metadata, and add it to mpd queue
            3 # requirements: yt-dlp, id3tag
            4 
            5 set -e
            6 
            7 musicroot="/br/radio/music"
            8 
            9 die() {
           10         printf '%s: error: %s\n' "${0##*/}" "$1" >&2
           11         exit 1
           12 }
           13 
           14 show_help() {
           15         printf 'usage: %s [OPTIONS] [URL ..]\n' "${0##*/}"
           16         echo "downloads music from the web and tags it. Playlists are supported."
           17         echo "If no URLs are supplied as arguments, they are expected as stdin."
           18         echo
           19         echo "OPTIONS are one or more of the following:"
           20         echo "  -a  add metadata without interaction"
           21         echo "  -h  show this message"
           22         echo "  -m  move output music file to $musicroot"
           23         echo "  -q  add to mpd queue (requires -m)"
           24         echo "  -t  download through torsocks(1)"
           25         echo "  --  do not consider any following arguments as options"
           26 }
           27 
           28 ytdl() {
           29         $prefix yt-dlp \
           30                 --extract-audio \
           31                 --audio-quality 0 \
           32                 --audio-format mp3 \
           33                 --add-metadata \
           34                 --output "%(playlist_index)s - %(title)s.%(ext)s" \
           35                 "$1" 
           36 }
           37 
           38 handle_url() {
           39         oldpwd="$PWD"
           40         outputdir="$(mktemp -d)"
           41         mkdir -p "$outputdir"
           42         cd "$outputdir"
           43 
           44         album="$(ytdl "$1" | grep 'Finished downloading' | sed 's/.*: //')"
           45 
           46         if [ $? -ne 0 ]; then
           47                 die "youtube-dl error"
           48         fi
           49 
           50         if [ "$(ls *.mp3 2>/dev/null | wc -l)" -lt 1 ]; then
           51                 printf "youtube-dl error" "$1"
           52                 if [ -n "$DISPLAY" ]; then
           53                         notify -u CRITICAL "youtube-dl error" "$1"
           54                 fi
           55                 exit 1
           56         fi
           57 
           58         if [ "$auto" = 0 ]; then
           59                 printf "add metadata? [Y/n] "
           60                 read
           61         else
           62                 REPLY=y
           63         fi
           64 
           65         case "$REPLY" in
           66                 N|n)
           67                         mv ./*.mp3 "$oldpwd/"
           68                         cd -
           69                         exit 0;;
           70         esac
           71 
           72         artist="Unknown Artist"
           73         album="${album:-Unknown Album}"
           74         track=1
           75 
           76         # Loop over files with spaces
           77         SAVEIFS=$IFS
           78         IFS=$(printf '\n\b')
           79         for f in *.mp3; do
           80 
           81                 [ "$track" = 1 ] && \
           82                         artist="$(printf '%s' "$f" | \
           83                                 awk -F'-' '{gsub(/^ +/, "", $2); gsub(/ +$/, "", $2); print $2}')"
           84 
           85 
           86                 song=$(printf '%s' "$f" | sed 's/.* - //; s/^ //; s/\.mp3//')
           87 
           88                 if [ "$auto" = 0 ]; then
           89                         printf 'file: %s\n' "$f"
           90 
           91                         printf 'song [%s]: ' "$song"
           92                         read
           93                         song="${REPLY:-$song}"
           94 
           95                         printf 'track [%s]: ' "$track"
           96                         read
           97                         track="${REPLY:-$track}"
           98 
           99                         printf 'album [%s]: ' "$album"
          100                         read
          101                         album="${REPLY:-$album}"
          102 
          103                         printf 'artist [%s]: ' "$artist"
          104                         read
          105                         artist="${REPLY:-$artist}"
          106                 fi
          107 
          108                 id3tag --artist="$artist" --album="$album" \
          109                         --song="$song" --track="$track" \
          110                         "$f" 2>&1 >/dev/null
          111 
          112                 track=$(( track + 1 ))
          113 
          114                 if [ "$move_music" = 1 ]; then
          115                         outdir="$musicroot/$artist/$album"
          116                         mkdir -p "$outdir"
          117                         mv "${f}" "$outdir"
          118                         mpc update --wait >/dev/null
          119                         if [ "$queue" = 1 ]; then
          120                                 mpc findadd \
          121                                         artist "$artist" \
          122                                         album "$album" \
          123                                         title "$song"
          124                         fi
          125                 else
          126                         mv "${f}" "$oldpwd/"
          127                 fi
          128         done
          129 
          130         IFS=$SAVEIFS
          131 
          132         rmdir "$outputdir"
          133         cd - >/dev/null
          134 }
          135 
          136 prefix=""
          137 auto=0
          138 move_music=0
          139 queue=0
          140 while :; do
          141         case "$1" in
          142                 -a)
          143                         auto=1;;
          144                 -h)
          145                         show_help
          146                         exit 0;;
          147                 -m)
          148                         move_music=1;;
          149                 -q)
          150                         queue=1;;
          151                 -t)
          152                         prefix="torsocks";;
          153                 --)
          154                         shift
          155                         break;;
          156                 -?*)
          157                         die "unknown option specified: $1";;
          158                 *)
          159                         break
          160         esac
          161         shift
          162 done
          163 
          164 if [ $# -lt 1 ]; then
          165         urls="$(cat)"
          166 else
          167         urls="$@"
          168 fi
          169 
          170 for u in "$urls"; do
          171         handle_url "$u"
          172 done