Repository navigation
Expand file tree
/
Copy pathtwitch-data.php
More file actions
94 lines (78 loc) · 2.19 KB
/
Copy pathtwitch-data.php
File metadata and controls
94 lines (78 loc) · 2.19 KB
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
<?php
header('Content-Type: application/json; charset=utf-8');
function loadEnvValue(string $key): ?string {
$value = getenv($key);
if ($value !== false && $value !== '') {
return $value;
}
$envFile = __DIR__ . '/.env';
if (!is_readable($envFile)) {
return null;
}
$env = parse_ini_file($envFile, false, INI_SCANNER_TYPED);
if (!is_array($env) || !array_key_exists($key, $env)) {
return null;
}
$envValue = $env[$key];
return is_string($envValue) || is_numeric($envValue) ? (string) $envValue : null;
}
$apiKey = loadEnvValue('API_KEY') ?? loadEnvValue('LASTFM_API_KEY');
$username = loadEnvValue('LASTFM_USERNAME') ?? 'VoidemLIVE';
if (!$apiKey) {
http_response_code(500);
echo json_encode([
'playing' => false,
'status' => 'not playing',
'error' => 'Missing API key',
]);
exit;
}
$url = 'https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks'
. '&user=' . urlencode($username)
. '&api_key=' . urlencode($apiKey)
. '&format=json&limit=1';
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 5,
]);
$response = curl_exec($curl);
if ($response === false || curl_errno($curl)) {
$error = curl_error($curl);
curl_close($curl);
http_response_code(502);
echo json_encode([
'playing' => false,
'status' => 'not playing',
'error' => $error ?: 'Failed to fetch track data',
]);
exit;
}
curl_close($curl);
$data = json_decode($response, true);
$track = $data['recenttracks']['track'][0] ?? null;
if (!$track) {
echo json_encode([
'playing' => false,
'status' => 'not playing',
]);
exit;
}
$playing = (($track['@attr']['nowplaying'] ?? '') === 'true');
if (!$playing) {
echo json_encode([
'playing' => false,
'status' => 'not playing',
]);
exit;
}
$image = $track['image'][1]['#text'] ?? '';
echo json_encode([
'playing' => true,
'status' => 'now playing',
'trackName' => $track['name'] ?? '—',
'artist' => $track['artist']['#text'] ?? '—',
'image' => $image,
]);