-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from hyvor/giphy-tenor
tenor and giphy with oembed
- Loading branch information
Showing
6 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Hyvor\Unfold\Embed\Platforms; | ||
|
||
use Hyvor\Unfold\Embed\EmbedParserAbstract; | ||
use Hyvor\Unfold\Embed\EmbedParserOEmbedInterface; | ||
|
||
class Giphy extends EmbedParserAbstract implements EmbedParserOEmbedInterface | ||
{ | ||
public function regex() | ||
{ | ||
return [ | ||
/** | ||
* /gifs/ oembed returns a photo response | ||
* We return a simple <img> tag | ||
*/ | ||
"https://giphy.com/gifs/.*", | ||
|
||
/** | ||
* 2024-11-14: Clips oEmbed returns an iframe | ||
* but the iframe doesn't work due to X-Frame-Options: DENY | ||
*/ | ||
// "https://giphy.com/clips/.*", | ||
|
||
// 2024-11-14: gph.is links don't work with oembed | ||
// "http://gph.is/.*", | ||
|
||
"https://media\d?.giphy.com/media/.*/giphy.(gif|webp)", | ||
]; | ||
} | ||
|
||
public function oEmbedUrl(): string | ||
{ | ||
return 'https://giphy.com/services/oembed'; | ||
} | ||
|
||
protected function htmlFromOEmbedArray(array $data): ?string | ||
{ | ||
$type = $data['type'] ?? null; | ||
|
||
/** | ||
* giphy.com/gifs/* return a photo oembed | ||
* so, we simply generate an <img> tag | ||
*/ | ||
if ($type === 'photo') { | ||
/** @var ?string $url */ | ||
$url = $data['url'] ?? null; | ||
|
||
if (!$url) { | ||
return null; | ||
} | ||
|
||
/** @var ?string $title */ | ||
$title = $data['title'] ?? ''; | ||
|
||
$width = $data['width'] ?? null; | ||
// $height = $data['height'] ?? null; | ||
|
||
$attrs = [ | ||
'src' => $url, | ||
'alt' => strval($title), | ||
'width' => is_int($width) ? min($width, 600) : null, | ||
//'height' => is_int($height) ? min($height, 600) : null, | ||
'style' => 'max-width: 100%;' | ||
]; | ||
$attrs = array_filter($attrs); | ||
$attrs = array_map(fn($key, $value) => "$key=\"$value\"", array_keys($attrs), $attrs); | ||
|
||
return "<img " . implode(' ', $attrs) . ">"; | ||
} | ||
|
||
/** @var ?string $html */ | ||
$html = $data['html'] ?? null; | ||
|
||
return $html; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Hyvor\Unfold\Embed\Platforms; | ||
|
||
use Hyvor\Unfold\Embed\EmbedParserAbstract; | ||
use Hyvor\Unfold\Embed\EmbedParserOEmbedInterface; | ||
|
||
class Tenor extends EmbedParserAbstract implements EmbedParserOEmbedInterface | ||
{ | ||
public function regex() | ||
{ | ||
return [ | ||
'https://tenor.com/view/(.*)', | ||
]; | ||
} | ||
|
||
public function oEmbedUrl(): string | ||
{ | ||
return 'https://tenor.com/oembed'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Unit\Parsers; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Middleware; | ||
use GuzzleHttp\Psr7\Response; | ||
use Hyvor\Unfold\Embed\Platforms\Giphy; | ||
use Hyvor\Unfold\UnfoldConfig; | ||
|
||
it('matches tenor URLs', function (string $url) { | ||
$parser = new Giphy(UnfoldConfig::withUrl($url)); | ||
$match = $parser->match(); | ||
expect($match)->toBeArray(); | ||
})->with([ | ||
'https://giphy.com/gifs/studiosoriginals-kindness-world-day-IwADz4opoIZthLv6Mz', | ||
'https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExaGZhOGZwaTNmaHdtaGdrYjh0b3NtM2hsaXg5M2VsM250bWN4ZzZyciZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/IwADz4opoIZthLv6Mz/giphy.webp', | ||
]); | ||
|
||
|
||
it('fetches giphy', function () { | ||
$history = []; | ||
$historyMiddleware = Middleware::history($history); | ||
|
||
$mock = new MockHandler([ | ||
new Response(200, [], json_encode([ | ||
'html' => '<giphy></giphy>', | ||
])) | ||
]); | ||
|
||
$handlerStack = new HandlerStack($mock); | ||
$handlerStack->push($historyMiddleware); | ||
|
||
$client = new Client(['handler' => $handlerStack]); | ||
|
||
$url = 'https://giphy.com/gifs/studiosoriginals-kindness-world-day-IwADz4opoIZthLv6Mz'; | ||
$config = new UnfoldConfig(httpClient: $client); | ||
$config->start($url); | ||
$parser = new Giphy($config); | ||
$response = $parser->parse(); | ||
expect($response)->toBe('<giphy></giphy>'); | ||
|
||
$request = $history[0]['request']; | ||
|
||
expect((string)$request->getUri())->toBe( | ||
'https://giphy.com/services/oembed?url=' . $url . '&format=json' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Unit\Parsers; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Middleware; | ||
use GuzzleHttp\Psr7\Response; | ||
use Hyvor\Unfold\Embed\Platforms\Tenor; | ||
use Hyvor\Unfold\UnfoldConfig; | ||
|
||
it('matches tenor URLs', function (string $url) { | ||
$parser = new Tenor(UnfoldConfig::withUrl($url)); | ||
$match = $parser->match(); | ||
expect($match)->toBeArray(); | ||
})->with([ | ||
'https://tenor.com/view/shannon-sharpe-shannon-sharpe-drinking-fox-undisputed-skip-bayless-gif-23482018', | ||
'https://tenor.com/view/steph-curry-stephen-curry-chef-curry-curry-warriors-gif-2893813801205262964' | ||
]); | ||
|
||
it('fetches tenor', function () { | ||
$history = []; | ||
$historyMiddleware = Middleware::history($history); | ||
|
||
$mock = new MockHandler([ | ||
new Response(200, [], json_encode([ | ||
'html' => '<tenor></tenor>', | ||
])) | ||
]); | ||
|
||
$handlerStack = new HandlerStack($mock); | ||
$handlerStack->push($historyMiddleware); | ||
$client = new Client(['handler' => $handlerStack]); | ||
|
||
$url = 'https://tenor.com/view/shannon-sharpe-shannon-sharpe-drinking-fox-undisputed-skip-bayless-gif-23482018'; | ||
$config = new UnfoldConfig(httpClient: $client); | ||
$config->start($url); | ||
$parser = new Tenor($config); | ||
$response = $parser->parse(); | ||
expect($response)->toBe('<tenor></tenor>'); | ||
|
||
$request = $history[0]['request']; | ||
|
||
expect((string)$request->getUri())->toBe( | ||
'https://tenor.com/oembed?url=' . $url . '&format=json' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters