aboutsummaryrefslogtreecommitdiffstats
path: root/api/nowplaying.php
diff options
context:
space:
mode:
authorsillylaird <sillyfanboy@gmail.com>2026-09-03 00:33:59 +0000
committersillylaird <sillyfanboy@gmail.com>2026-09-03 00:33:59 +0000
commit898b52edcb47bcb3e9d6106e74ca73e74ea01e70 (patch)
tree85c6ee5ad58b860144551184d4cf86b560c62b91 /api/nowplaying.php
downloadwww-898b52edcb47bcb3e9d6106e74ca73e74ea01e70.tar.gz
www-898b52edcb47bcb3e9d6106e74ca73e74ea01e70.zip
import live www.sillylaird.ca webrootHEADmain
Diffstat (limited to 'api/nowplaying.php')
-rw-r--r--api/nowplaying.php47
1 files changed, 47 insertions, 0 deletions
diff --git a/api/nowplaying.php b/api/nowplaying.php
new file mode 100644
index 0000000..1faf016
--- /dev/null
+++ b/api/nowplaying.php
@@ -0,0 +1,47 @@
+<?php
+require_once __DIR__ . '/../partials/proxy_helpers.php';
+
+header('Content-Type: application/json; charset=utf-8');
+header('Cache-Control: public, max-age=10');
+
+$cacheFile = sys_get_temp_dir() . '/sillylaird_nowplaying.json';
+$ttl = 10;
+
+if (is_file($cacheFile) && (time() - filemtime($cacheFile)) < $ttl) {
+ readfile($cacheFile);
+ exit;
+}
+
+if (!proxy_rate_limit('nowplaying', 30, 60)) {
+ http_response_code(429);
+ echo json_encode(['error' => 'rate limited']);
+ exit;
+}
+
+$ctx = stream_context_create(['http' => [
+ 'timeout' => 4,
+ 'header' => "User-Agent: sillylaird-nowplaying/1.0\r\n",
+]]);
+
+$raw = @file_get_contents('https://radio.sillylaird.ca/status-json.xsl', false, $ctx);
+if ($raw === false) {
+ if (is_file($cacheFile)) { readfile($cacheFile); exit; }
+ http_response_code(502);
+ echo json_encode(['error' => 'upstream']);
+ exit;
+}
+
+$data = json_decode($raw, true);
+$src = $data['icestats']['source'] ?? null;
+if (is_array($src) && isset($src[0])) $src = $src[0];
+
+$out = json_encode([
+ 'title' => $src['title'] ?? ($src['yp_currently_playing'] ?? null),
+ 'name' => $src['server_name'] ?? null,
+ 'online' => isset($src['title']) || isset($src['yp_currently_playing']),
+ 'fetched' => time(),
+]);
+
+@file_put_contents($cacheFile . '.tmp', $out, LOCK_EX);
+@rename($cacheFile . '.tmp', $cacheFile);
+echo $out;