diff --git a/app/public/api/version.php b/app/public/api/version.php new file mode 100644 index 0000000..2850cd2 --- /dev/null +++ b/app/public/api/version.php @@ -0,0 +1,12 @@ + true, + 'version' => file_exists($versionFile) + ? trim(file_get_contents($versionFile)) + : '0', +]); diff --git a/app/public/assets/js/live-now.js b/app/public/assets/js/live-now.js index 5abbf4b..5d52f90 100644 --- a/app/public/assets/js/live-now.js +++ b/app/public/assets/js/live-now.js @@ -48,6 +48,7 @@ document.addEventListener('DOMContentLoaded', () => { const liveNow = new LiveEngine({ url: '/api/live-now.php', + versionUrl: '/api/version.php', interval: 30000, gridSelector: '[data-live-now-grid]', itemKey: item => item.channel_id, diff --git a/app/public/assets/js/live.js b/app/public/assets/js/live.js index c73fe51..83f8032 100644 --- a/app/public/assets/js/live.js +++ b/app/public/assets/js/live.js @@ -31,6 +31,8 @@ window.TVGuideLive = { window.LiveEngine = class LiveEngine { constructor(options) { this.url = options.url; + this.versionUrl = options.versionUrl ?? null; + this.version = null; this.interval = options.interval ?? 30000; this.gridSelector = options.gridSelector; this.itemKey = options.itemKey; @@ -49,10 +51,35 @@ window.LiveEngine = class LiveEngine { return document.querySelector(this.gridSelector); } + async hasChanged() { + if (!this.versionUrl) { + return true; + } + + const data = await TVGuideLive.fetchJson(this.versionUrl); + const version = String(data.version ?? '0'); + + if (this.version === null) { + this.version = version; + return true; + } + + if (this.version !== version) { + this.version = version; + return true; + } + + return false; + } + async refresh() { const grid = this.getGrid(); if (!grid) return; + if (!(await this.hasChanged())) { + return; + } + try { const data = await TVGuideLive.fetchJson(this.url); const items = data.items ?? []; diff --git a/app/src/Services/XmlTvCacheService.php b/app/src/Services/XmlTvCacheService.php index c7c9465..541aa96 100644 --- a/app/src/Services/XmlTvCacheService.php +++ b/app/src/Services/XmlTvCacheService.php @@ -112,6 +112,11 @@ class XmlTvCacheService $this->cacheFile, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) ); + + file_put_contents( + dirname($this->cacheFile) . '/guide.version', + (string)filemtime($this->cacheFile) + ); } private function parseTime(string $value): ?DateTime