Support /live and /shorts for Youtube metadata lookups.

This commit is contained in:
flash 2024-01-06 11:04:17 +00:00
parent d1bab14435
commit f2c25e2c6f
2 changed files with 31 additions and 18 deletions

24
composer.lock generated
View file

@ -12,7 +12,7 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://patchii.net/flash/index.git", "url": "https://patchii.net/flash/index.git",
"reference": "e31781c69f0b13fe251771c8e7e529222630a44f" "reference": "18397477d6734b4336c8b273f069cd8584173435"
}, },
"require": { "require": {
"ext-mbstring": "*", "ext-mbstring": "*",
@ -50,7 +50,7 @@
], ],
"description": "Composer package for the common library for my projects.", "description": "Composer package for the common library for my projects.",
"homepage": "https://railgun.sh/index", "homepage": "https://railgun.sh/index",
"time": "2023-11-20T19:01:19+00:00" "time": "2024-01-04T02:07:43+00:00"
}, },
{ {
"name": "flashwave/syokuhou", "name": "flashwave/syokuhou",
@ -58,7 +58,7 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://patchii.net/flash/syokuhou.git", "url": "https://patchii.net/flash/syokuhou.git",
"reference": "fdf3c38cc216bf7024af331cbe1758532355c22f" "reference": "c1fe9371ada20fcea51c225cc53b9ceae4642bc4"
}, },
"require": { "require": {
"flashwave/index": "dev-master", "flashwave/index": "dev-master",
@ -89,7 +89,7 @@
], ],
"description": "Configuration library for PHP.", "description": "Configuration library for PHP.",
"homepage": "https://railgun.sh/syokuhou", "homepage": "https://railgun.sh/syokuhou",
"time": "2023-11-20T19:10:04+00:00" "time": "2024-01-04T02:12:49+00:00"
}, },
{ {
"name": "guzzlehttp/psr7", "name": "guzzlehttp/psr7",
@ -525,16 +525,16 @@
}, },
{ {
"name": "sentry/sentry", "name": "sentry/sentry",
"version": "4.1.0", "version": "4.3.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/getsentry/sentry-php.git", "url": "https://github.com/getsentry/sentry-php.git",
"reference": "89666f297891ff937fceb2f3d1fb967a6848cf37" "reference": "cd89f230bda0833cb9992ebe9a1b7d24d6ee245b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/getsentry/sentry-php/zipball/89666f297891ff937fceb2f3d1fb967a6848cf37", "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/cd89f230bda0833cb9992ebe9a1b7d24d6ee245b",
"reference": "89666f297891ff937fceb2f3d1fb967a6848cf37", "reference": "cd89f230bda0833cb9992ebe9a1b7d24d6ee245b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -558,7 +558,7 @@
"phpbench/phpbench": "^1.0", "phpbench/phpbench": "^1.0",
"phpstan/phpstan": "^1.3", "phpstan/phpstan": "^1.3",
"phpunit/phpunit": "^8.5.14|^9.4", "phpunit/phpunit": "^8.5.14|^9.4",
"symfony/phpunit-bridge": "^5.2|^6.0", "symfony/phpunit-bridge": "^5.2|^6.0|^7.0",
"vimeo/psalm": "^4.17" "vimeo/psalm": "^4.17"
}, },
"suggest": { "suggest": {
@ -598,7 +598,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/getsentry/sentry-php/issues", "issues": "https://github.com/getsentry/sentry-php/issues",
"source": "https://github.com/getsentry/sentry-php/tree/4.1.0" "source": "https://github.com/getsentry/sentry-php/tree/4.3.1"
}, },
"funding": [ "funding": [
{ {
@ -610,7 +610,7 @@
"type": "custom" "type": "custom"
} }
], ],
"time": "2023-12-04T12:41:21+00:00" "time": "2023-12-22T18:46:49+00:00"
}, },
{ {
"name": "symfony/deprecation-contracts", "name": "symfony/deprecation-contracts",
@ -758,5 +758,5 @@
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.3.0" "plugin-api-version": "2.6.0"
} }

View file

@ -11,6 +11,12 @@ final class YouTubeLookup implements \Uiharu\ILookup {
'youtu.be', 'www.youtu.be', // www. doesn't work for this, but may as well cover it 'youtu.be', 'www.youtu.be', // www. doesn't work for this, but may as well cover it
]; ];
private const WATCH_PREFIXES = [
'/watch/',
'/live/',
'/shorts/',
];
private const VALID_TLDS = [ private const VALID_TLDS = [
'ae', 'at', 'az', 'ba', 'be', 'bg', 'bh', 'bo', 'by', 'ae', 'at', 'az', 'ba', 'be', 'bg', 'bh', 'bo', 'by',
'ca', 'cat', 'ch', 'cl', 'co', 'co.ae', 'co.at', 'co.cr', 'co.hu', 'ca', 'cat', 'ch', 'cl', 'co', 'co.ae', 'co.at', 'co.cr', 'co.hu',
@ -67,8 +73,14 @@ final class YouTubeLookup implements \Uiharu\ILookup {
} }
$urlPath = $url->getPath(); $urlPath = $url->getPath();
return $urlPath === '/watch' if($urlPath === '/watch')
|| str_starts_with($urlPath, '/watch/'); return true;
foreach(self::WATCH_PREFIXES as $prefix)
if(str_starts_with($urlPath, $prefix))
return true;
return false;
} }
private function lookupVideo(string $videoId): ?object { private function lookupVideo(string $videoId): ?object {
@ -99,11 +111,12 @@ final class YouTubeLookup implements \Uiharu\ILookup {
if(self::isShortDomain($url->getHost())) { if(self::isShortDomain($url->getHost())) {
$videoId = substr($urlPath, 1); $videoId = substr($urlPath, 1);
} elseif(array_key_exists('v', $urlQuery)) {
$videoId = $urlQuery['v'];
} else { } else {
if(str_starts_with($urlPath, '/watch/')) $urlPathParts = explode('/', trim($urlPath, '/'));
$videoId = explode('/', trim($urlPath, '/'))[1] ?? ''; if(count($urlPathParts) > 1)
else $videoId = $urlPathParts[1];
$videoId = $urlQuery['v'] ?? '';
} }
if(empty($videoId)) if(empty($videoId))