oEmbed APIを使用するように変更

This commit is contained in:
eai04191 2019-08-07 19:22:54 +09:00
parent f7c9e83b12
commit bc2f8662fc
4 changed files with 63 additions and 17 deletions

View File

@ -23,31 +23,25 @@ class DeviantArtResolver implements Resolver
public function resolve(string $url): Metadata
{
$res = $this->client->get($url);
$res = $this->client->get('https://backend.deviantart.com/oembed?url=' . $url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody());
$data = json_decode($res->getBody()->getContents(), true);
$metadata = new Metadata();
$dom = new \DOMDocument();
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
$xpath = new \DOMXPath($dom);
$node = $xpath->query('//*[@id="pimp-preload"]/following-sibling::div//img')->item(0);
$srcset = $node->getAttribute('srcset');
$srcset_array = explode('w,', $srcset);
$src = end($srcset_array);
$src = preg_replace('~ \d+w$~', '', $src);
if (preg_match('~\.wixmp\.com$~', parse_url($src)['host'])) {
if (preg_match('~\.wixmp\.com$~', parse_url($data['url'])['host'])) {
// アスペクト比を保ったまま、縦か横が最大700pxになるように変換する。
// Ref: https://support.wixmp.com/en/article/image-service-3835799
if (strpos($src, '/v1/fill/')) {
$src = preg_replace('~/v1/fill/w_\d+,h_\d+,q_\d+,strp~', '/v1/fit/w_700,h_700,q_70,strp', $src);
if (strpos($data['url'], '/v1/fill/')) {
$metadata->image = preg_replace('~/v1/fill/w_\d+,h_\d+,q_\d+,strp~', '/v1/fit/w_700,h_700,q_70,strp', $data['url']);
} else {
$src = $src . '/v1/fit/w_700,h_700,q_70,strp/image.jpg';
$metadata->image = $data['url'] . '/v1/fit/w_700,h_700,q_70,strp/image.jpg';
}
} else {
$metadata->image = $data['url'];
}
$metadata->image = $src;
$metadata->title = $data['title'] ?? '';
$metadata->description = 'By ' . $data['author_name'];
return $metadata;
} else {

View File

@ -0,0 +1,50 @@
<?php
namespace Tests\Unit\MetadataResolver;
use App\MetadataResolver\DeviantArtResolver;
use Tests\TestCase;
class DeviantArtResolverTest extends TestCase
{
use CreateMockedResolver;
public function setUp()
{
parent::setUp();
if (!$this->shouldUseMock()) {
sleep(1);
}
}
public function testWixmp()
{
$responseText = file_get_contents(__DIR__ . '/../../fixture/DeviantArt/wixmp.json');
$this->createResolver(DeviantArtResolver::class, $responseText);
$metadata = $this->resolver->resolve('https://www.deviantart.com/bonchilo/art/Sally-Nox-743562408');
$this->assertSame('Sally Nox', $metadata->title);
$this->assertSame('By Bonchilo', $metadata->description);
$this->assertSame('https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/f6b84a8f-053e-4ab6-bd6c-71276a4a9282/dcap4fc-6fd6359c-770b-4515-9e29-e99311d58d57.png/v1/fit/w_700,h_700,q_70,strp/sally__nox_by_bonchilo_dcap4fc-pre.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9MTE0MCIsInBhdGgiOiJcL2ZcL2Y2Yjg0YThmLTA1M2UtNGFiNi1iZDZjLTcxMjc2YTRhOTI4MlwvZGNhcDRmYy02ZmQ2MzU5Yy03NzBiLTQ1MTUtOWUyOS1lOTkzMTFkNThkNTcucG5nIiwid2lkdGgiOiI8PTEyODAifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6aW1hZ2Uub3BlcmF0aW9ucyJdfQ.zbw4e5eH0NafyMmhM15DKN1NjawSZBUwr2RWQWB7O3o', $metadata->image);
if ($this->shouldUseMock()) {
$this->assertSame('https://backend.deviantart.com/oembed?url=https://www.deviantart.com/bonchilo/art/Sally-Nox-743562408', (string) $this->handler->getLastRequest()->getUri());
}
}
public function testMature()
{
$responseText = file_get_contents(__DIR__ . '/../../fixture/DeviantArt/mature.json');
$this->createResolver(DeviantArtResolver::class, $responseText);
$metadata = $this->resolver->resolve('https://www.deviantart.com/rasbii/art/backstage-620617246');
$this->assertSame('backstage', $metadata->title);
$this->assertSame('By Rasbii', $metadata->description);
$this->assertSame('https://orig00.deviantart.net/eb50/f/2016/191/a/b/preview_by_rasbii-da9hzby.png', $metadata->image);
if ($this->shouldUseMock()) {
$this->assertSame('https://backend.deviantart.com/oembed?url=https://www.deviantart.com/rasbii/art/backstage-620617246', (string) $this->handler->getLastRequest()->getUri());
}
}
}

1
tests/fixture/DeviantArt/mature.json vendored Normal file
View File

@ -0,0 +1 @@
{"version":"1.0","type":"photo","title":"backstage","category":"Anthro > Digital Media > Drawings > Animals","url":"https:\/\/orig00.deviantart.net\/eb50\/f\/2016\/191\/a\/b\/preview_by_rasbii-da9hzby.png","author_name":"Rasbii","author_url":"https:\/\/www.deviantart.com\/rasbii","provider_name":"DeviantArt","provider_url":"https:\/\/www.deviantart.com","safety":"adult","pubdate":"2016-07-09T13:12:42-07:00","community":{"statistics":{"_attributes":{"views":6678,"favorites":127,"comments":39,"downloads":0}}},"rating":"adult","license":{"_attributes":{"type":"text\/html","href":"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/"},"0":"Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License"},"width":"689","height":"619","imagetype":"png","thumbnail_url":"https:\/\/t00.deviantart.net\/eJ1IFDUqQxCrbw2pGNP2ZYtyqk4=\/fit-in\/300x900\/filters:no_upscale():origin()\/pre00\/e504\/th\/pre\/f\/2016\/191\/a\/b\/preview_by_rasbii-da9hzby.png","thumbnail_width":300,"thumbnail_height":270,"thumbnail_url_150":"https:\/\/t00.deviantart.net\/EUshcQ7hG73H6zzfsrnH8xJl5dQ=\/fit-in\/150x150\/filters:no_upscale():origin()\/pre00\/e504\/th\/pre\/f\/2016\/191\/a\/b\/preview_by_rasbii-da9hzby.png","thumbnail_url_200h":"https:\/\/t00.deviantart.net\/ijA0nHI6NA42Ocm0Bfd1rhrDHh0=\/300x200\/filters:fixed_height(100,100):origin()\/pre00\/e504\/th\/pre\/f\/2016\/191\/a\/b\/preview_by_rasbii-da9hzby.png","thumbnail_width_200h":223,"thumbnail_height_200h":200}

1
tests/fixture/DeviantArt/wixmp.json vendored Normal file
View File

@ -0,0 +1 @@
{"version":"1.0","type":"photo","title":"Sally Nox","category":"Manga & Anime > Digital Media > Drawings","url":"https:\/\/images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com\/f\/f6b84a8f-053e-4ab6-bd6c-71276a4a9282\/dcap4fc-6fd6359c-770b-4515-9e29-e99311d58d57.png\/v1\/fill\/w_947,h_844,q_70,strp\/sally__nox_by_bonchilo_dcap4fc-pre.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9MTE0MCIsInBhdGgiOiJcL2ZcL2Y2Yjg0YThmLTA1M2UtNGFiNi1iZDZjLTcxMjc2YTRhOTI4MlwvZGNhcDRmYy02ZmQ2MzU5Yy03NzBiLTQ1MTUtOWUyOS1lOTkzMTFkNThkNTcucG5nIiwid2lkdGgiOiI8PTEyODAifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6aW1hZ2Uub3BlcmF0aW9ucyJdfQ.zbw4e5eH0NafyMmhM15DKN1NjawSZBUwr2RWQWB7O3o","author_name":"Bonchilo","author_url":"https:\/\/www.deviantart.com\/bonchilo","provider_name":"DeviantArt","provider_url":"https:\/\/www.deviantart.com","safety":"nonadult","pubdate":"2018-05-05T07:18:54-07:00","community":{"statistics":{"_attributes":{"views":243,"favorites":73,"comments":7,"downloads":7}}},"copyright":{"_attributes":{"url":"https:\/\/www.deviantart.com\/bonchilo","year":"2018","entity":"Bonchilo"}},"width":947,"height":844,"imagetype":"","thumbnail_url":"https:\/\/images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com\/f\/f6b84a8f-053e-4ab6-bd6c-71276a4a9282\/dcap4fc-6fd6359c-770b-4515-9e29-e99311d58d57.png\/v1\/fit\/w_300,h_900,q_70,strp\/sally__nox_by_bonchilo_dcap4fc-300w.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9MTE0MCIsInBhdGgiOiJcL2ZcL2Y2Yjg0YThmLTA1M2UtNGFiNi1iZDZjLTcxMjc2YTRhOTI4MlwvZGNhcDRmYy02ZmQ2MzU5Yy03NzBiLTQ1MTUtOWUyOS1lOTkzMTFkNThkNTcucG5nIiwid2lkdGgiOiI8PTEyODAifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6aW1hZ2Uub3BlcmF0aW9ucyJdfQ.zbw4e5eH0NafyMmhM15DKN1NjawSZBUwr2RWQWB7O3o","thumbnail_width":300,"thumbnail_height":267,"thumbnail_url_150":"https:\/\/images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com\/f\/f6b84a8f-053e-4ab6-bd6c-71276a4a9282\/dcap4fc-6fd6359c-770b-4515-9e29-e99311d58d57.png\/v1\/fit\/w_150,h_150,q_70,strp\/sally__nox_by_bonchilo_dcap4fc-150.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9MTE0MCIsInBhdGgiOiJcL2ZcL2Y2Yjg0YThmLTA1M2UtNGFiNi1iZDZjLTcxMjc2YTRhOTI4MlwvZGNhcDRmYy02ZmQ2MzU5Yy03NzBiLTQ1MTUtOWUyOS1lOTkzMTFkNThkNTcucG5nIiwid2lkdGgiOiI8PTEyODAifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6aW1hZ2Uub3BlcmF0aW9ucyJdfQ.zbw4e5eH0NafyMmhM15DKN1NjawSZBUwr2RWQWB7O3o","thumbnail_url_200h":"https:\/\/images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com\/f\/f6b84a8f-053e-4ab6-bd6c-71276a4a9282\/dcap4fc-6fd6359c-770b-4515-9e29-e99311d58d57.png\/v1\/fill\/w_225,h_200,q_70,strp\/sally__nox_by_bonchilo_dcap4fc-200h.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9MTE0MCIsInBhdGgiOiJcL2ZcL2Y2Yjg0YThmLTA1M2UtNGFiNi1iZDZjLTcxMjc2YTRhOTI4MlwvZGNhcDRmYy02ZmQ2MzU5Yy03NzBiLTQ1MTUtOWUyOS1lOTkzMTFkNThkNTcucG5nIiwid2lkdGgiOiI8PTEyODAifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6aW1hZ2Uub3BlcmF0aW9ucyJdfQ.zbw4e5eH0NafyMmhM15DKN1NjawSZBUwr2RWQWB7O3o","thumbnail_width_200h":225,"thumbnail_height_200h":200}