Большое обновление и рефакторинг

- улучшен и нарощен парсинг плейлистов
- упрощена конфигурация
- название плейлиста в заголовке страницы подробностей
- fuzzy-поиск каналов на странице подробностей
- эскизы логотипов на странице подробностей
- бейдж статуса плейлиста на главной теперь перед названием
- удалены laravel-завивимости
- какие-нибудь мелочи, которые забыл упомянуть
This commit is contained in:
2024-09-23 13:05:01 +08:00
parent aff93aaf78
commit 24636837cc
22 changed files with 557 additions and 739 deletions

View File

@@ -4,8 +4,12 @@ declare(strict_types=1);
namespace App\Controllers;
use App\Core\IniFile;
use App\Core\Playlist;
use App\Exceptions\PlaylistNotFoundException;
use Exception;
use Flight;
use Random\RandomException;
/**
* Абстрактный контроллер для расширения
@@ -13,15 +17,69 @@ use Flight;
abstract class Controller
{
/**
* Перебрасывает на страницу 404 при ненайденном плейлисте
* @var IniFile Класс для работы с ini-файлом плейлистов
*/
protected IniFile $ini;
/**
* Конструктор
*/
public function __construct()
{
$this->ini = Flight::get('ini');
}
/**
* Возвращает плейлист по его ID для обработки
*
* @param string $id
* @param bool $asJson
* @return Playlist
* @throws Exception
*/
protected function getPlaylist(string $id, bool $asJson = false): Playlist
{
if ($this->ini->getRedirection($id)) {
Flight::redirect(base_url($this->ini->getRedirection($id) . ($asJson ? '/json' : '/details')));
die;
}
try {
return $this->ini->getPlaylist($id);
} catch (PlaylistNotFoundException) {
$this->notFound($id, $asJson);
die;
}
}
/**
* Возвращает обработанный плейлист для ответа
*
* @param string $id ID плейлиста
* @param bool $asJson Обрабатывать как json
* @return array
* @throws RandomException
* @throws Exception
*/
protected function getPlaylistResponse(string $id, bool $asJson = false): array
{
$playlist = $this->getPlaylist($id, $asJson);
$playlist->download();
$playlist->parse();
return $playlist->toArray();
}
/**
* Перебрасывает на страницу 404 при ненайденном плейлисте
*
* @param string $id ID плейлиста
* @param bool $asJson Обрабатывать как json
* @return void
* @throws Exception
*/
public function notFound(string $id): void
public function notFound(string $id, bool $asJson = false): void
{
Flight::response()->status(404)->sendHeaders();
view('notfound', ['id' => $id]);
$asJson || view('notfound', ['id' => $id]);
}
}

View File

@@ -4,8 +4,6 @@ declare(strict_types=1);
namespace App\Controllers;
use App\Core\PlaylistProcessor;
use App\Core\RedirectedPlaylist;
use Exception;
use Flight;
@@ -15,22 +13,9 @@ use Flight;
class HomeController extends Controller
{
/**
* @var PlaylistProcessor Обработчик ini-списка
*/
protected PlaylistProcessor $ini;
/**
* Конструктор
*/
public function __construct()
{
$this->ini = new PlaylistProcessor();
}
/**
* Отображает главную страницу на указанной странице списка плейлистов
* Отображает главную страницу с учётом пагинации списка плейлистов
*
* @param int $page
* @param int $page Текущая страница списка
* @return void
* @throws Exception
*/
@@ -44,19 +29,21 @@ class HomeController extends Controller
}
// иначе формируем и сортируем список при необходимости, рисуем страницу
$per_page = 10;
$list = $this->ini->playlists
->filter(static fn ($playlist) => !($playlist instanceof RedirectedPlaylist))
->forPage($page, $per_page);
$perPage = 10;
$playlists = $this->ini->playlists(false);
$count = count($playlists);
$pageCount = ceil($count / $perPage);
$offset = max(0, ($page - 1) * $perPage);
$list = array_slice($playlists, $offset, $perPage, true);
view('list', [
'updated_at' => $this->ini->updatedAt(),
'count' => $this->ini->playlists->count(),
'count' => $count,
'pages' => [
'count' => ceil($this->ini->playlists->count() / $per_page),
'count' => $pageCount,
'current' => $page,
],
'playlists' => $list->toArray(),
'playlists' => $list,
]);
}

