diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eed41c6..9459c172 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [4.7.0] - 2026-11-20 +### Added +- YouTube: `$info->image` is `maxresdefault.jpg` when the video page links it in `og:image`, instead of the oEmbed `hqdefault.jpg` with black bars [#535] +- YouTube: the start (`t=` or `start=`) and end (`end=`) times of the URL are kept in the player URL, in seconds or in the `1m30s` format [#268] +- Vimeo adapter: `$info->image` is the full size thumbnail instead of the one resized to the player width (like `_295x166`) [#391] +- Google Drive adapter: shared files (`drive.google.com/file/d//view` and `drive.google.com/open?id=`) get the `/preview` iframe of Drive's "Embed item" option. Files that need a login get no code [#408] +- Tencent Video adapter: `v.qq.com/x/cover//.html`, `v.qq.com/x/page/.html` and `v.qq.com/x/cover/.html?vid=` URLs get a `v.qq.com/txp/iframe/player.html` iframe built from the URL [#243] +- README section about the results that are specific to some sites, and how to get `null` instead of the provider name fallback [#462] + +### Changed +- The provider name is also read from the `application-name` and `apple-mobile-web-app-title` metas before falling back to the domain name, and the fallback skips `www` (`www.gov.pt` was "Www") [#462] + ## [4.6.3] - 2026-11-20 ### Fixed - A `Referer` header set by the caller (for example with `addDefaultHeaders()`) is kept when a request follows redirects, instead of being replaced by the previous URL. Vimeo videos with domain-level privacy need it to return their title, thumbnail and other metadata [#377] [#567] @@ -385,7 +397,13 @@ Full library refactoring. [#567]: https://github.com/php-embed/Embed/issues/567 [#574]: https://github.com/oscarotero/Embed/issues/574 [#577]: https://github.com/oscarotero/Embed/issues/577 +[#268]: https://github.com/php-embed/Embed/issues/268 +[#391]: https://github.com/php-embed/Embed/issues/391 +[#408]: https://github.com/php-embed/Embed/issues/408 +[#462]: https://github.com/php-embed/Embed/issues/462 +[#535]: https://github.com/php-embed/Embed/issues/535 +[4.7.0]: https://github.com/php-embed/Embed/compare/v4.6.3...v4.7.0 [4.6.3]: https://github.com/php-embed/Embed/compare/v4.6.2...v4.6.3 [4.6.2]: https://github.com/php-embed/Embed/compare/v4.6.1...v4.6.2 [4.6.1]: https://github.com/php-embed/Embed/compare/v4.6.0...v4.6.1 diff --git a/README.md b/README.md index 99d6e950..ef184696 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,36 @@ foreach ($infos as $info) { } ``` +## Site-specific results + +Some sites get extra handling from their adapters: + +- **YouTube**: `$info->image` is `maxresdefault.jpg` when the video page links it in `og:image` (YouTube only does it for videos that have it), and the oEmbed `hqdefault.jpg` otherwise. The start (`t=` or `start=`) and end (`end=`) times of the URL are kept in the player URL, in seconds or in the `1m30s` format, for example `https://www.youtube.com/watch?v=ID&t=10&end=20`. +- **Vimeo**: `$info->image` is the thumbnail at its full size, without the size suffix that the oEmbed `thumbnail_url` has (like `_295x166`). The resized one is still available with `$info->getOEmbed()->url('thumbnail_url')`. +- **Google Drive**: shared files (`drive.google.com/file/d//view` or `drive.google.com/open?id=`) get the same `/preview` iframe as Drive's "Embed item" option. Files that need a login return no code. +- **Bilibili, Youku and Tencent Video**: the player iframe is built from the video URL (`bilibili.com/video/BV...`, `v.youku.com/v_show/id_.html`, `v.qq.com/x/cover//.html` or `v.qq.com/x/page/.html`). Tencent Video pages are rendered with JavaScript, so their title and image are often missing. +- **NetEase Music**: songs, albums and playlists get the `music.163.com/outchain/player` iframe. + +`$info->providerName` is read from the oEmbed `provider_name`, the `og:site_name`, `publisher` and similar metas, and then `application-name` or `apple-mobile-web-app-title`. When none of them is found, it falls back to the capitalized domain name (`Example` for `www.example.com`). To get `null` instead of that fallback, replace the detector: + +```php +use Embed\Detectors\Detector; + +class StrictProviderName extends Detector +{ + public function detect(): ?string + { + $oembed = $this->extractor->getOEmbed(); + $metas = $this->extractor->getMetas(); + + return $oembed->str('provider_name') + ?? $metas->str('og:site_name', 'publisher', 'application-name', 'apple-mobile-web-app-title'); + } +} + +$embed->getExtractorFactory()->addDetector('providerName', StrictProviderName::class); +``` + ## Document The document is the object that store the html code of the page. You can use it to extract extra info from the html code: diff --git a/src/Adapters/GoogleDrive/Detectors/Code.php b/src/Adapters/GoogleDrive/Detectors/Code.php new file mode 100644 index 00000000..0a14302e --- /dev/null +++ b/src/Adapters/GoogleDrive/Detectors/Code.php @@ -0,0 +1,47 @@ + + */ +class Code extends Detector +{ + public function detect(): ?EmbedCode + { + $result = parent::detect(); + + if ($result !== null) { + return $result; + } + + // Private or deleted files answer with an error status, + // there is nothing to embed for them. + if ($this->extractor->getResponse()->getStatusCode() >= 400) { + return null; + } + + $id = Extractor::parseFileId($this->extractor->getUri()) + ?? Extractor::parseFileId($this->extractor->getRequest()->getUri()); + + if ($id === null) { + return null; + } + + // Same iframe as Drive's "Embed item" option. + return new EmbedCode(html('iframe', [ + 'src' => 'https://drive.google.com/file/d/'.rawurlencode($id).'/preview', + 'width' => 640, + 'height' => 480, + 'allow' => 'autoplay', + 'frameborder' => 0, + 'allowfullscreen' => 'true', + ]), 640, 480); + } +} diff --git a/src/Adapters/GoogleDrive/Extractor.php b/src/Adapters/GoogleDrive/Extractor.php new file mode 100644 index 00000000..07750023 --- /dev/null +++ b/src/Adapters/GoogleDrive/Extractor.php @@ -0,0 +1,44 @@ +> + */ +class Extractor extends Base +{ + public function createCustomDetectors(): array + { + return [ + 'code' => new Detectors\Code($this), + ]; + } + + /** + * Returns the file id of drive.google.com/file/d//view (also /edit, + * /preview and /u// variants) and drive.google.com/open?id= URLs. + */ + public static function parseFileId(UriInterface $uri): ?string + { + if (preg_match('#^/file(?:/u/\d+)?/d/([A-Za-z0-9_-]{10,})(?:/|$)#', $uri->getPath(), $matches) === 1) { + return $matches[1]; + } + + if ($uri->getPath() !== '/open') { + return null; + } + + parse_str($uri->getQuery(), $query); + $id = $query['id'] ?? null; + + if (is_string($id) && preg_match('/^[A-Za-z0-9_-]{10,}$/', $id) === 1) { + return $id; + } + + return null; + } +} diff --git a/src/Adapters/TencentVideo/Detectors/Code.php b/src/Adapters/TencentVideo/Detectors/Code.php new file mode 100644 index 00000000..62aa08c4 --- /dev/null +++ b/src/Adapters/TencentVideo/Detectors/Code.php @@ -0,0 +1,34 @@ + + */ +class Code extends Detector +{ + public function detect(): ?EmbedCode + { + $vid = Extractor::parseVideoId($this->extractor->getUri()); + + if ($vid === null) { + return parent::detect(); + } + + // Same player as Tencent Video's share code. + return new EmbedCode(html('iframe', [ + 'src' => 'https://v.qq.com/txp/iframe/player.html?vid='.rawurlencode($vid), + 'frameborder' => 0, + 'allowfullscreen' => 'true', + 'scrolling' => 'no', + 'width' => 640, + 'height' => 360, + ]), 640, 360); + } +} diff --git a/src/Adapters/TencentVideo/Extractor.php b/src/Adapters/TencentVideo/Extractor.php new file mode 100644 index 00000000..9ecd42a6 --- /dev/null +++ b/src/Adapters/TencentVideo/Extractor.php @@ -0,0 +1,45 @@ +> + */ +class Extractor extends Base +{ + public function createCustomDetectors(): array + { + return [ + 'code' => new Detectors\Code($this), + ]; + } + + /** + * Returns the video id (vid) of v.qq.com/x/cover//.html, + * v.qq.com/x/page/.html and v.qq.com/x/cover/.html?vid= URLs. + * Cover URLs without a vid point to a series, not to one video. + */ + public static function parseVideoId(UriInterface $uri): ?string + { + if (preg_match('#^/x/(?:cover/[0-9a-z]+|page)/([0-9a-z]{11})\.html$#i', $uri->getPath(), $matches) === 1) { + return $matches[1]; + } + + if (preg_match('#^/x/cover/[0-9a-z]+\.html$#i', $uri->getPath()) !== 1) { + return null; + } + + parse_str($uri->getQuery(), $query); + $vid = $query['vid'] ?? null; + + if (is_string($vid) && preg_match('/^[0-9a-z]{11}$/i', $vid) === 1) { + return $vid; + } + + return null; + } +} diff --git a/src/Adapters/Vimeo/Detectors/Image.php b/src/Adapters/Vimeo/Detectors/Image.php new file mode 100644 index 00000000..4ce29041 --- /dev/null +++ b/src/Adapters/Vimeo/Detectors/Image.php @@ -0,0 +1,38 @@ + + */ +class Image extends Detector +{ + public function detect(): ?UriInterface + { + $image = parent::detect(); + + if ($image === null || $image->getHost() !== 'i.vimeocdn.com') { + return $image; + } + + return self::fullSize($image); + } + + /** + * Vimeo's oEmbed thumbnail is resized to the player width + * (like .../video/123-abc-d_295x166). Without the size suffix, + * Vimeo's CDN serves the image at its full size. + */ + public static function fullSize(UriInterface $image): UriInterface + { + if (preg_match('#^(/video/[^/_]+)_\d+(?:x\d+)?(\.[a-z]+)?$#i', $image->getPath(), $matches) !== 1) { + return $image; + } + + return $image->withPath($matches[1].($matches[2] ?? '')); + } +} diff --git a/src/Adapters/Vimeo/Extractor.php b/src/Adapters/Vimeo/Extractor.php new file mode 100644 index 00000000..063559ba --- /dev/null +++ b/src/Adapters/Vimeo/Extractor.php @@ -0,0 +1,19 @@ +> + */ +class Extractor extends Base +{ + public function createCustomDetectors(): array + { + return [ + 'image' => new Detectors\Image($this), + ]; + } +} diff --git a/src/Adapters/Youtube/Detectors/Code.php b/src/Adapters/Youtube/Detectors/Code.php new file mode 100644 index 00000000..14c1ee72 --- /dev/null +++ b/src/Adapters/Youtube/Detectors/Code.php @@ -0,0 +1,105 @@ + + */ +class Code extends Detector +{ + public function detect(): ?EmbedCode + { + $code = parent::detect(); + + if ($code === null) { + return null; + } + + $times = array_filter([ + 'start' => $this->detectTime('start', 't'), + 'end' => $this->detectTime('end'), + ], static fn (?int $time): bool => $time !== null); + + if ($times === []) { + return $code; + } + + return self::withTimes($code, $times); + } + + /** + * YouTube's oEmbed only keeps the start time when it gets the exact URL, + * and always drops the end time. Read both from the requested URL. + */ + private function detectTime(string ...$names): ?int + { + foreach ([$this->extractor->getRequest()->getUri(), $this->extractor->getUri()] as $uri) { + parse_str($uri->getQuery(), $query); + + foreach ($names as $name) { + $value = $query[$name] ?? null; + + if (is_string($value)) { + $time = self::parseTime($value); + + if ($time !== null) { + return $time; + } + } + } + } + + return null; + } + + /** + * Accepts seconds (90) or YouTube's time format (1m30s, 1h2m3s). + */ + public static function parseTime(string $time): ?int + { + if (preg_match('/^\d+$/', $time) === 1) { + $seconds = (int) $time; + } elseif (preg_match('/^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/', $time, $matches) === 1 && $time !== '') { + $seconds = ((int) ($matches[1] ?? 0)) * 3600 + ((int) ($matches[2] ?? 0)) * 60 + ((int) ($matches[3] ?? 0)); + } else { + return null; + } + + return $seconds > 0 ? $seconds : null; + } + + /** + * @param array $times + */ + private static function withTimes(EmbedCode $code, array $times): EmbedCode + { + $html = preg_replace_callback( + '#(]*?\bsrc=")(https://www\.youtube(?:-nocookie)?\.com/embed/[^"]*)(")#i', + static function (array $matches) use ($times): string { + $src = $matches[2]; + + foreach ($times as $name => $value) { + if (preg_match('/[?&](?:amp;)?'.$name.'=/', $src) === 1) { + continue; + } + + $src .= (strpos($src, '?') === false ? '?' : '&').$name.'='.$value; + } + + return $matches[1].$src.$matches[3]; + }, + $code->html, + 1 + ); + + if (!is_string($html) || $html === $code->html) { + return $code; + } + + return new EmbedCode($html, $code->width, $code->height); + } +} diff --git a/src/Adapters/Youtube/Detectors/Image.php b/src/Adapters/Youtube/Detectors/Image.php new file mode 100644 index 00000000..3105f37a --- /dev/null +++ b/src/Adapters/Youtube/Detectors/Image.php @@ -0,0 +1,52 @@ + + */ +class Image extends Detector +{ + public function detect(): ?UriInterface + { + $image = parent::detect(); + + return $this->detectMaxRes($image) ?? $image; + } + + /** + * YouTube's oEmbed thumbnail is always hqdefault.jpg (480x360, with black + * bars on 16:9 videos). The page links maxresdefault.jpg in og:image only + * when the video has it, so use it when it belongs to the same video. + */ + private function detectMaxRes(?UriInterface $image): ?UriInterface + { + if ($image === null || $image->getHost() !== 'i.ytimg.com') { + return null; + } + + if (preg_match('#^/vi/([^/]+)/hqdefault\.jpg$#', $image->getPath(), $matches) !== 1) { + return null; + } + + $metas = $this->extractor->getMetas(); + + foreach (['og:image', 'twitter:image'] as $key) { + $url = $metas->url($key); + + if ( + $url !== null + && $url->getHost() === 'i.ytimg.com' + && $url->getPath() === "/vi/{$matches[1]}/maxresdefault.jpg" + ) { + return $url; + } + } + + return null; + } +} diff --git a/src/Adapters/Youtube/Extractor.php b/src/Adapters/Youtube/Extractor.php index 1b81d63f..34c59012 100644 --- a/src/Adapters/Youtube/Extractor.php +++ b/src/Adapters/Youtube/Extractor.php @@ -14,6 +14,8 @@ public function createCustomDetectors(): array { return [ 'feeds' => new Detectors\Feeds($this), + 'image' => new Detectors\Image($this), + 'code' => new Detectors\Code($this), ]; } } diff --git a/src/Detectors/ProviderName.php b/src/Detectors/ProviderName.php index 1340aa7b..ed90f448 100644 --- a/src/Detectors/ProviderName.php +++ b/src/Detectors/ProviderName.php @@ -32,6 +32,12 @@ public function detect(): string return $result; } + // Names that sites give to themselves when added to a home screen + $result = $metas->str('application-name', 'apple-mobile-web-app-title'); + if (is_string($result) && trim($result) !== '') { + return $result; + } + return ucfirst($this->fallback()); } @@ -39,6 +45,9 @@ private function fallback(): string { $host = $this->extractor->getUri()->getHost(); + // "www" is never the name of a site, even under suffixes like gov.pt + $host = (string) preg_replace('/^www\d*\./i', '', $host); + $host = array_reverse(explode('.', $host)); switch (count($host)) { diff --git a/src/ExtractorFactory.php b/src/ExtractorFactory.php index f5cb6fcb..43f9f104 100644 --- a/src/ExtractorFactory.php +++ b/src/ExtractorFactory.php @@ -28,6 +28,7 @@ class ExtractorFactory 'instagram.com' => Adapters\Instagram\Extractor::class, 'imageshack.com' => Adapters\ImageShack\Extractor::class, 'youtube.com' => Adapters\Youtube\Extractor::class, + 'vimeo.com' => Adapters\Vimeo\Extractor::class, 'twitch.tv' => Adapters\Twitch\Extractor::class, 'kick.com' => Adapters\Kick\Extractor::class, 'bandcamp.com' => Adapters\Bandcamp\Extractor::class, @@ -36,6 +37,8 @@ class ExtractorFactory 'bilibili.com' => Adapters\Bilibili\Extractor::class, 'youku.com' => Adapters\Youku\Extractor::class, 'music.163.com' => Adapters\NetEase\Extractor::class, + 'v.qq.com' => Adapters\TencentVideo\Extractor::class, + 'drive.google.com' => Adapters\GoogleDrive\Extractor::class, ]; /** @var array>> */ private array $customDetectors = []; diff --git a/tests/ChineseProvidersTest.php b/tests/ChineseProvidersTest.php index 66053c57..415682a1 100644 --- a/tests/ChineseProvidersTest.php +++ b/tests/ChineseProvidersTest.php @@ -12,7 +12,7 @@ use Psr\Http\Message\ResponseInterface; /** - * Bilibili, Youku and NetEase Music adapters [#243]. + * Bilibili, Youku, NetEase Music and Tencent Video adapters [#243]. * * The player iframes are built from the URL alone, so these tests use * offline pages. Youku and NetEase real pages are covered by PagesTest @@ -220,6 +220,40 @@ private function page(string $head): string /** * @param array $routes path => body */ + /** + * @dataProvider tencentVideoUrlProvider + */ + public function testTencentVideoBuildsPlayerIframe(string $url, string $path): void + { + $info = $this->embed([$path => '腾讯视频']) + ->get($url); + + $this->assertNotNull($info->code); + $this->assertSame( + '', + $info->code->html + ); + $this->assertSame(640, $info->code->width); + $this->assertSame(360, $info->code->height); + } + + public function tencentVideoUrlProvider(): array + { + return [ + 'cover' => ['https://v.qq.com/x/cover/mzc002001bk7moh/w41029n1rrv.html', '/x/cover/mzc002001bk7moh/w41029n1rrv.html'], + 'page' => ['https://v.qq.com/x/page/w41029n1rrv.html', '/x/page/w41029n1rrv.html'], + 'cover with vid' => ['https://v.qq.com/x/cover/mzc002001bk7moh.html?vid=w41029n1rrv', '/x/cover/mzc002001bk7moh.html'], + ]; + } + + public function testTencentVideoCoverWithoutVidHasNoIframe(): void + { + $info = $this->embed(['/x/cover/mzc002001bk7moh.html' => '腾讯视频']) + ->get('https://v.qq.com/x/cover/mzc002001bk7moh.html'); + + $this->assertNull($info->code); + } + private function embed(array $routes): Embed { $this->client = $client = new RecordingClient(static function (RequestInterface $request) use ($routes): ResponseInterface { diff --git a/tests/PagesTest.php b/tests/PagesTest.php index fb117fb8..f734f08b 100644 --- a/tests/PagesTest.php +++ b/tests/PagesTest.php @@ -59,6 +59,8 @@ public function urlDataProvider(): array ['https://v.youku.com/v_show/id_XNTk1MTU4MDQ0OA==.html'], ['https://music.163.com/song?id=186016'], ['https://music.163.com/#/album?id=18905'], + ['https://v.qq.com/x/cover/mzc002001bk7moh/w41029n1rrv.html'], + ['https://drive.google.com/file/d/1sF_h2x3lULc-fV4FK9FRCnnF60XkXuz2/view'], ]; } diff --git a/tests/ProviderFeaturesTest.php b/tests/ProviderFeaturesTest.php new file mode 100644 index 00000000..98302a48 --- /dev/null +++ b/tests/ProviderFeaturesTest.php @@ -0,0 +1,299 @@ +embed([ + 'www.youtube.com/watch' => $this->page(""), + 'www.youtube.com/oembed' => $this->youtubeOEmbed(), + ])->get("https://www.youtube.com/watch?v={$id}"); + + $this->assertSame("https://i.ytimg.com/vi/{$id}/maxresdefault.jpg", (string) $info->image); + } + + public function testYoutubeKeepsHqdefaultWithoutMaxres(): void + { + $id = self::YOUTUBE_ID; + $info = $this->embed([ + 'www.youtube.com/watch' => $this->page(""), + 'www.youtube.com/oembed' => $this->youtubeOEmbed(), + ])->get("https://www.youtube.com/watch?v={$id}"); + + $this->assertSame("https://i.ytimg.com/vi/{$id}/hqdefault.jpg", (string) $info->image); + } + + public function testYoutubeIgnoresMaxresdefaultOfAnotherVideo(): void + { + $id = self::YOUTUBE_ID; + $info = $this->embed([ + 'www.youtube.com/watch' => $this->page(''), + 'www.youtube.com/oembed' => $this->youtubeOEmbed(), + ])->get("https://www.youtube.com/watch?v={$id}"); + + $this->assertSame("https://i.ytimg.com/vi/{$id}/hqdefault.jpg", (string) $info->image); + } + + public function testYoutubeAddsTheEndTimeToThePlayer(): void + { + $id = self::YOUTUBE_ID; + $info = $this->embed([ + 'www.youtube.com/watch' => $this->page(''), + 'www.youtube.com/oembed' => $this->youtubeOEmbed("https://www.youtube.com/embed/{$id}?start=10&feature=oembed"), + ])->get("https://www.youtube.com/watch?v={$id}&start=10&end=20"); + + $this->assertNotNull($info->code); + $this->assertStringContainsString("src=\"https://www.youtube.com/embed/{$id}?start=10&feature=oembed&end=20\"", $info->code->html); + $this->assertSame(200, $info->code->width); + $this->assertSame(113, $info->code->height); + } + + public function testYoutubeAcceptsTheEndTimeInMinutesAndSeconds(): void + { + $id = self::YOUTUBE_ID; + $info = $this->embed([ + 'www.youtube.com/watch' => $this->page(''), + 'www.youtube.com/oembed' => $this->youtubeOEmbed("https://www.youtube.com/embed/{$id}"), + ])->get("https://www.youtube.com/watch?v={$id}&end=1m30s"); + + $this->assertNotNull($info->code); + $this->assertStringContainsString("src=\"https://www.youtube.com/embed/{$id}?end=90\"", $info->code->html); + } + + public function testYoutubeAddsTheStartTimeWhenOEmbedDropsIt(): void + { + $id = self::YOUTUBE_ID; + $info = $this->embed([ + 'www.youtube.com/watch' => $this->page(''), + 'www.youtube.com/oembed' => $this->youtubeOEmbed(), + ])->get("https://www.youtube.com/watch?v={$id}&t=1m5s&end=90"); + + $this->assertNotNull($info->code); + $this->assertStringContainsString("src=\"https://www.youtube.com/embed/{$id}?feature=oembed&start=65&end=90\"", $info->code->html); + } + + public function testYoutubeWithoutEndTimeKeepsTheOEmbedCode(): void + { + $id = self::YOUTUBE_ID; + $info = $this->embed([ + 'www.youtube.com/watch' => $this->page(''), + 'www.youtube.com/oembed' => $this->youtubeOEmbed(), + ])->get("https://www.youtube.com/watch?v={$id}&end=soon"); + + $this->assertNotNull($info->code); + $this->assertStringContainsString("src=\"https://www.youtube.com/embed/{$id}?feature=oembed\"", $info->code->html); + } + + /** + * @dataProvider vimeoThumbnailProvider + */ + public function testVimeoUsesTheFullSizeThumbnail(string $thumbnail, string $expected): void + { + $info = $this->embed([ + 'vimeo.com/76979871' => $this->page(''), + 'vimeo.com/api/oembed.json' => ['application/json', (string) json_encode([ + 'type' => 'video', + 'title' => 'Video', + 'thumbnail_url' => $thumbnail, + 'thumbnail_width' => 295, + 'thumbnail_height' => 166, + ])], + ])->get('https://vimeo.com/76979871'); + + $this->assertSame($expected, (string) $info->image); + } + + public function vimeoThumbnailProvider(): array + { + return [ + 'current' => [ + 'https://i.vimeocdn.com/video/452001751-8216e0571c251a09d7a8387550942d89f7f86f6398f8ed886e639b0dd50d3c90-d_295x166?region=us', + 'https://i.vimeocdn.com/video/452001751-8216e0571c251a09d7a8387550942d89f7f86f6398f8ed886e639b0dd50d3c90-d?region=us', + ], + 'width only' => [ + 'https://i.vimeocdn.com/video/452001751-8216e0571c251a09d7a8387550942d89f7f86f6398f8ed886e639b0dd50d3c90-d_640', + 'https://i.vimeocdn.com/video/452001751-8216e0571c251a09d7a8387550942d89f7f86f6398f8ed886e639b0dd50d3c90-d', + ], + 'legacy' => [ + 'https://i.vimeocdn.com/video/938592587_295x166.jpg', + 'https://i.vimeocdn.com/video/938592587.jpg', + ], + 'full size' => [ + 'https://i.vimeocdn.com/video/938592587.jpg', + 'https://i.vimeocdn.com/video/938592587.jpg', + ], + 'other host' => [ + 'https://example.com/video/938592587_295x166.jpg', + 'https://example.com/video/938592587_295x166.jpg', + ], + ]; + } + + /** + * @dataProvider googleDriveUrlProvider + */ + public function testGoogleDriveBuildsPreviewIframe(string $url, string $path): void + { + $info = $this->embed([ + 'drive.google.com'.$path => $this->page(''), + ])->get($url); + + $this->assertNotNull($info->code); + $this->assertSame( + '', + $info->code->html + ); + $this->assertSame(640, $info->code->width); + $this->assertSame(480, $info->code->height); + } + + public function googleDriveUrlProvider(): array + { + $id = '1A2b3C4d5E6f7G8h9I0jKlMnOpQrStUvW'; + + return [ + 'view' => ["https://drive.google.com/file/d/{$id}/view?usp=sharing", "/file/d/{$id}/view"], + 'edit' => ["https://drive.google.com/file/d/{$id}/edit", "/file/d/{$id}/edit"], + 'user' => ["https://drive.google.com/file/u/1/d/{$id}/view", "/file/u/1/d/{$id}/view"], + 'open' => ["https://drive.google.com/open?id={$id}", '/open'], + ]; + } + + public function testGoogleDriveWithoutAccessHasNoIframe(): void + { + $info = $this->embed([])->get('https://drive.google.com/file/d/1A2b3C4d5E6f7G8h9I0jKlMnOpQrStUvW/view'); + + $this->assertNull($info->code); + } + + public function testGoogleDriveFolderHasNoIframe(): void + { + $info = $this->embed([ + 'drive.google.com/drive/folders/1A2b3C4d5E6f7G8h9I0jKlMnOpQrStUvW' => $this->page(''), + ])->get('https://drive.google.com/drive/folders/1A2b3C4d5E6f7G8h9I0jKlMnOpQrStUvW'); + + $this->assertNull($info->code); + } + + public function testProviderNameFromApplicationName(): void + { + $info = $this->embed([ + 'eportugal.gov.pt/' => $this->page(''), + ])->get('https://eportugal.gov.pt/'); + + $this->assertSame('ePortugal', $info->providerName); + } + + public function testProviderNameFromAppleWebAppTitle(): void + { + $info = $this->embed([ + 'eportugal.gov.pt/' => $this->page(''), + ])->get('https://eportugal.gov.pt/'); + + $this->assertSame('ePortugal', $info->providerName); + } + + public function testProviderNamePrefersSiteName(): void + { + $info = $this->embed([ + 'eportugal.gov.pt/' => $this->page( + '' + .'' + ), + ])->get('https://eportugal.gov.pt/'); + + $this->assertSame('ePortugal.gov.pt', $info->providerName); + } + + /** + * @dataProvider providerNameFallbackProvider + */ + public function testProviderNameFallbackSkipsWww(string $url, string $expected): void + { + $uri = (new Psr17Factory())->createUri($url); + $info = $this->embed([$uri->getHost().'/' => $this->page('')])->get($url); + + $this->assertSame($expected, $info->providerName); + } + + public function providerNameFallbackProvider(): array + { + return [ + 'www under a public suffix' => ['https://www.gov.pt/', 'Gov'], + 'subdomain under a public suffix' => ['https://eportugal.gov.pt/', 'Eportugal'], + 'www' => ['https://www.example.com/', 'Example'], + 'www under co.uk' => ['https://www.example.co.uk/', 'Example'], + ]; + } + + /** + * @param array $routes host+path => html, or [content type, body] + * @param array $settings + */ + private function embed(array $routes, array $settings = []): Embed + { + $client = new RecordingClient(static function (RequestInterface $request) use ($routes): ResponseInterface { + $factory = new Psr17Factory(); + $uri = $request->getUri(); + $route = $routes[$uri->getHost().$uri->getPath()] ?? null; + + if ($route === null) { + return $factory->createResponse(404)->withBody($factory->createStream('')); + } + + [$type, $body] = is_array($route) ? $route : ['text/html', $route]; + + return $factory->createResponse(200) + ->withHeader('Content-Type', $type) + ->withBody($factory->createStream($body)); + }); + + $crawler = new Crawler($client); + $crawler->setUrlPolicy(UrlPolicy::default()->withResolver(static function (string $host): array { + return ['1.1.1.1']; + })); + + $embed = new Embed($crawler); + $embed->setSettings($settings); + + return $embed; + } + + private function page(string $head): string + { + return "Test page{$head}"; + } + + /** + * @return array{0: string, 1: string} + */ + private function youtubeOEmbed(string $src = 'https://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed'): array + { + return ['application/json', (string) json_encode([ + 'type' => 'video', + 'title' => 'Video', + 'provider_name' => 'YouTube', + 'thumbnail_url' => 'https://i.ytimg.com/vi/'.self::YOUTUBE_ID.'/hqdefault.jpg', + 'width' => 200, + 'height' => 113, + 'html' => '', + ])]; + } +} diff --git a/tests/cache/drive.google.com.file-d-1sf_h2x3lulc-fv4fk9frcnnf60xkxuz2-view.php b/tests/cache/drive.google.com.file-d-1sf_h2x3lulc-fv4fk9frcnnf60xkxuz2-view.php new file mode 100644 index 00000000..2b79a28c --- /dev/null +++ b/tests/cache/drive.google.com.file-d-1sf_h2x3lulc-fv4fk9frcnnf60xkxuz2-view.php @@ -0,0 +1,71 @@ + [ + 'content-type' => [ + 'text/html; charset=utf-8' + ], + 'vary' => [ + 'Sec-Fetch-Dest, Sec-Fetch-Mode, Sec-Fetch-Site' + ], + 'x-robots-tag' => [ + 'noindex, nofollow, nosnippet' + ], + 'cache-control' => [ + 'no-cache, no-store, max-age=0, must-revalidate' + ], + 'pragma' => [ + 'no-cache' + ], + 'expires' => [ + 'Mon, 01 Jan 1990 00:00:00 GMT' + ], + 'date' => [ + 'Sat, 21 Nov 2026 01:07:05 GMT' + ], + 'content-security-policy' => [ + 'script-src \'report-sample\' \'nonce-j5UbfkCKvqAEYBcX38IJNA\' \'unsafe-inline\' \'unsafe-eval\' \'strict-dynamic\' https: http: \'wasm-unsafe-eval\';object-src \'none\';base-uri \'self\';report-uri https://csp.withgoogle.com/csp/viewer/', + 'require-trusted-types-for \'script\';report-uri https://csp.withgoogle.com/csp/docs-tt' + ], + 'reporting-endpoints' => [ + 'default="/file/d/1sF_h2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y' => [ + 'include-js-call-stacks-in-crash-reports' + ], + 'referrer-policy' => [ + 'strict-origin-when-cross-origin' + ], + 'content-encoding' => [ + 'gzip' + ], + 'server' => [ + 'ESF' + ], + 'x-xss-protection' => [ + '0' + ], + 'x-frame-options' => [ + 'SAMEORIGIN' + ], + 'x-content-type-options' => [ + 'nosniff' + ], + 'alt-svc' => [ + 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000' + ], + 'Content-Location' => [ + 'https://drive.google.com/file/d/1sF_h2x3lULc-fV4FK9FRCnnF60XkXuz2/view' + ], + 'X-Request-Time' => [ + '0.491 ms' + ] + ], + 'statusCode' => 200, + 'reasonPhrase' => 'OK', + 'body' => 'Sample.mp4 - Google Drive
' +]; diff --git a/tests/cache/v.qq.com.x-cover-mzc002001bk7moh-w41029n1rrv.html.php b/tests/cache/v.qq.com.x-cover-mzc002001bk7moh-w41029n1rrv.html.php new file mode 100644 index 00000000..a2e52135 --- /dev/null +++ b/tests/cache/v.qq.com.x-cover-mzc002001bk7moh-w41029n1rrv.html.php @@ -0,0 +1,112 @@ + [ + 'date' => [ + 'Sat, 21 Nov 2026 01:07:04 GMT' + ], + 'content-type' => [ + 'text/html; charset=utf-8' + ], + 'set-cookie' => [ + 'vqq_video_detail_is_gray=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT' + ], + 'vary' => [ + 'Accept-Encoding' + ], + 'origin-agent-cluster' => [ + '?0' + ], + 'server' => [ + 'Lego Server' + ], + 'x-cache-lookup' => [ + 'Cache Miss', + 'Cache Miss' + ], + 'content-encoding' => [ + 'gzip' + ], + 'cache-control' => [ + 'no-cache, no-store' + ], + 'age' => [ + '0' + ], + 'content-length' => [ + '42139' + ], + 'x-nws-log-uuid' => [ + '16111971914953753' + ], + 'access-control-expose-headers' => [ + 'X-Client-Ip', + 'X-Server-Ip', + 'X-Upstream-Ip', + 'Date' + ], + 'x-client-ip' => [ + '3.216.229.202' + ], + 'x-server-ip' => [ + '43.175.44.34_eth0' + ], + 'x-upstream-ip' => [ + '$upstream_server' + ], + 'x-forwarded-for' => [ + '3.216.229.202' + ], + 'x-ua-compatible' => [ + 'IE=Edge' + ], + 'Content-Location' => [ + 'https://v.qq.com/x/cover/mzc002001bk7moh/w41029n1rrv.html' + ], + 'X-Request-Time' => [ + '1.417 ms' + ] + ], + 'statusCode' => 200, + 'reasonPhrase' => 'OK', + 'body' => '腾讯视频
「ctrl+D」添加至网页收藏夹
历史
创作

