-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
124 lines (103 loc) · 3.87 KB
/
api.php
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
<?php
require_once __DIR__ . "/lib/MphpD/MphpD.php";
use FloFaber\MphpD\MphpD;
use FloFaber\MphpD\MPDException;
// Constants
define('MPD_HOST', 'localhost');
define('MPD_PORT', 6600);
define('MPD_PASSWORD', '');
// Global variables
$output_arr = array();
/***********************************************************************/
// Load settings file
$settings_json = file_get_contents("settings.json");
$settings_data = json_decode($settings_json, true);
// Load channel data
$stations = array();
foreach ($settings_data["stations"] as &$station) {
$stations[$station["url"]] = $station["name"];
}
// Configure and initialize MphpD client library
$mphpd = new MphpD([
"host" => MPD_HOST,
"port" => MPD_PORT,
"timeout" => 5
]);
try {
$mphpd_connected = $mphpd->connect();
} catch (MPDException $e) {
$output_arr['error'] = $e->getMessage();
$output_arr['status'] = 'ERROR';
}
// Process API calls
if ($mphpd_connected) {
if (isset($_GET['action'])) {
$action = $_GET['action'];
$parameters = isset($_GET['parameters']) ? $_GET['parameters'] : array();
switch ($action) {
case 'get_status':
$status = $mphpd->status();
$output_arr['content'] = $status;
$output_arr['status'] = 'OK';
break;
case 'get_volume':
$volume = $mphpd->player()->volume();
$output_arr['content'] = $volume;
$output_arr['status'] = 'OK';
break;
case 'get_songinfo':
$songinfo = $mphpd->player()->current_song();
if ($songinfo) {
$output_arr['content'] = array(
'song' => $songinfo['title'],
'streamurl' => (isset($stations[$songinfo['file']]) ? $stations[$songinfo['file']] : $songinfo['file'])
);
} else {
$output_arr['content'] = array(
'song' => 'PAUSED',
'streamurl' => 'Please select a channel'
);
}
$output_arr['status'] = 'OK';
break;
case 'set_station':
if (isset($parameters['streamurl'])) {
$streamurl = $parameters['streamurl'];
$mphpd->queue()->clear();
$mphpd->queue()->add($streamurl);
$mphpd->player()->play(0);
$output_arr['content'] = print_r($parameters['streamurl'], true);
$output_arr['status'] = 'OK';
} else {
$output_arr['error'] = 'no streamurl provided';
$output_arr['status'] = 'ERROR';
}
break;
case 'set_volume':
if (isset($parameters['volume'])) {
$volume = $parameters['volume'];
$mphpd->player()->volume(intval($volume));
} else {
$output_arr['error'] = 'no valid volume level provided';
$output_arr['status'] = 'ERROR';
}
$output_arr['content'] = '';
$output_arr['status'] = 'OK';
break;
default:
$output_arr['error'] = 'action doesn\'t exist';
$output_arr['status'] = 'ERROR';
}
} else {
$output_arr['error'] = 'no action defined';
$output_arr['status'] = 'ERROR';
}
} else {
$output_arr['error'] = 'MPD client not connected';
$output_arr['status'] = 'ERROR';
}
$output = json_encode($output_arr);
echo $output;
if ($output_arr['status'] == 'ERROR') {-
http_response_code(500);
}