View File

@@ -4,9 +4,6 @@ declare(strict_types=1);
namespace App\Controllers;
use App\Core\{
PlaylistProcessor,
RedirectedPlaylist};
use App\Exceptions\PlaylistNotFoundException;
use Exception;
use Flight;
@@ -16,34 +13,17 @@ use Flight;
*/
class PlaylistController extends Controller
{
/**
* @var PlaylistProcessor Обработчик ini-списка
*/
protected PlaylistProcessor $ini;
/**
* Конструктор
*/
public function __construct()
{
$this->ini = new PlaylistProcessor();
}
/**
* Отправляет запрос с клиента по прямой ссылке плейлиста
*
* @param $id
* @param string $id ID плейлиста
* @return void
* @throws Exception
*/
public function download($id): void
public function download(string $id): void
{
try {
$playlist = $this->ini->playlist($id);
if ($playlist instanceof RedirectedPlaylist) {
Flight::redirect(base_url($playlist->redirect_id));
die;
}
$playlist = $this->ini->getPlaylist($id);
Flight::redirect($playlist->pls);
} catch (PlaylistNotFoundException) {
$this->notFound($id);
@@ -54,49 +34,27 @@ class PlaylistController extends Controller
/**
* Отображает страницу описания плейлиста
*
* @param string $id
* @param string $id ID плейлиста
* @return void
* @throws Exception
*/
public function details(string $id): void
{
try {
$playlist = $this->ini->playlist($id);
if ($playlist instanceof RedirectedPlaylist) {
Flight::redirect(base_url($playlist->redirect_id . '/details'));
die;
}
view('details', [
...$playlist->toArray(),
...$this->ini->parse($id),
]);
} catch (PlaylistNotFoundException) {
$this->notFound($id);
}
$result = $this->getPlaylistResponse($id);
view('details', $result);
}
/**
* Возвращает JSON с описанием плейлиста
*
* @param string $id
* @param string $id ID плейлиста
* @return void
* @throws Exception
*/
public function json(string $id): void
{
try {
$playlist = $this->ini->playlist($id);
if ($playlist instanceof RedirectedPlaylist) {
Flight::redirect(base_url($playlist->redirect_id . '/json'));
die;
}
Flight::json([
...$playlist->toArray(),
...$this->ini->parse($id),
]);
} catch (PlaylistNotFoundException) {
Flight::response()->status(404)->sendHeaders();
Flight::json(['error' => ['message' => 'Playlist not found']]);
}
$result = $this->getPlaylistResponse($id, true);
Flight::json($result);
}
}

View File

@@ -1,28 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Core;
use Illuminate\Contracts\Support\Arrayable;
/**
* Базовый класс плейлиста
*/
abstract class BasicPlaylist implements Arrayable
{
/**
* @var string ID плейлиста
*/
public string $id;
/**
* Возвращает ссылку на плейлист в рамках проекта
*
* @return string
*/
public function url(): string
{
return sprintf('%s/%s', base_url(), $this->id);
}
}

View File

@@ -4,10 +4,8 @@ declare(strict_types=1);
namespace App\Core;
use App\Controllers\AjaxController;
use App\Extensions\TwigFunctions;
use Flight;
use Illuminate\Support\Arr;
use Twig\Environment;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
@@ -24,11 +22,19 @@ final class Bootstrapper
*/
public static function bootSettings(): void
{
$settings = Arr::dot(require_once config_path('app.php'));
Arr::map($settings, function ($value, $key) {
Flight::set("flight.$key", $value);
});
Flight::set('config', $settings);
$config = require_once config_path('app.php');
foreach ($config as $key => $value) {
Flight::set($key, $value);
}
Flight::set('config', $config);
}
public static function bootIni(): void
{
$loader = new IniFile();
$loader->load();
Flight::set('ini', $loader);
}
/**
@@ -38,18 +44,20 @@ final class Bootstrapper
*/
public static function bootTwig(): void
{
$filesystemLoader = new FilesystemLoader(config('views.path'));
Flight::register(
'view',
Environment::class,
[$filesystemLoader, config('twig')],
function ($twig) {
/** @var Environment $twig */
Flight::set('twig', $twig);
$twig->addExtension(new TwigFunctions());
$twig->addExtension(new DebugExtension());
}
);
$twigCfg = [
'cache' => config('twig.cache'),
'debug' => config('twig.debug'),
];
$closure = static function ($twig) {
/** @var Environment $twig */
Flight::set('twig', $twig);
$twig->addExtension(new TwigFunctions());
$twig->addExtension(new DebugExtension());
};
$loader = new FilesystemLoader(config('flight.views.path'));
Flight::register('view', Environment::class, [$loader, $twigCfg], $closure);
}
/**

120
src/app/Core/IniFile.php Normal file
View File

@@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
namespace App\Core;
use App\Exceptions\PlaylistNotFoundException;
use Exception;
/**
* Класс для работы с ini-файлом плейлистов
*/
class IniFile
{
/**
* @var array Считанное из файла содержимое ini-файла
*/
protected array $rawIni;
/**
* @var Playlist[] Коллекция подгруженных плейлистов
*/
protected array $playlists = [];
/**
* @var string[] Карта переадресаций плейлистов
*/
protected array $redirections = [];
/**
* @var string Дата последнего обновления списка
*/
protected string $updated_at;
/**
* Считывает ini-файл и инициализирует объекты плейлистов
*
* @return void
* @throws Exception
*/
public function load(): void
{
$filepath = config_path('playlists.ini');
$this->updated_at = date('d.m.Y h:i', filemtime($filepath));
$this->rawIni = parse_ini_file($filepath, true);
foreach ($this->rawIni as $id => $data) {
$this->playlists[(string)$id] = $this->makePlaylist($id, $data);
}
}
/**
* Возвращает объекты плейлистов
*
* @param bool $all true - получить все, false - получить только НЕпереадресованные
* @return Playlist[]
*/
public function playlists(bool $all = true): array
{
if ($all) {
return $this->playlists;
}
return array_filter($this->playlists, static fn ($playlist) => is_null($playlist->redirectId));
}
/**
* Возвращает дату обновления ini-файла
*
* @return string
*/
public function updatedAt(): string
{
return $this->updated_at;
}
/**
* Возвращает ID плейлиста, на который нужно переадресовать указанный
*
* @param string $id ID плейлиста
* @return string|null
*/
public function getRedirection(string $id): ?string
{
return $this->redirections[$id] ?? null;
}
/**
* Возвращает объект плейлиста
*
* @param string $id ID плейлиста
* @return Playlist|null
* @throws PlaylistNotFoundException
*/
public function getPlaylist(string $id): ?Playlist
{
return $this->playlists[$id] ?? throw new PlaylistNotFoundException($id);
}
/**
* Создаёт объекты плейлистов, рекурсивно определяя переадресации
*
* @param int|string $id ID плейлиста
* @param array $params Описание плейлиста
* @param string|null $redirectId ID для переадресации
* @return Playlist
* @throws Exception
*/
protected function makePlaylist(int|string $id, array $params, ?string $redirectId = null): Playlist
{
$id = (string)$id;
if (isset($params['redirect'])) {
$this->redirections[$id] = $redirectId = (string)$params['redirect'];
$params = $this->rawIni[$redirectId];
return $this->makePlaylist($id, $params, $redirectId);
}
return new Playlist($id, $params, $redirectId);
}
}

View File

@@ -5,11 +5,12 @@ declare(strict_types=1);
namespace App\Core;
use Exception;
use Random\RandomException;
/**
* Плейлист без редиректа
*/
class Playlist extends BasicPlaylist
class Playlist
{
/**
* @var string|null Название плейлиста
@@ -36,18 +37,43 @@ class Playlist extends BasicPlaylist
*/
public string $url;
/**
* @var string|null Сырое содержимое плейлиста
*/
protected ?string $rawContent = null;
/**
* @var array Обработанное содержимое плейлиста
*/
protected array $parsedContent = [];
/**
* @var array Статус скачивания плейлиста
*/
protected array $downloadStatus = [
'httpCode' => 'unknown',
'errCode' => 'unknown',
'errText' => 'unknown',
'possibleStatus' => 'unknown',
];
/**
* Конструктор
*
* @param string $id
* @param array $params
* @param string $id ID плейлиста
* @param array $params Описание плейлиста
* @param string|null $redirectId ID для переадресации
* @throws Exception
*/
public function __construct(public string $id, array $params)
{
public function __construct(
public readonly string $id,
array $params,
public readonly ?string $redirectId = null
) {
empty($params['pls']) && throw new Exception(
"Плейлист с ID=$id обязан иметь параметр pls или redirect"
);
$this->url = base_url($id);
$this->name = empty($params['name']) ? "Плейлист #$id" : $params['name'];
$this->desc = empty($params['desc']) ? null : $params['desc'];
@@ -56,7 +82,172 @@ class Playlist extends BasicPlaylist
}
/**
* @inheritDoc
* Получает содержимое плейлиста с третьей стороны
*
* @return void
*/
public function download(): void
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $this->pls,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => false,
CURLOPT_FAILONERROR => true,
]);
$content = curl_exec($curl);
$this->rawContent = $content === false ? null : $content;
$this->downloadStatus['httpCode'] = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
$this->downloadStatus['errCode'] = curl_errno($curl);
$this->downloadStatus['errText'] = curl_error($curl);
$this->downloadStatus['possibleStatus'] = $this->guessStatus($this->downloadStatus['errCode']);
curl_close($curl);
}
/**
* Возвращает статус проверки плейлиста по коду ошибки curl
*
* @param int $curlErrCode
* @return string
*/
protected function guessStatus(int $curlErrCode): string
{
return match ($curlErrCode) {
0 => 'online',
28 => 'timeout',
5, 6, 7, 22, 35 => 'offline',
default => 'error',
};
}
/**
* Парсит полученный от третьей стороны плейлист
*
* @return array Информация о составе плейлиста
* @throws RandomException
*/
public function parse(): array
{
if (!empty($this->parsed())) {
return $this->parsed();
}
$result = [
'attributes' => [],
'channels' => [],
'groups' => [],
'encoding' => [
'name' => 'unknown',
'alert' => false,
],
];
if (is_null($this->rawContent)) {
return $this->parsedContent = $result;
}
$enc = mb_detect_encoding($this->rawContent, config('app.pls_encodings'));
$result['encoding']['name'] = $enc;
if ($enc !== 'UTF-8') {
$result['encoding']['alert'] = true;
$this->rawContent = mb_convert_encoding($this->rawContent, 'UTF-8', $enc);
}
$lines = explode("\n", $this->rawContent);
$isHeader = $isGroup = $isChannel = false;
foreach ($lines as $line) {
if (empty($line = trim($line))) {
continue;
}
if (str_starts_with($line, '#EXTM3U ')) {
$isHeader = true;
$isGroup = $isChannel = false;
$result['attributes'] = $this->parseAttributes($line);
continue;
}
if (str_starts_with($line, '#EXTINF:')) {
$isChannel = true;
$isHeader = $isGroup = false;
$combined = trim(substr($line, strpos($line, ',') + 1));
$exploded = explode(',', $line);
$attrs = $this->parseAttributes($exploded[0]);
$tvgid = empty($attrs['tvg-id']) ? ' неизвестен' : "='{$attrs['tvg-id']}'";
$name = trim($exploded[1] ?? "(канал без названия, tvg-id$tvgid)");
$channel = [
'_id' => md5($name . random_int(1, 99999)),
'name' => trim($name),
'url' => null,
'group' => $attrs['group-title'] ?? null,
'attributes' => $attrs,
];
unset($name, $attrs, $combined, $exploded);
continue;
}
if (str_starts_with($line, '#EXTGRP:')) {
$isGroup = true;
$isHeader = false;
if ($isChannel) {
$exploded = explode(':', $line);
$channel['group'] = $exploded[1];
}
continue;
}
if ($isChannel) {
$channel['url'] = str_starts_with($line, 'http') ? $line : null;
$result['channels'][] = $channel;
$isChannel = false;
unset($channel);
}
}
$groups = [];
foreach ($result['channels'] as $channel) {
$name = $channel['group'] ?? '(без группы)';
$id = md5($name);
if (empty($groups[$id])) {
$groups[$id] = [
'_id' => $id,
'name' => $name,
'channels' => [],
];
}
$groups[$id]['channels'][] = $channel['_id'];
}
$result['groups'] = array_values($groups);
return $this->parsedContent = $result;
}
/**
* Парсит атрибуты строки и возвращает ассоциативный массив
*
* @param string $line
* @return array
*/
protected function parseAttributes(string $line): array
{
if (str_starts_with($line, '#')) {
$line = trim(substr($line, strpos($line, ' ') + 1));
}
preg_match_all('#(?<key>[a-z-]+)="(?<value>.*)"#U', $line, $matches);
return array_combine($matches['key'], $matches['value']);
}
/**
* Возвращает содержимое объекта в виде массива
*
* @return array
*/
public function toArray(): array
{
@@ -67,6 +258,41 @@ class Playlist extends BasicPlaylist
'desc' => $this->desc,
'pls' => $this->pls,
'src' => $this->src,
'status' => $this->status(),
'content' => [
...$this->parsed(),
'channelCount' => count($this->parsed()['channels'])
],
];
}
/**
* Возвращает ссылку на плейлист в рамках проекта
*
* @return string
*/
public function url(): string
{
return sprintf('%s/%s', base_url(), $this->id);
}
/**
* Возвращает статус скачивания плейлиста
*
* @return array|string[]
*/
public function status(): array
{
return $this->downloadStatus;
}
/**
* Возвращает обработанное содержимое плейлиста
*
* @return array
*/
public function parsed(): array
{
return $this->parsedContent;
}
}

