-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcurrently_playing.php
107 lines (63 loc) · 2.06 KB
/
currently_playing.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
<?php
$frames = array(
"frames" => array(
array(
"text" => "?",
"icon" => "i647"
),
)
);
function output($text) {
global $frames;
$frames["frames"][0]["text"] = $text;
echo(json_encode($frames));
}
if (!function_exists("getallheaders")) {
// getallheaders is required to get the Authorization header as it does not appear
// in $_SERVER or anywhere else. Therefore, try updating all of your software -
// web server, PHP, etc. and try again.
// This works absolutely fine on Apache and PHP 7.
http_response_code("501 Not Implemented");
error_log("https://github.com/WilliamVenner/LaMetric-Spotify/issues/2#issuecomment-325140347");
return;
}
$headers = getallheaders();
if (!isset($headers["Authorization"])) {
output("X"); return;
}
$auth = $headers["Authorization"];
require("inc/api.php");
$Spotify = new Spotify($auth);
$result = $Spotify -> API("me/player/currently-playing", false, true);
if (isset($result["error"])) {
if ($result["error"]["message"] == "invalid id") {
output("[Local File]"); return;
}
if ($result["error"]["message"] == "The access token expired") {
header("HTTP/1.1 401 Unauthorized");
output("[X]"); return;
}
if ($result["error"]["message"] == "API rate limit exceeded") {
output("Too many requests"); return;
}
output("!"); return;
}
if (!isset($result["item"])) {
output("[X]"); return;
}
$track_info = $result["item"];
if ($result["is_playing"] != true && $_GET["show-paused"] == "false") {
output("[II]"); return;
}
$s = "";
if ($_GET["show-paused"] == "true" && $result["is_playing"] == false) $s .= "[II] ";
if ($_GET["show-artists"] == "true" && (($_GET["show-paused"] == "true" && $result["is_playing"] == false) || $result["is_playing"] == true)) {
foreach($track_info["artists"] as $i => $artist) {
if ($i > 0 && count($track_info["artists"]) > 1) $s .= ", ";
$s .= $artist["name"];
}
$s .= " - ";
}
$s .= $track_info["name"];
output($s);
?>