77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Apoloz\VideoExtractor\Parsing;
|
|
|
|
use Apoloz\VideoExtractor\Dto\VideoRef;
|
|
use Apoloz\VideoExtractor\Exceptions\ParseFailedException;
|
|
use Apoloz\VideoExtractor\Platform\Platform;
|
|
|
|
/**
|
|
* Parses Rutube URLs into a {@see VideoRef}.
|
|
*
|
|
* Supported URL shapes (mirrors the yt-dlp `RutubeIE` `_VALID_URL`):
|
|
* https://rutube.ru/video/373ba417757981facf06c55ea5c46cb4/
|
|
* https://rutube.ru/live/video/c58f502c7bb34a8fcdd976b221fca292/
|
|
* https://rutube.ru/video/private/884fb55f07a97ab673c7d654553e0f48/?p=token
|
|
* https://rutube.ru/play/embed/03a9cb54bac3376af4c5cb0f18444e01
|
|
* https://rutube.ru/embed/03a9cb54bac3376af4c5cb0f18444e01
|
|
*
|
|
* The video id is a 32-character lowercase hex hash.
|
|
*/
|
|
final class RutubeUrlParser
|
|
{
|
|
private const ID_PATTERN = '[0-9a-f]{32}';
|
|
|
|
/**
|
|
* @throws ParseFailedException when the URL does not match any known shape
|
|
*/
|
|
public function parse(string $url): VideoRef
|
|
{
|
|
if (preg_match('#' . self::ID_PATTERN . '#i', $url, $m)) {
|
|
$id = strtolower($m[0]);
|
|
|
|
// Preserve private-video tokens (`?p=...`) which must be forwarded
|
|
// to the API requests (see yt-dlp #19163).
|
|
$query = $this->extractPrivateToken($url);
|
|
|
|
return new VideoRef(
|
|
platform: Platform::RUTUBE,
|
|
id: $id,
|
|
query: $query,
|
|
);
|
|
}
|
|
|
|
// Numeric embed id: rutube.ru/video/embed/6722881 — requires a
|
|
// play/options lookup to resolve the 32-hex `effective_video`.
|
|
if (preg_match('#/embed/(\d+)#i', $url, $m)) {
|
|
return new VideoRef(
|
|
platform: Platform::RUTUBE,
|
|
id: $m[1],
|
|
type: 'embed_numeric',
|
|
);
|
|
}
|
|
|
|
throw new ParseFailedException("URL does not look like a Rutube video: {$url}");
|
|
}
|
|
|
|
/**
|
|
* @return array<string, scalar|null>
|
|
*/
|
|
private function extractPrivateToken(string $url): array
|
|
{
|
|
$queryStr = (string) parse_url($url, PHP_URL_QUERY);
|
|
if ($queryStr === '') {
|
|
return [];
|
|
}
|
|
parse_str($queryStr, $query);
|
|
|
|
if (isset($query['p']) && is_string($query['p'])) {
|
|
return ['p' => $query['p']];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
}
|