-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.cgi
executable file
·247 lines (187 loc) · 4.78 KB
/
spotify.cgi
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
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/perl
# control spotify via cgi and Applescript
use CGI qw/:standard *table/;
my $dim_amount = 30;
my $delta_volume = 10;
print header(
-charset => 'UTF-8',
),
start_html(
-title => 'Spot',
-head => meta ({
-http_equiv => 'refresh',
-content => '60;url=http://donkey.cissme.com:8080/cgi-bin/spotify.cgi',
})
),
p,
img {src=>'/spotify-logo.png'},
'<font size="6">Spotify Control</font>',
hr,
p,
start_table,
"<TR>",
"<TD>", start_form, submit('action', 'play'), end_form, "</TD>",
"<TD>", start_form, submit('action', 'pause'), end_form, "</TD>",
"</TR>",
"<TR>",
"<TD>", start_form, submit('action', 'dim volume'), end_form, "</TD>",
"<TD>", start_form, submit('action', 'undim'), end_form, "</TD>",
"</TR>",
"<TR>",
"<TD>", start_form, submit('action', 'louder'), end_form, "</TD>",
"<TD>", start_form, submit('action', 'quieter'), end_form, "</TD>",
"</TR>",
"<TR>",
"<TD>", start_form, submit('action', 'next'), end_form, "</TD>",
"<TD>", start_form, submit('action', 'previous'), end_form, "</TD>",
"</TR>",
"<TR>",
"<TD>", start_form, submit('action', "what's playing"), end_form, "</TD>",
"</TR>",
end_table,
hr;
if (param()) {
if (param('action') =~ /play/) {
play();
} elsif (param('action') =~ /pause/ ) {
pause();
} elsif (param('action') =~ /previous/ ) {
play_previous();
} elsif (param('action') =~ /next/ ) {
play_next();
} elsif (param('action') =~ /^dim volume/ ) {
dim_volume();
} elsif (param('action') =~ /undim/ ) {
undim_volume();
} elsif (param('action') =~ /^louder$/ ) {
increase_volume();
} elsif (param('action') =~ /^quieter$/ ) {
decrease_volume();
};
show_status();
} else {
show_status();
}
sub show_status {
print p, "Now ", get_state(), " - ", "<I>", get_track(), ,"</I>", " by ", "<I>", get_artist(), "</I>",
p, "Volume: ", get_volume();
}
####
sub osascript {
return unless (defined($_[0]));
my $script = $_[0];
$retval = (`/usr/bin/osascript -e '$script'`);
chomp $retval;
return $retval;
}
sub get_artist {
my $script=<<_END_;
tell application "Spotify"
set output to artist of current track
end tell
_END_
osascript $script;
}
sub get_track {
my $script=<<_END_;
tell application "Spotify"
set output to name of current track
end tell
_END_
osascript $script;
}
sub get_state {
my $script=<<_END_;
tell application "System Events"
set MyList to (name of every process)
end tell
if (MyList contains "Spotify") is true then
tell application "Spotify"
set output to player state
end tell
else
set output to "not running"
end if
_END_
osascript $script;
}
sub get_volume {
my $script=<<_END_;
tell application "Spotify"
set output to sound volume
end tell
_END_
osascript $script;
}
sub set_volume {
return unless (defined($_[0]));
my $new_volume = $_[0];
my $script=<<_END_;
tell application "Spotify"
set sound volume to $new_volume
end tell
_END_
osascript $script;
}
sub increase_volume {
my $script=<<_END_;
tell application "Spotify"
set sound volume to (sound volume + $delta_volume)
end tell
_END_
osascript $script;
}
sub decrease_volume {
my $script=<<_END_;
tell application "Spotify"
set sound volume to (sound volume - $delta_volume)
end tell
_END_
osascript $script;
}
sub dim_volume {
my $script=<<_END_;
tell application "Spotify"
set sound volume to (sound volume - $dim_amount)
end tell
_END_
osascript $script;
}
sub undim_volume {
my $script=<<_END_;
tell application "Spotify"
set sound volume to (sound volume + $dim_amount)
end tell
_END_
osascript $script;
}
sub play_pause {
my $script=<<_END_;
tell application "Spotify" to playpause
_END_
osascript $script;
}
sub play {
my $script=<<_END_;
tell application "Spotify" to play
_END_
osascript $script;
}
sub pause {
my $script=<<_END_;
tell application "Spotify" to pause
_END_
osascript $script;
}
sub play_next {
my $script=<<_END_;
tell application "Spotify" to next track
_END_
osascript $script;
}
sub play_previous {
my $script=<<_END_;
tell application "Spotify" to previous track
_END_
osascript $script;
}