dotfiles

regexghost dotfiles and scripts
git clone https://git.regexghost.com/dotfiles.git
Log | Files | Refs | README

music.sh (2948B)


      1 #!/usr/bin/env sh
      2 
      3 # Music playing script
      4 
      5 DMENU_SCRIPT="$XDG_DATA_HOME/regexghost/wm-scripts/dmenu-runner.sh"
      6 MUSIC_DIR="$HOME/Music"
      7 FAVOURITES_DIR="$MUSIC_DIR/Favourites"
      8 
      9 mocp_command="mocp -M $XDG_CONFIG_HOME/moc"
     10 
     11 ping_panel () {
     12 	barpid="$(cat ~/.cache/bar_pid)"
     13 	if ! [ "$barpid" = "" ]; then
     14 		kill -35 "$barpid"
     15 	fi
     16 }
     17 
     18 # Technically these first 4 are unnecessary, could just bind directly to the command
     19 # but I like it all being in one file
     20 if [ "$1" = "--toggle-pause" ]; then
     21 	$mocp_command --toggle-pause
     22 	ping_panel
     23 	exit
     24 elif [ "$1" = "--quit" ]; then
     25 	$mocp_command --stop
     26 	ping_panel
     27 	exit
     28 elif [ "$1" = "--next" ]; then
     29 	$mocp_command --next
     30 	ping_panel
     31 	exit
     32 elif [ "$1" = "--previous" ]; then
     33 	$mocp_command --previous
     34 	ping_panel
     35 	exit
     36 elif [ "$1" = "--favourite" ]; then
     37 	song="$($mocp_command -i | grep "File:" | sed 's/File: //g')"
     38 	echo "$song" | grep -q "CurrentPlaylist/" && exit
     39 	favourite_path="$(echo "$song" | sed "s|$MUSIC_DIR/||g")"
     40 	favourite_dir="$(dirname "$favourite_path")"
     41 	mkdir -p "$FAVOURITES_DIR/${favourite_dir}"
     42 	ln -sf "$song" "$FAVOURITES_DIR/${favourite_path}"
     43 	notify-send "Added to favourites"
     44 	exit
     45 elif [ "$1" = "--get-song" ]; then
     46 	song="$($mocp_command -i 2> /dev/null | awk '/^Title:/ {S1 = ""; printf $0}')"
     47 	[ "$song" = "" ] && echo "None" && exit
     48 	echo "$song"
     49 	exit
     50 fi
     51 
     52 # Start mocp if not running, and set mode to shuffle
     53 if ! pgrep mocp 2> /dev/null > /dev/null; then
     54 	$mocp_command -S -M "$XDG_CONFIG_HOME/moc"
     55 	$mocp_command -t shuffle,repeat
     56 fi
     57 
     58 # If mocp is currently playing music, just notify-send the song title
     59 if ! $mocp_command -i | grep -q "State: STOP"; then
     60 	mocp_i="$($mocp_command -i)"
     61 	song_name="$(echo "$mocp_i" | grep "^Title: " | sed 's/Title: //g')"
     62 	total="$(echo "$mocp_i" | awk '/^TotalSec/ {print $2}')"
     63 	current="$(echo "$mocp_i" | awk '/^CurrentSec/ {print $2}')"
     64 	progress=$(echo "$current/$total*100" | bc -l | cut -d "." -f 1)
     65 	notify-send -i emblem-music-symbolic "Currently Playing:" "$song_name" -h "int:value:${progress}"
     66 	exit
     67 fi
     68 
     69 if [ "$1" = "--choice" ]; then
     70 	# Ask for playlist
     71 	playlist="$(ls "$MUSIC_DIR" | sed 's/\([A-Z][a-z]\)/ \1/g' | sed 's/\([a-z]\)\([0-9]\)/\1 \2/g' | sed 's/^ //g' | "${DMENU_SCRIPT}" "Select Playlist:")"
     72 	[ "$?" != "0" ] && exit
     73 	playlist_path="$MUSIC_DIR/$(echo "$playlist" | sed 's/ //g')"
     74 else
     75 	playlist_path="$MUSIC_DIR/CurrentPlaylist"
     76 fi
     77 
     78 # Check for sub playlists
     79 if [ $(find "$playlist_path" -type d | wc -l) -ne 1 ]; then
     80 	playlist="$(ls "$playlist_path" | sed 's/\([A-Z][a-z]\)/ \1/g' | sed 's/\([a-z]\)\([0-9]\)/\1 \2/g' | sed 's/^ //g' | awk 'BEGIN {RS = ""} {print "All Songs\n"$0}' | "${DMENU_SCRIPT}" "Select Playlist:")"
     81 	[ "$?" != "0" ] && exit
     82 	# If all songs, don't change playlist path
     83 	if ! [ "$playlist" = "All Songs" ]; then
     84 		playlist_path="$playlist_path/$(echo "$playlist" | sed 's/ //g')"
     85 	fi
     86 fi
     87 
     88 $mocp_command --clear
     89 $mocp_command --append "$playlist_path"
     90 $mocp_command --play
     91 ping_panel