-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
350 lines (279 loc) · 11.5 KB
/
Plugin.php
File metadata and controls
350 lines (279 loc) · 11.5 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
namespace AppLocalPlugins\ChannelsDvr;
use App\Models\Channel;
use App\Plugins\Contracts\ChannelProcessorPluginInterface;
use App\Plugins\Contracts\HookablePluginInterface;
use App\Plugins\Contracts\PluginInterface;
use App\Plugins\Support\PluginActionResult;
use App\Plugins\Support\PluginExecutionContext;
use Illuminate\Support\Facades\Http;
use Throwable;
class Plugin implements ChannelProcessorPluginInterface, HookablePluginInterface, PluginInterface
{
private const STATIONS_PATH = '/dvr/guide/stations';
public function runAction(string $action, array $payload, PluginExecutionContext $context): PluginActionResult
{
return match ($action) {
'health_check' => $this->healthCheck($context),
'sync_station_ids' => $this->syncFromAction($payload, $context),
default => PluginActionResult::failure("Unsupported action [{$action}]."),
};
}
public function runHook(string $hook, array $payload, PluginExecutionContext $context): PluginActionResult
{
if ($hook !== 'playlist.synced') {
return PluginActionResult::success("Hook [{$hook}] not handled by Channels DVR.");
}
$playlistId = (int) ($payload['playlist_id'] ?? 0);
if ($playlistId === 0) {
return PluginActionResult::failure('Missing playlist_id in hook payload.');
}
$configured = $context->settings['default_playlist_id'] ?? null;
$watchedIds = array_map('intval', array_filter((array) $configured));
if ($watchedIds === []) {
return PluginActionResult::success('No default playlist(s) configured — skipping automatic station ID sync.');
}
if (! in_array($playlistId, $watchedIds, true)) {
return PluginActionResult::success("Playlist #{$playlistId} is not in the configured defaults — skipping.");
}
return $this->processPlaylist($playlistId, $context);
}
private function healthCheck(PluginExecutionContext $context): PluginActionResult
{
$baseUrl = $this->buildBaseUrl($context->settings);
if ($baseUrl === '') {
return PluginActionResult::failure('DVR host is not configured. Set the DVR Host setting.');
}
$url = $baseUrl.self::STATIONS_PATH;
$context->info("Checking Channels DVR at {$url}...");
try {
$response = Http::timeout(10)->get($url);
if (! $response->successful()) {
return PluginActionResult::failure("DVR returned HTTP {$response->status()} from {$url}.");
}
$stations = $this->parseStations($response->json());
return PluginActionResult::success('Channels DVR is reachable.', [
'url' => $url,
'station_count' => count($stations),
]);
} catch (Throwable $e) {
return PluginActionResult::failure("Could not reach Channels DVR: {$e->getMessage()}");
}
}
private function syncFromAction(array $payload, PluginExecutionContext $context): PluginActionResult
{
$playlistId = (int) ($payload['playlist_id'] ?? 0);
if ($playlistId === 0) {
return PluginActionResult::failure('Missing playlist_id in action payload.');
}
return $this->processPlaylist($playlistId, $context);
}
private function processPlaylist(int $playlistId, PluginExecutionContext $context): PluginActionResult
{
$settings = $context->settings;
$baseUrl = $this->buildBaseUrl($settings);
$overwriteExisting = (bool) ($settings['overwrite_existing'] ?? false);
$skipVod = (bool) ($settings['skip_vod'] ?? true);
$isDryRun = $context->dryRun;
if ($baseUrl === '') {
return PluginActionResult::failure('DVR host is not configured. Set the DVR Host setting.');
}
$url = $baseUrl.self::STATIONS_PATH;
$context->info("Fetching stations from {$url}...");
try {
$response = Http::timeout(30)->get($url);
} catch (Throwable $e) {
return PluginActionResult::failure("Failed to connect to Channels DVR: {$e->getMessage()}");
}
if (! $response->successful()) {
return PluginActionResult::failure("Channels DVR returned HTTP {$response->status()} from {$url}.");
}
$stations = $this->parseStations($response->json());
if ($stations === []) {
return PluginActionResult::success('No stations returned from Channels DVR — nothing to map.');
}
$context->info(sprintf('Loaded %d station(s) from Channels DVR.', count($stations)));
$lookup = $this->buildLookup($stations);
$query = Channel::query()
->where('playlist_id', $playlistId)
->where('enabled', true)
->select(['id', 'title', 'title_custom', 'name', 'name_custom', 'station_id']);
if ($skipVod) {
$query->where('is_vod', false);
}
if (! $overwriteExisting) {
$query->where(function ($q): void {
$q->whereNull('station_id')->orWhere('station_id', '');
});
}
$channels = $query->get();
$total = $channels->count();
if ($total === 0) {
return PluginActionResult::success('No channels require station ID mapping.', [
'matched' => 0,
'unmatched' => 0,
'total' => 0,
]);
}
$context->info(sprintf(
'Processing %d channel(s) for playlist #%d%s.',
$total,
$playlistId,
$isDryRun ? ' [dry run]' : ''
));
$matched = 0;
$unmatched = 0;
foreach ($channels as $i => $channel) {
$displayName = trim((string) ($channel->title_custom ?? $channel->title ?? $channel->name_custom ?? $channel->name ?? ''));
if ($displayName === '') {
$unmatched++;
continue;
}
$stationId = $this->resolveStationId($displayName, $lookup);
if ($stationId !== null) {
$matched++;
$context->info("Matched: \"{$displayName}\" → station_id={$stationId}");
if (! $isDryRun) {
Channel::where('id', $channel->id)->update(['station_id' => $stationId]);
}
} else {
$unmatched++;
}
if (($i + 1) % 50 === 0) {
$context->heartbeat(progress: (int) ((($i + 1) / $total) * 100));
}
}
return PluginActionResult::success(
sprintf(
'%d of %d channel(s) matched%s.',
$matched,
$total,
$isDryRun ? ' (dry run — no changes written)' : ''
),
[
'matched' => $matched,
'unmatched' => $unmatched,
'total' => $total,
'dry_run' => $isDryRun,
]
);
}
/**
* Build the base URL from settings (no trailing slash, port appended).
*
* Strips any port already embedded in the host value so that the Port
* setting is always the authoritative source — prevents double-port URLs
* if the user includes the port in the DVR Host field (e.g. "192.168.1.1:8089").
*/
private function buildBaseUrl(array $settings): string
{
$host = trim((string) ($settings['dvr_host'] ?? ''));
if ($host === '') {
return '';
}
if (! str_starts_with($host, 'http://') && ! str_starts_with($host, 'https://')) {
$host = 'http://'.$host;
}
// Strip any port already in the host so the Port setting wins.
$parsed = parse_url($host);
$scheme = $parsed['scheme'] ?? 'http';
$hostname = $parsed['host'] ?? '';
if ($hostname === '') {
return '';
}
$port = (int) ($settings['dvr_port'] ?? 8089);
return "{$scheme}://{$hostname}:{$port}";
}
/**
* Parse the raw Channels DVR guide stations response into a flat list.
*
* The actual API response is a nested map:
* { "<provider>": { "<stationId>": { "callSign": "...", "stationId": "...", ... } } }
*
* Multiple providers may be present at the top level. Each station object
* is keyed by its numeric station ID and contains `callSign` and `stationId`.
* The `name` field is typically empty — call sign is the primary identifier.
*
* @return array<int, array{station_id: string, name: string, callsign: string}>
*/
private function parseStations(mixed $raw): array
{
if (! is_array($raw)) {
return [];
}
$stations = [];
foreach ($raw as $providerStations) {
if (! is_array($providerStations)) {
continue;
}
foreach ($providerStations as $stationKey => $station) {
if (! is_array($station)) {
continue;
}
$id = (string) ($station['stationId'] ?? $station['StationID'] ?? $station['station_id'] ?? $stationKey ?? '');
$name = (string) ($station['name'] ?? $station['Name'] ?? '');
$callsign = (string) ($station['callSign'] ?? $station['CallSign'] ?? $station['callsign'] ?? '');
if ($id === '') {
continue;
}
$stations[] = ['station_id' => $id, 'name' => $name, 'callsign' => $callsign];
}
}
return $stations;
}
/**
* Build a normalized lookup map: normalized_label → station_id.
*
* Call sign is indexed first so it wins over the full name when both
* normalize to the same string (e.g. "ESPN" callsign vs "ESPN" name).
*
* @param array<int, array{station_id: string, name: string, callsign: string}> $stations
* @return array<string, string>
*/
private function buildLookup(array $stations): array
{
$lookup = [];
foreach ($stations as $station) {
foreach ([$station['callsign'], $station['name']] as $label) {
$key = $this->normalize($label);
if ($key !== '' && ! isset($lookup[$key])) {
$lookup[$key] = $station['station_id'];
}
}
}
return $lookup;
}
/**
* Attempt to resolve a station ID for a channel display name.
*
* Tries in order:
* 1. Exact normalized match (e.g. "ESPN" → "espn")
* 2. Strip common quality suffixes and retry (e.g. "ESPN HD" → "ESPN")
*
* @param array<string, string> $lookup
*/
private function resolveStationId(string $displayName, array $lookup): ?string
{
$key = $this->normalize($displayName);
if (isset($lookup[$key])) {
return $lookup[$key];
}
// Strip trailing quality/resolution suffixes and retry
$stripped = preg_replace('/\s+(HD|SD|FHD|UHD|4K|\d+p)$/i', '', $displayName);
if ($stripped !== null && $stripped !== $displayName) {
$key = $this->normalize($stripped);
if (isset($lookup[$key])) {
return $lookup[$key];
}
}
return null;
}
/**
* Normalize a station label to a plain lowercase alphanumeric string
* for case- and punctuation-insensitive matching.
*/
private function normalize(string $value): string
{
return strtolower(trim((string) preg_replace('/[^a-z0-9]+/i', '', $value)));
}
}