投稿に使える画像があれば使う

This commit is contained in:
Irie Aoi 2020-12-03 17:19:14 +09:00
parent 6449094b78
commit c616800da1
3 changed files with 1128 additions and 12 deletions

View File

@ -4,6 +4,7 @@ namespace App\MetadataResolver;
use Carbon\Carbon; use Carbon\Carbon;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class CienResolver extends MetadataResolver class CienResolver extends MetadataResolver
{ {
@ -25,7 +26,15 @@ class CienResolver extends MetadataResolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
$metadata = $this->ogpResolver->parse((string) $res->getBody()); $html = (string) $res->getBody();
$metadata = $this->ogpResolver->parse($html);
$crawler = new Crawler($html);
// OGPのデフォルトはバナーなので、投稿に使える画像があればそれを使う
$selector = 'img[data-actual*="image-web"]';
if ($crawler->filter($selector)->count() !== 0) {
$metadata->image = $crawler->filter($selector)->attr('data-actual');
}
// JWTがついていれば画像URLのJWTから有効期限を拾う // JWTがついていれば画像URLのJWTから有効期限を拾う
parse_str(parse_url($metadata->image, PHP_URL_QUERY), $params); parse_str(parse_url($metadata->image, PHP_URL_QUERY), $params);

View File

@ -25,24 +25,29 @@ class CienResolverTest extends TestCase
$this->createResolver(CienResolver::class, $responseText); $this->createResolver(CienResolver::class, $responseText);
$metadata = $this->resolver->resolve('https://ci-en.dlsite.com/creator/2462/article/87502'); $metadata = $this->resolver->resolve('https://ci-en.dlsite.com/creator/2462/article/87502');
$this->assertSame('進捗とボツ立ち絵 - ねんない5 - Ci-en(シエン)', $metadata->title); $this->assertSame('進捗とボツ立ち絵 - ねんない5 - Ci-en', $metadata->title);
$this->assertSame('今日のサムネイルはストアページに掲載する予定のキャラクター紹介画像です。 ドットでない解像度の高いイラストは時間も体力も精神力もかかるので、こういうのを行うタスクを開発終盤に残さないでよかったと本気……', $metadata->description); $this->assertSame('ドット製D ACTを製作しています。' . PHP_EOL . '恐ろしい存在に襲われる絶望感や、被虐的な官能がテーマです。', $metadata->description);
$this->assertStringStartsWith('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-800.jpg?jwt=', $metadata->image); $this->assertStringStartsWith('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-web.jpg?jwt=', $metadata->image);
if ($this->shouldUseMock()) { if ($this->shouldUseMock()) {
$this->assertSame('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-800.jpg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkiOiJqd3RhdXRoX3NlY18yMDIwT2N0IiwiaXNzIjoiaHR0cHM6XC9cL2NpLWVuLmRsc2l0ZS5jb21cLyIsInN1YiI6IjAwMDAwMDAwMDAwIiwiYXVkIjoiYTdhZmQzYjAyYTZkMWNhYTZhZmU2YTNiZjU1NTBmYjZhNDJhZWZiYTY4NmYxN2EwYTJmNjNjOTdmZDY4NjdhYiIsImV4cCI6MTYwMjk5NTIyMX0.bXUG2T6nXl4hdvsvt1wkIMvbbBdsKk-xbwB6SaxARZA', $metadata->image); $this->assertSame('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-web.jpg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkiOiJqd3RhdXRoX3NlY18yMDIwT2N0IiwiaXNzIjoiaHR0cHM6XC9cL2NpLWVuLmRsc2l0ZS5jb21cLyIsInN1YiI6IjAwMDAwMDAwMDAwIiwiYXVkIjoiYTdhZmQzYjAyYTZkMWNhYTZhZmU2YTNiZjU1NTBmYjZhNDJhZWZiYTY4NmYxN2EwYTJmNjNjOTdmZDY4NjdhYiIsImV4cCI6MTYwNzA2NzMyOX0.-462_WtZ6AUOxrfndBE-0_oWHKwesP9mMMn6K2oYQJM', $metadata->image);
$this->assertSame(1602995221, $metadata->expires_at->timestamp); $this->assertSame(1607067329, $metadata->expires_at->timestamp);
$this->assertSame('https://ci-en.dlsite.com/creator/2462/article/87502', (string) $this->handler->getLastRequest()->getUri()); $this->assertSame('https://ci-en.dlsite.com/creator/2462/article/87502', (string) $this->handler->getLastRequest()->getUri());
} }
} }
public function testWithNoTimestamp() public function testWithNoPostImage()
{ {
$responseText = $this->fetchSnapshot(__DIR__ . '/../../fixture/Cien/testWithNoTimestamp.html'); $responseText = $this->fetchSnapshot(__DIR__ . '/../../fixture/Cien/testWithNoPostImage.html');
$this->createResolver(CienResolver::class, $responseText); $this->createResolver(CienResolver::class, $responseText);
$this->expectException(\RuntimeException::class); $metadata = $this->resolver->resolve('https://ci-en.dlsite.com/creator/148/article/401866');
$this->expectExceptionMessage('Parameter "jwt" not found. Image=https://ci-en.dlsite.com/assets/img/common/logo_Ci-en_R18.svg Source=https://ci-en.dlsite.com/'); $this->assertSame('近況報告 - 薄稀 - Ci-en', $metadata->title);
$this->assertSame('サキュバスをはじめ、M向けの魔物娘をよく描くエロ絵描きです(´ω`)', $metadata->description);
$this->resolver->resolve('https://ci-en.dlsite.com/'); $this->assertSame('https://media.ci-en.jp/public/cover/creator/00000148/9153a13f78591bc2c9efae1021a26f9b90d24d3b30a0b3e699d0050f09dab6df/image-990-c.jpg', $metadata->image);
if ($this->shouldUseMock()) {
$this->assertNull($metadata->expires_at);
$this->assertSame('https://ci-en.dlsite.com/creator/148/article/401866', (string) $this->handler->getLastRequest()->getUri());
}
} }
} }

File diff suppressed because one or more lines are too long