diff --git a/app/Core/IniFile.php b/app/Core/IniFile.php index d40ee54..93f934a 100644 --- a/app/Core/IniFile.php +++ b/app/Core/IniFile.php @@ -35,52 +35,8 @@ class IniFile public function load(): void { $filepath = config_path('playlists.ini'); - $ini = parse_ini_file($filepath, true); + $this->playlists = parse_ini_file($filepath, true); $this->updatedAt = date('d.m.Y h:i', filemtime($filepath)); - $plsCodes = array_keys($ini); - $cached = array_combine($plsCodes, redis()->mget($plsCodes)); - - foreach ($cached as $code => $data) { - $raw = $ini[$code]; - $data === false && $data = [ - 'code' => $code, - 'name' => $raw['name'] ?? "Плейлист #$code", - 'description' => $raw['desc'] ?? null, - 'url' => $raw['pls'], - 'source' => $raw['src'] ?? null, - 'content' => null, - 'isOnline' => null, - 'attributes' => [], - 'groups' => [], - 'channels' => [], - 'checkedAt' => null, - ]; - - // приколы golang - $data['attributes'] === null && $data['attributes'] = []; - $data['groups'] === null && $data['groups'] = []; - $data['channels'] === null && $data['channels'] = []; - - $data['onlinePercent'] = 0; - $data['offlinePercent'] = 0; - if ($data['isOnline'] === true && count($data['channels']) > 0) { - $data['onlinePercent'] = round($data['onlineCount'] / count($data['channels']) * 100); - $data['offlinePercent'] = round($data['offlineCount'] / count($data['channels']) * 100); - } - - $data['hasCatchup'] = str_contains($data['content'] ?? '', 'catchup'); - $data['hasTvg'] = !empty($data['attributes']['url-tvg']) - || !empty($data['attributes']['x-tvg-url']); - - $data['tags'] = []; - foreach ($data['channels'] as $channel) { - $data['tags'] = array_merge($data['tags'], $channel['tags']); - } - $data['tags'] = array_values(array_unique($data['tags'])); - sort($data['tags']); - - $this->playlists[$code] = $data; - } } /** @@ -92,6 +48,12 @@ class IniFile public function getPlaylists(): array { empty($this->playlists) && $this->load(); + $plsCodes = array_keys($this->playlists); + $cached = array_combine($plsCodes, redis()->mget($plsCodes)); + foreach ($cached as $code => $data) { + $this->playlists[$code] = $this->initPlaylist($code, $data); + } + return $this->playlists; } @@ -106,7 +68,10 @@ class IniFile public function getPlaylist(string $code): ?array { empty($this->playlists) && $this->load(); - return $this->playlists[$code] ?? throw new PlaylistNotFoundException($code); + $data = redis()->get($code); + $pls = $this->initPlaylist($code, $data); + + return $pls ?? throw new PlaylistNotFoundException($code); } /** @@ -118,4 +83,56 @@ class IniFile { return $this->updatedAt; } + + /** + * Подготавливает данные о плейлисте в расширенном формате + * + * @param string $code + * @param array|false $data + * @return array + */ + protected function initPlaylist(string $code, array|false $data): array + { + if ($data === false) { + $raw = $this->playlists[$code]; + $data === false && $data = [ + 'code' => $code, + 'name' => $raw['name'] ?? "Плейлист #$code", + 'description' => $raw['desc'] ?? null, + 'url' => $raw['pls'], + 'source' => $raw['src'] ?? null, + 'content' => null, + 'isOnline' => null, + 'attributes' => [], + 'groups' => [], + 'channels' => [], + 'checkedAt' => null, + ]; + } + + // приколы golang + $data['attributes'] === null && $data['attributes'] = []; + $data['groups'] === null && $data['groups'] = []; + $data['channels'] === null && $data['channels'] = []; + + $data['onlinePercent'] = 0; + $data['offlinePercent'] = 0; + if ($data['isOnline'] === true && count($data['channels']) > 0) { + $data['onlinePercent'] = round($data['onlineCount'] / count($data['channels']) * 100); + $data['offlinePercent'] = round($data['offlineCount'] / count($data['channels']) * 100); + } + + $data['hasCatchup'] = str_contains($data['content'] ?? '', 'catchup'); + $data['hasTvg'] = !empty($data['attributes']['url-tvg']) + || !empty($data['attributes']['x-tvg-url']); + + $data['tags'] = []; + foreach ($data['channels'] as $channel) { + $data['tags'] = array_merge($data['tags'], $channel['tags']); + } + $data['tags'] = array_values(array_unique($data['tags'])); + sort($data['tags']); + + return $data; + } }