View File

@@ -1,175 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Core;
use App\Exceptions\PlaylistNotFoundException;
use Illuminate\Support\Collection;
/**
* Обработчик списка плейлистов
*/
final class PlaylistProcessor
{
/**
* @var Collection Коллекция подгруженных плейлистов
*/
public Collection $playlists;
/**
* @var string Дата последнего обновления списка
*/
protected string $updated_at;
/**
* Конструктор
*/
public function __construct()
{
$filepath = config_path('playlists.ini');
$this->updated_at = date('d.m.Y h:i', filemtime($filepath));
$this->playlists = collect(parse_ini_file($filepath, true))
->transform(static fn ($playlist, $id) => empty($playlist['redirect'])
? new Playlist((string)$id, $playlist)
: new RedirectedPlaylist((string)$id, $playlist['redirect'])
);
}
/**
* Проверяет есть ли в списке плейлист по его id
*
* @param string $id
* @return bool
*/
public function hasId(string $id): bool
{
return $this->playlists->keys()->contains($id);
}
/**
* Возвращает из коллекции указанный плейлист, если он существует
*
* @param string $id
* @return Playlist|RedirectedPlaylist
* @throws PlaylistNotFoundException
*/
public function playlist(string $id): Playlist|RedirectedPlaylist
{
!$this->hasId($id) && throw new PlaylistNotFoundException($id);
return $this->playlists[$id];
}
/**
* Проверяет доступность плейлиста на третьей стороне
*
* @param string $id
* @return bool
* @throws PlaylistNotFoundException
*/
public function check(string $id): bool
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $this->playlist($id)->pls,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_HEADER => false,
CURLOPT_NOBODY => true,
]);
curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_close($curl);
return $code < 400;
}
/**
* Получает содержимое плейлиста с третьей стороны
*
* @param string $id
* @return array
* @throws PlaylistNotFoundException
*/
protected function fetch(string $id): array
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $this->playlist($id)->pls,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => false,
CURLOPT_FAILONERROR => true,
]);
$content = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
$err_code = curl_errno($curl);
$err_text = curl_error($curl);
curl_close($curl);
return [
'content' => $content,
'http_code' => $http_code,
'err_code' => $err_code,
'err_text' => $err_text,
];
}
/**
* Возвращает статус проверки плейлиста по коду ошибки curl
*
* @param int $curl_err_code
* @return string
*/
protected function guessStatus(int $curl_err_code): string
{
return match ($curl_err_code) {
0 => 'online',
28 => 'timeout',
5, 6, 7, 22, 35 => 'offline',
default => 'error',
};
}
/**
* Парсит полученный от третьей стороны плейлист
*
* @param string $id
* @return array Информация о составе плейлиста
* @throws PlaylistNotFoundException
*/
public function parse(string $id): array
{
$fetched = $this->fetch($id);
if ($fetched['err_code'] > 0) {
return [
'status' => $this->guessStatus($fetched['err_code']),
'error' => [
'code' => $fetched['err_code'],
'message' => $fetched['err_text'],
],
];
}
$result['status'] = $this->guessStatus($fetched['err_code']);
$result['encoding']['name'] = 'UTF-8';
$result['encoding']['alert'] = false;
if (($enc = mb_detect_encoding($fetched['content'], config('app.pls_encodings'))) !== 'UTF-8') {
$fetched['content'] = mb_convert_encoding($fetched['content'], 'UTF-8', $enc);
$result['encoding']['name'] = $enc;
$result['encoding']['alert'] = true;
}
$matches = [];
preg_match_all("/^#EXTINF:-?\d.*,\s*(.*)/m", $fetched['content'], $matches);
$result['channels'] = array_map('trim', $matches[1]);
$result['count'] = $fetched['http_code'] < 400 ? count($result['channels']) : 0;
return $result;
}
/**
* Возвращает дату последнего обновления списка плейлистов
*
* @return string
*/
public function updatedAt(): string
{
return $this->updated_at;
}
}

