-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBasm.sh
executable file
·235 lines (184 loc) · 5.4 KB
/
Basm.sh
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
# Copyright (c) 2010, 2012 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
BASM_RC="$HOME/.Basm.rc"
OAuth_sh=$(which StereomoodOAuth.sh)
(( $? != 0 )) && echo 'Unable to locate StereomoodOAuth.sh! Make sure it is in searching PATH.' && exit 1
source "$OAuth_sh"
# If you are going to develop based on this, please use your own key and
# secret.
oauth_consumer_key='3727e42eb4dfc4d53db038e02f3e357404bf6557a'
oauth_consumer_secret='ac1a13e8adf36343a0f6673115a3d3d8'
usage () {
echo "usage: $0 options
OPTIONS:
-h Show this message
-q Query text
-t Type: mood, activity, or site
Example: $0 -q happy -t mood
"
exit $1
}
load_config () {
[[ -f "$BASM_RC" ]] && . "$BASM_RC"
SM_init
if [[ "$oauth_token" == "" ]] || [[ "$oauth_token_secret" == "" ]]; then
SM_access_token_helper
if (( $? == 0 )); then
oauth_token=${SM_ret[0]}
oauth_token_secret=${SM_ret[1]}
if [[ "$oauth_token" == "" ]] || [[ "$oauth_token_secret" == "" ]]; then
echo -e "Couldn't get the token"!
exit 1
fi
echo "oauth_token='${SM_ret[0]}'" >> "$BASM_RC"
echo "oauth_token_secret='${SM_ret[1]}'" >> "$BASM_RC"
echo "Token saved to $BASM_RC."
echo
else
echo 'Unable to get access token'
exit 1
fi
chmod 600 "$BASM_RC"
fi
}
quit () {
# Show cursor
echo -e "\033[?25h"
# Echo for stdin
stty echo
echo
exit $1
}
listen () {
SM_get_search_total "$1" "$2"
(( $SM_rval != 0 )) && echo "$SM_ret" && exit $SM_rval
total=$SM_ret
echo "Keys: P - Pause | A - Add to library | N - Next song | Q - Quit"
echo
echo "$total songs in $1 $2"
echo
while true; do
# 1-indexed
page=$(( $RANDOM * total / 32768 + 1 ))
SM_search 'xml' "$1" "$2" '1' "$page"
total=$(SM_extract_XML_value 'total' "$SM_ret")
song_id=$(SM_extract_XML_value 'id' "$SM_ret")
song_title=$(SM_extract_XML_value 'title' "$SM_ret")
song_artist=$(SM_extract_XML_value 'artist' "$SM_ret")
song_album=$(SM_extract_XML_value 'album' "$SM_ret")
song_url=$(SM_extract_XML_value 'url' "$SM_ret")
song_image_url=$(SM_extract_XML_value 'image_url' "$SM_ret")
song_audio_url=$(SM_extract_XML_value 'audio_url' "$SM_ret")
song_post_url=$(SM_extract_XML_value 'post_url' "$SM_ret")
# Clean up because current line may be 'Next song...'
echo -e "\033[0G\033[A\033[2K\n\033[2KID: $song_id"
echo "Title: $song_title"
echo "Artist: $song_artist"
echo "Album: $song_album"
echo "URL: $song_url"
echo "Audio: $song_audio_url"
echo
exec 8> >(mplayer -msglevel all=5 "$song_audio_url" | while read -d \[ line; do
ret=$(echo -n "$line" | egrep "^J.A:")
[[ "$ret" != "" ]] && echo -ne "\033[2K\033[0G$ret"
done)
subshell_pid=$!
t_start=$(date +%s)
finished_song=
while true; do
ps $subshell_pid &> /dev/null
(( $? != 0 )) && finished_song=1 && break
read -n 1 -t 1 ch
[[ $? -gt 0 ]] && continue
case "$ch" in
a|A) SM_user_library_song 'xml' "$song_id"
echo -e "\033[2K\033[0G$(SM_extract_XML_value 'message' "$SM_ret")"
continue
;;
p|P) echo -n 'p' >&8
sleep 0.1
echo -ne "\033[2K\033[0G === PAUSED ===\033[0G"
continue
;;
n|N) echo -n 'q' >&8
sleep 0.1
echo -ne "\033[A\033[2K\n"
echo -ne "\033[2K\033[0G"
break
;;
q|Q) echo -n 'q' >&8
sleep 0.1
echo -ne "\033[A\033[2K\n"
echo -ne "\033[2K\033[0G"
break
;;
esac
done
exec 8>&-
t_diff=$(( $(date +%s) - $t_start))
# finished listening?
# FIXME this is not a good way to make it
if [[ "$finished_song" != "" ]] && (( t_diff >= 90 )); then
echo -ne "\033[2K\033[0GAdding listen...\033[0G"
SM_song_listen 'xml' "$song_id"
echo -e "\033[2K$(SM_extract_XML_value 'message' "$SM_ret")\n"
fi
case "$ch" in
n|N) echo -ne "Next song...\033[0G"
continue
;;
q|Q) echo -ne "Quiting..."
break
;;
esac
done
}
main () {
echo "Basm: A Bash client for stereomood"
echo
load_config
# Catch program exiting and keyboard interrupt (Ctrl+C)
trap quit INT EXIT
# Catch window size changing
trap sig_winch WINCH
# No echo for stdin
stty -echo
# Hide cursor
echo -ne "\033[?25l"
basm_q=
basm_type=
while getopts "q:t:h" name
do
case $name in
q) basm_q="$OPTARG";;
t) basm_type="$OPTARG";;
h) usage 0
;;
?) usage
exit 2;;
esac
done
[[ "$basm_q" == "" ]] && usage 1
[[ "$basm_type" == "" ]] && uasge 1
listen "$basm_q" "$basm_type"
return 0
}
main "$@"