-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_tunezd.pl
164 lines (139 loc) · 4.16 KB
/
old_tunezd.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use IPC::Open2;
# User Configuration Values:
# $hostname
# This is the hostname to connect to (or IP address). If you're running this
# on your own computer, simply use the default
my $hostname = "localhost";
# $control_password
# This MUST be the same thing as $control_password in config.inc.php.
# NOTE: This is NOT the same thing as the database password
my $control_password = "gandalf";
# $server_path
# This is the path within the server. If Tunez is running in the root
# directory of the webserver, just leave this alone. Here's an example:
# my $server_path = "~user/"
my $server_path = "tunez/";
my $URL = "http://$hostname/$server_path" . "admin_control.php";
# $mpg123_executable
# This is the path to the mpg123 executable and the important command line
# options, -R for remote control via a pipe and the 'asdf' can very well be
# anything, you have to pass something though. Don't ask me, read the mpg123
# manpage.. weird stuff.
my $mpg123_executable = "/usr/bin/mpg123-oss -b 1024 -R asdf";
my $ice_exec = "/usr/bin/shout";
# $mode
# blah blah blah . . need some comments
# options: mpg123, mplayer, icecast
my $mode = "mpg123";
# END user configuration variables.
my $ua = LWP::UserAgent->new();
my $pid;
my $line;
my $song;
my $flag=0;
my $debug=0;
local (*Reader, *Writer);
# Functions...
sub initialize($) {
my $executable = shift;
return open2(\*Reader, \*Writer, $executable);
}
sub pause {
print Writer "PAUSE";
}
sub cleanup($) {
my $pid = shift;
print Writer "QUIT";
close Writer;
close Reader;
waitpid($pid, 0);
}
sub queue() {
my $req = new HTTP::Request POST => $URL;
$req->content_type('application/x-www-form-urlencoded');
$req->content("auth_pw=$control_password&request=queue");
my $response = $ua->request($req);
my $song = $response->content;
print "got content: $song\n";
return $song;
}
sub advance() {
my $req = new HTTP::Request POST => $URL;
$req->content_type('application/x-www-form-urlencoded');
$req->content("auth_pw=$control_password&request=next");
my $response = $ua->request($req);
my $check_next = $response->content;
print "response after add: $check_next\n";
return $check_next;
}
sub set_random($) {
my $song_id = shift;
my $req = new HTTP::Request POST => $URL;
$req->content_type('application/x-www-form-urlencoded');
$req->content("auth_pw=$control_password&request=set_random&song_id=$song_id");
my $response = $ua->request($req);
my $check_next = $response->content;
print "response after set_random: $check_next\n";
return $check_next;
}
sub loop() {
my $data = queue();
$data =~ /random: (\d)\n/;
if ($1) { # song is random
$data =~ /song_id: (\d+)\n/;
my $song_id = $1;
my $response = set_random($song_id);
}
else { # song is normal
my $response = advance();
}
if ($data =~ /filename: (.*?)\n/) {
my $filename = $1;
if ($mode eq "mpg123") {
print Writer "LOAD $filename";
while ($line = <Reader>) {
if ($line =~ /No such file or directory/) {
return -1;
}
#elsif ($line =~ /ERROR/) {
# return -1;
#}
if ($line =~ /^\@P 0$/) {
last;
}
}
} elsif ($mode eq "icecast") {
`$ice_exec -P letmein "$filename"`;
}
}
return 0;
}
# end of Functions...
$pid = initialize($mpg123_executable);
if ($debug) {
my $req = new HTTP::Request POST => $URL;
$req->content_type('application/x-www-form-urlencoded');
$req->content("auth_pw=$control_password&request=print");
my $response = $ua->request($req);
my $song = $response->content;
print $song;
cleanup($pid);
exit;
}
while (1) {
$flag = loop();
if($flag == -1) {
# mpg123 died for some unknown reason
cleanup($pid);
if ($mode eq "mpg123") {
$pid = initialize($mpg123_executable);
}
}
}
cleanup($pid);
# END
1;