Phase 2.2
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
require_once '/var/www/src/bootstrap.php';
|
||||
|
||||
$versionFile = '/var/www/cache/guide.version';
|
||||
|
||||
ApiResponse::json([
|
||||
'ok' => true,
|
||||
'version' => file_exists($versionFile)
|
||||
? trim(file_get_contents($versionFile))
|
||||
: '0',
|
||||
]);
|
||||
@@ -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,
|
||||
|
||||
@@ -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 ?? [];
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user