From f2c25e2c6fbe4674bb38ebbe84c73fce7bd49e34 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 6 Jan 2024 11:04:17 +0000 Subject: [PATCH] Support /live and /shorts for Youtube metadata lookups. --- composer.lock | 24 ++++++++++++------------ src/Lookup/YouTubeLookup.php | 25 +++++++++++++++++++------ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/composer.lock b/composer.lock index 73a4c20..786bc83 100644 --- a/composer.lock +++ b/composer.lock @@ -12,7 +12,7 @@ "source": { "type": "git", "url": "https://patchii.net/flash/index.git", - "reference": "e31781c69f0b13fe251771c8e7e529222630a44f" + "reference": "18397477d6734b4336c8b273f069cd8584173435" }, "require": { "ext-mbstring": "*", @@ -50,7 +50,7 @@ ], "description": "Composer package for the common library for my projects.", "homepage": "https://railgun.sh/index", - "time": "2023-11-20T19:01:19+00:00" + "time": "2024-01-04T02:07:43+00:00" }, { "name": "flashwave/syokuhou", @@ -58,7 +58,7 @@ "source": { "type": "git", "url": "https://patchii.net/flash/syokuhou.git", - "reference": "fdf3c38cc216bf7024af331cbe1758532355c22f" + "reference": "c1fe9371ada20fcea51c225cc53b9ceae4642bc4" }, "require": { "flashwave/index": "dev-master", @@ -89,7 +89,7 @@ ], "description": "Configuration library for PHP.", "homepage": "https://railgun.sh/syokuhou", - "time": "2023-11-20T19:10:04+00:00" + "time": "2024-01-04T02:12:49+00:00" }, { "name": "guzzlehttp/psr7", @@ -525,16 +525,16 @@ }, { "name": "sentry/sentry", - "version": "4.1.0", + "version": "4.3.1", "source": { "type": "git", "url": "https://github.com/getsentry/sentry-php.git", - "reference": "89666f297891ff937fceb2f3d1fb967a6848cf37" + "reference": "cd89f230bda0833cb9992ebe9a1b7d24d6ee245b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/89666f297891ff937fceb2f3d1fb967a6848cf37", - "reference": "89666f297891ff937fceb2f3d1fb967a6848cf37", + "url": "https://api.github.com/repos/getsentry/sentry-php/zipball/cd89f230bda0833cb9992ebe9a1b7d24d6ee245b", + "reference": "cd89f230bda0833cb9992ebe9a1b7d24d6ee245b", "shasum": "" }, "require": { @@ -558,7 +558,7 @@ "phpbench/phpbench": "^1.0", "phpstan/phpstan": "^1.3", "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" }, "suggest": { @@ -598,7 +598,7 @@ ], "support": { "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": [ { @@ -610,7 +610,7 @@ "type": "custom" } ], - "time": "2023-12-04T12:41:21+00:00" + "time": "2023-12-22T18:46:49+00:00" }, { "name": "symfony/deprecation-contracts", @@ -758,5 +758,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Lookup/YouTubeLookup.php b/src/Lookup/YouTubeLookup.php index f80810e..8bbbcde 100644 --- a/src/Lookup/YouTubeLookup.php +++ b/src/Lookup/YouTubeLookup.php @@ -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 ]; + private const WATCH_PREFIXES = [ + '/watch/', + '/live/', + '/shorts/', + ]; + private const VALID_TLDS = [ 'ae', 'at', 'az', 'ba', 'be', 'bg', 'bh', 'bo', 'by', '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(); - return $urlPath === '/watch' - || str_starts_with($urlPath, '/watch/'); + if($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 { @@ -99,11 +111,12 @@ final class YouTubeLookup implements \Uiharu\ILookup { if(self::isShortDomain($url->getHost())) { $videoId = substr($urlPath, 1); + } elseif(array_key_exists('v', $urlQuery)) { + $videoId = $urlQuery['v']; } else { - if(str_starts_with($urlPath, '/watch/')) - $videoId = explode('/', trim($urlPath, '/'))[1] ?? ''; - else - $videoId = $urlQuery['v'] ?? ''; + $urlPathParts = explode('/', trim($urlPath, '/')); + if(count($urlPathParts) > 1) + $videoId = $urlPathParts[1]; } if(empty($videoId))