<?php
require_once __DIR__ . '/includes/stream_lib.php';
date_default_timezone_set('Australia/Sydney');
$cfg = smurf_get_stream_config();
$hostname = (string)($cfg['server_ip'] ?? '');
$port = (string)($cfg['server_port'] ?? '');
$mount = (string)($cfg['mount_main'] ?? '/stream');
$url = "http://$hostname:$port/status-json.xsl";
$logFile = __DIR__ . '/data/recently_played.txt';
$lastFile = __DIR__ . '/data/last_played.txt';
$json = @file_get_contents($url);
$data = json_decode((string)$json, true);
if (!$data || !isset($data['icestats']['source'])) exit;
$source = $data['icestats']['source'];
if (isset($source['mount']) && $source['mount'] !== $mount) {
  foreach ((array)$source as $s) {
    if (isset($s['mount']) && $s['mount'] === $mount) { $source = $s; break; }
  }
}
$title = trim((string)($source['title'] ?? ''));
if ($title === '') exit;
$last = file_exists($lastFile) ? trim((string)file_get_contents($lastFile)) : '';
if ($title !== $last) {
  $timestamp = date('Y-m-d H:i');
  file_put_contents($logFile, "$timestamp|$title
", FILE_APPEND);
  file_put_contents($lastFile, $title);
}
?>