上传优质视频

获得流量扶持

多种收益类型

助力作者变现

海量激励活动

提升内容收益

QQ浏览器
搜狗浏览器
ima
ima
ima
腾讯新闻
腾讯新闻
腾讯新闻
暗区突围:无限
三角洲行动
逆战:未来
王者:世界
视频极速下载  本地视频播放  桌面便捷访问
立即体验
查看更多
「ctrl+D」添加至网页收藏夹
历史
创作

上传优质视频

获得流量扶持

多种收益类型

助力作者变现

海量激励活动

提升内容收益

QQ浏览器
搜狗浏览器
ima
ima
ima
腾讯新闻
腾讯新闻
腾讯新闻
暗区突围:无限
三角洲行动
逆战:未来
王者:世界
视频极速下载  本地视频播放  桌面便捷访问
立即体验
查看更多
腾讯视频
' +]; diff --git a/tests/fixtures/drive.google.com.file-d-1sf_h2x3lulc-fv4fk9frcnnf60xkxuz2-view.php b/tests/fixtures/drive.google.com.file-d-1sf_h2x3lulc-fv4fk9frcnnf60xkxuz2-view.php new file mode 100644 index 00000000..0d28ef26 --- /dev/null +++ b/tests/fixtures/drive.google.com.file-d-1sf_h2x3lulc-fv4fk9frcnnf60xkxuz2-view.php @@ -0,0 +1,32 @@ + null, + 'authorUrl' => null, + 'cms' => null, + 'code' => [ + 'html' => '', + 'width' => 640, + 'height' => 480, + 'ratio' => 75.0 + ], + 'description' => null, + 'favicon' => 'https://ssl.gstatic.com/docs/doclist/images/drive_favicon_2026_32dp.png', + 'feeds' => [], + 'icon' => null, + 'image' => null, + 'keywords' => [], + 'language' => null, + 'languages' => [], + 'license' => null, + 'providerName' => 'Google Docs', + 'providerUrl' => 'https://drive.google.com', + 'publishedTime' => null, + 'redirect' => null, + 'title' => 'Sample.mp4', + 'url' => 'https://drive.google.com/file/d/1sF_h2x3lULc-fV4FK9FRCnnF60XkXuz2/view', + 'linkedData' => [], + 'oEmbed' => [], + 'allLinkedData' => [] +]; diff --git a/tests/fixtures/v.qq.com.x-cover-mzc002001bk7moh-w41029n1rrv.html.php b/tests/fixtures/v.qq.com.x-cover-mzc002001bk7moh-w41029n1rrv.html.php new file mode 100644 index 00000000..9e3628b9 --- /dev/null +++ b/tests/fixtures/v.qq.com.x-cover-mzc002001bk7moh-w41029n1rrv.html.php @@ -0,0 +1,32 @@ + null, + 'authorUrl' => null, + 'cms' => null, + 'code' => [ + 'html' => '', + 'width' => 640, + 'height' => 360, + 'ratio' => 56.25 + ], + 'description' => null, + 'favicon' => 'https://v.qq.com/favicon.ico', + 'feeds' => [], + 'icon' => 'https://vfiles.gtimg.cn/wuji_dashboard/xy/starter/4ea79867.png?max_age=7776000', + 'image' => null, + 'keywords' => [], + 'language' => 'zh-CN', + 'languages' => [], + 'license' => null, + 'providerName' => 'Qq', + 'providerUrl' => 'https://v.qq.com', + 'publishedTime' => null, + 'redirect' => null, + 'title' => '腾讯视频', + 'url' => 'https://v.qq.com/x/cover/mzc002001bk7moh/w41029n1rrv.html', + 'linkedData' => [], + 'oEmbed' => [], + 'allLinkedData' => [] +];