-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.php
262 lines (237 loc) · 9.78 KB
/
index.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?PHP
$user = 'lastfm username';
$key = 'lastfm apikey';
$status = 'Last Played';
$endpoint = 'https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=' . $user . '&limit=2&api_key=' . $key . '&format=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // 0 for indefinite
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10 second attempt before timing out
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$response = curl_exec($ch);
$lastfm[] = json_decode($response, true);
curl_close($ch);
// Get the album artwork.
$artwork = $lastfm[0]['recenttracks']['track'][0]['image'][3]['#text'];
if ( empty( $artwork ) ) { // Check if album artwork exists on last.fm, otherwise use a placeholder
$artwork = 'artwork-placeholder.png';
}
else { // change the size to the largest available.
$artwork = str_replace('300x300', '600x600', $artwork);
}
// Populate array with the returned information.
$trackInfo = [
'name' => $lastfm[0]['recenttracks']['track'][0]['name'],
'album' => $lastfm[0]['recenttracks']['track'][0]['album']['#text'],
'artist' => $lastfm[0]['recenttracks']['track'][0]['artist']['#text'],
'albumArt' => $artwork,
'status' => $status
];
// Check if we are currently playing a song.
if ( !empty($lastfm[0]['recenttracks']['track'][0]['@attr']['nowplaying']) ) {
$trackInfo['nowPlaying'] = $lastfm[0]['recenttracks']['track'][0]['@attr']['nowplaying'];
$trackInfo['status'] = 'Now Playing';
}
// We are currently playing a song, get more information about it.
if($trackInfo['status'] == "Now Playing") {
// Get the number of plays for the track.
$endpoint = 'https://ws.audioscrobbler.com/2.0/?method=track.getInfo&username=' . $user . '&api_key=' . $key . '&artist=' . urlencode($trackInfo['artist']) . '&track=' . urlencode($trackInfo['name']) . '&format=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // 0 for indefinite
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10 second attempt before timing out
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$response = curl_exec($ch);
$track[] = json_decode($response, true);
curl_close($ch);
// Get the number of plays for this track.
$plays = $track[0]['track']['userplaycount'];
if($plays > 0) {
$playcount = $plays . ' Plays';
}
// Check if it's a loved song.
$loved = '';
if($track[0]['track']['userloved'] == 1) {
$loved = ' ❤️';
}
// We are playing something, let's check if the song changes every 15 seconds.
$duration = 15000;
}
// We are not currently playing a song, let's show some stats instead.
if($trackInfo['status'] != "Now Playing") {
// Get information about the user.
$endpoint = 'https://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=' . $user . '&api_key=' . $key . '&format=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // 0 for indefinite
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10 second attempt before timing out
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$response = curl_exec($ch);
$stats[] = json_decode($response, true);
curl_close($ch);
$username = $stats[0]['user']['realname'];
$playcount = number_format($stats[0]['user']['playcount']);
// Get infromation about the last played albums.
$endpoint = 'https://ws.audioscrobbler.com/2.0/?method=user.gettopalbums&user=' . $user . '&api_key=' . $key . '&period=7day&limit=12&format=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // 0 for indefinite
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10 second attempt before timing out
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$response = curl_exec($ch);
$albumcharts[] = json_decode($response, true);
curl_close($ch);
// Get the top 12 albums and their artwork.
$album_1 = $albumcharts[0]['topalbums']['album'][0]['image'][3]['#text'];
$album_2 = $albumcharts[0]['topalbums']['album'][1]['image'][3]['#text'];
$album_3 = $albumcharts[0]['topalbums']['album'][2]['image'][3]['#text'];
$album_4 = $albumcharts[0]['topalbums']['album'][3]['image'][3]['#text'];
$album_5 = $albumcharts[0]['topalbums']['album'][4]['image'][3]['#text'];
$album_6 = $albumcharts[0]['topalbums']['album'][5]['image'][3]['#text'];
$album_7 = $albumcharts[0]['topalbums']['album'][6]['image'][3]['#text'];
$album_8 = $albumcharts[0]['topalbums']['album'][7]['image'][3]['#text'];
$album_9 = $albumcharts[0]['topalbums']['album'][8]['image'][3]['#text'];
$album_10 = $albumcharts[0]['topalbums']['album'][9]['image'][3]['#text'];
$album_11 = $albumcharts[0]['topalbums']['album'][10]['image'][3]['#text'];
$album_12 = $albumcharts[0]['topalbums']['album'][11]['image'][3]['#text'];
// Not playing anything, so let's refresh every 30 seconds.
$duration = 30000;
}
?>
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title><?php echo $trackInfo['status']; ?> from Last.fm</title>
<link rel="stylesheet" href="/static/css/normalize.min.css">
<link rel="stylesheet" href="style.min.css?v1">
<style>
.artwork {
background-image: url("<?php echo $trackInfo['albumArt']; ?>");
}
</style>
<script>
let playing = false;
function requestUpdate() {
let url = `https://ws.audioscrobbler.com/2.0/?api_key=<?php echo $key; ?>&method=user.getRecentTracks&user=<?php echo $user ?>&extended=1&limit=1&format=json`;
let request = new Request(url, {
"method": "GET",
});
return fetch(request);
}
function successHandler(value) {
value.json().then(data => {
//console.log(data);
let track = data.recenttracks.track[0];
let title = data.recenttracks.track[0].name;
let old_title = document.querySelector(".track");
console.log(title);
console.log(old_title.innerText);
if (track["@attr"] == undefined) {
if (playing) {
// Stop Playing
console.log("Not playing anything.");
playing = false;
window.location.reload(1);
}
} else if (playing == false) {
// Start Playing
console.log("Now playing.");
playing = true;
}
if(title && old_title.innerText){
if(title != old_title.innerText) {
// Refresh page.
window.location.reload(1);
}
}
}, reason => {
})
setTimeout(tick, <?php echo $duration; ?>);
}
function failureHandler(reason) {
console.log("Last.fm query failed:", reason);
setTimeout(tick, 60000);
}
function tick() {
let rq = requestUpdate();
rq.then(successHandler, failureHandler);
rq.catch(failureHandler);
}
(function() {
tick();
})();
</script>
<?php if ($trackInfo['status'] == 'Now Playing') { ?>
</head>
<body>
<div id="container">
<div class="artwork"></div>
<section id="main">
<img class="art_image" src="<?php echo $trackInfo['albumArt']; ?>" width="500" height="500">
<div class="text">
<div class="track"><?php echo $trackInfo['name']; ?></div>
<div class="artist"><?php echo $trackInfo['artist']; ?></div>
<div class="album"><?php echo $trackInfo['album']; ?></div>
<div class="number"><?php echo $playcount; ?><?php echo $loved; ?></div>
</div>
</section>
</div>
</body>
</html>
<?php } else { ?>
</head>
<body>
<div id="container" class="last_played">
<div class="user"><?php echo $username; ?></div>
<div class="stat_box">
<div class="header">scrobbles</div>
<div class="scrobbles"><?php echo $playcount; ?></div>
</div>
<div class="stat_box">
<div class="header">last played</div>
<div class="play_box">
<div class="track"><?php echo $trackInfo['name']; ?></div>
<div class="artist"><?php echo $trackInfo['artist']; ?></div>
</div>
</div>
<div class="list">Top Albums - Last 7 Days</div>
<div class="albums">
<img class="top_albums" src="<?php echo $album_1; ?>">
<img class="top_albums" src="<?php echo $album_2; ?>">
<img class="top_albums" src="<?php echo $album_3; ?>">
<img class="top_albums" src="<?php echo $album_4; ?>">
<img class="top_albums" src="<?php echo $album_5; ?>">
<img class="top_albums" src="<?php echo $album_6; ?>">
<img class="top_albums" src="<?php echo $album_7; ?>">
<img class="top_albums" src="<?php echo $album_8; ?>">
<img class="top_albums" src="<?php echo $album_9; ?>">
<img class="top_albums" src="<?php echo $album_10; ?>">
<img class="top_albums" src="<?php echo $album_11; ?>">
<img class="top_albums" src="<?php echo $album_12; ?>">
</div>
</div>
</body>
</html>
<?php } ?>