View File

@@ -1,34 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Core;
/**
* Плейлист с редиректом
*/
class RedirectedPlaylist extends BasicPlaylist
{
/**
* Конструктор
*
* @param string $id
* @param string $redirect_id
*/
public function __construct(
public string $id,
public string $redirect_id,
) {
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'id' => $this->id,
'redirect_id' => $this->redirect_id,
];
}
}

View File

@@ -8,8 +8,8 @@ use Exception;
class PlaylistNotFoundException extends Exception
{
public function __construct(string $pls_code)
public function __construct(string $id)
{
parent::__construct("Плейлист $pls_code не найден!");
parent::__construct("Плейлист $id не найден!");
}
}

View File

@@ -84,6 +84,7 @@ function env(string $key, mixed $default = null): mixed
function view(mixed $template, array $data = []): void
{
$template = str_contains($template, '.twig') ? $template : "$template.twig";
/** @noinspection PhpVoidFunctionResultUsedInspection */
echo Flight::view()->render($template, $data);
}
@@ -134,16 +135,5 @@ function bool(mixed $value): bool
*/
function config(string $key, mixed $default = null): mixed
{
$config = Flight::get('config');
if (isset($config["flight.$key"])) {
return $config["flight.$key"];
}
if (isset($config[$key])) {
return $config[$key];
}
$config = Arr::undot($config);
if (Arr::has($config, $key)) {
return Arr::get($config, $key);
}
return $default;
return Flight::get('config')[$key] ?? $default;
}