-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplplay
executable file
·213 lines (186 loc) · 6.67 KB
/
plplay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/zsh
ID_FILE="$HOME/.scripts/playlist_ids.txt"
declare -a plist_arr
SHUFFLE="shuffle"
function print_usage {
echo "Usage: plplay [OPTION | command]"
echo "Select one of your mps-youtube playlists to start playing in the background using tmux"
echo ""
echo "Options:"
echo " -no-shuffle Play the selected playlist in its original order"
echo " --attach Attach to the created tmux session once playback starts"
echo " --help Display help information about using the 'plplay' script"
echo ""
echo "Commands:"
echo " stop Stop playback, killing the tmux session"
echo " update Select a playlist to update, then play the updated playlist"
echo " NOTE: The selected playlist must be an actual youtube playlist with"
echo " its playlist id stored in ./playlist_ids.txt following the format:"
echo ""
echo " '<mpsyt-playlist-name> <youtube-playlist-ID>'"
echo ""
echo " volup Increase the volume by 10"
echo " voldown Decrease the volume by 10"
}
function play() {
local pl_num=$1
local no_interrupt=${2:-0}
local pl_session=$(tmux list-sessions 2>/dev/null | grep -o "mpsyt_pl")
# Check if a current mpsyt_pl tmux session is already running
if [[ $pl_session ]]
then
# If $no_interrup is not set, kill the current mpsyt_pl session
# And unset the $pl_session variable
[[ $no_interrupt -eq 0 ]] && tmux kill-session -t mpsyt_pl && unset pl_session
fi
# Start a new mpsyt_pl tmux session
[[ -z $pl_session ]] && tmux new -s mpsyt_pl -d "mpsyt open $pl_num, dump, $SHUFFLE all"
}
function update() {
local pl_id="$1"
local pl_name="$2"
if [[ $1 ]] && [[ $2 ]]
then
# printf "tmux new -s update_mpsyt_pl -d \"mpsyt pl %s, dump, save %s, exit\n" "$pl_id" "$pl_name"
tmux new -s update_mpsyt_pl -d "mpsyt pl $pl_id, dump, save $pl_name, exit"
mpsyt pl $pl_id, dump, save $pl_name, exit > /dev/null &
wait
else
echo "ERR: update() given empty string"
fi
}
function volume_up() {
tmux send-keys -N 5 -t mpsyt_pl.0 0
}
function volume_down() {
tmux send-keys -N 5 -t mpsyt_pl.0 9
}
# get_index:
#
# Return the displayed index value of a specific mpsyt playlist
#
# Get the index value corresponding to the position of playlist name "$1" in the global
# playlist array: '$plist_arr'
#
# NOTE: the display order and thus indexing of the playlists is based on output
# generated from mpsyt, thus the index of a given playlist is subject to change
# across different calls to 'update'.
# It is for this reason that we MUST:
# - rebuild the playlist array each time
# - Store the playlist information according to its name
# - Store the playlists data in $ID_FILE using the format:
# <mpsyt playlist name> <YouTube playlist id>
#
# Parameters
# ----------
# $1 : string
# The name of the mpsyt playlist whose index we want to retrieve
#
# Returns
# -------
# $i : integer
# This should correspond to the index of the playlist as it was displayed
# on the menu when prompting the user for input.
function get_index() {
value="$1"
# Get playlist name from displayed index
for i in {1.."${#plist_arr[@]}"}
do
# echo "$i ${plist_arr[$i]} ${value}" 1>&2
if [[ "${plist_arr[$i]}" = "${value}" ]]
then
echo "${i}"
fi
done
}
function main() {
if [[ "$1" == "-h" ]] || [[ "$1" = "--help" ]]
then
print_usage
exit
fi
if [[ "$1" = "--no-shuffle" ]]
then
SHUFFLE=""
shift
fi
if [[ "$1" = "--attach" ]]
then
pl_num=$1
no_interrupt=$2
jl_session=$(tmux list-sessions 2>/dev/null | grep -o "mpsyt_pl")
if [[ $pl_session = "mpsyt_pl" ]]
then
[[ no_interrupt -eq 0 ]] && tmux kill-session -t mpsyt_pl
fi
[[ no_interrupt -eq 0 ]] && tmux new -s mpsyt_pl "mpsyt"
elif [[ $1 = "stop" ]]
then
tmux kill-session -t mpsyt_pl
elif [[ "$1" = "update" ]]
then
prompt=$(mpsyt ls, q | sed -n '/Local/,/Enter/p')
echo "$prompt" | head -n -2
printf "\nEnter ID to update a playlist\n"
# Preserve old IFS
# ORIG_IFS=$IFS
# plists=$(echo "$prompt" | tail -n +5 | head -n -2 | sed "s/^\s\+//g" | sed "s/\s\+/ /g" | cut -d " " -f 2)
# local IFS=$'\n' plist_arr=( $(echo "$prompt" | tail -n +5 | head -n -2 | sed "s/^\s\+//g" | sed "s/\s\+/ /g" | cut -d " " -f 2) )
# IFS=$ORIG_IFS
# zsh specific way of converting output to array
plists=$(echo "$prompt" | tail -n +5 | head -n -2 | sed "s/^\s\+//g" | sed "s/\s\+/ /g" | cut -d " " -f 2)
plist_arr=("${(f)plists}")
echo -n "> "
read pl_num
# if [[ -n $BASH_VERSION ]]
# then
# pl_num=$(($pl_num-1))
# fi
# Load the playlist id's into array from external '$ID_FILE'
# For reference the lines in '$ID_FILE' are formatted as follows:
# <mpsyt playlist name> <YouTube playlist id>
declare -a id_list
while IFS= read line
do
# Split the line into array:
# [<mpsyt playlist name>, <YouTube playlist id>]
cur=("${(@s/ /)line}")
# Calls get_index(<mpsyt playlist name>)
ind=$(get_index ${cur[1]})
# Make sure index is not NULL
# If returned index value is not NULL:
# store the playlists YouTube ID at index '$ind' in '$id_list'
# NOTE: ID's are stored in id list based on the position of the
# playlist in the global array '$plist_arr' NOT its position
# in '$id_list'.
[[ $ind ]] && id_list[$ind]="${cur[2]}" || echo "No id for ${cur[1]}"
done < $ID_FILE
pl_token="${id_list[$pl_num]}"
pl_name="${plist_arr[$pl_num]}"
# If the playlist has a corresponding YouTube ID in '$ID_FILE'
if [[ ! -z $pl_token ]]
then
update "$pl_token" "$pl_name"
fi
# Play the updated playlist
play $pl_num 1
elif [[ "$1" = "volup" ]]
then
volume_up
elif [[ "$1" = "voldown" ]]
then
volume_down
elif [[ ! -z "$1" ]]
then
printf "Unrecognized Option: '%s'\n" "$1"
print_usage
exit 1
else
mpsyt ls, q | sed -n '/Local/,/Enter/p' | head -n -2
printf "\nEnter <name or ID> to load a playlist\n"
echo -n "> "
read pl_num
play $pl_num
fi
}
main $@