diff --git a/app/MetadataResolver/NijieResolver.php b/app/MetadataResolver/NijieResolver.php index 35ae370..f511092 100644 --- a/app/MetadataResolver/NijieResolver.php +++ b/app/MetadataResolver/NijieResolver.php @@ -3,6 +3,7 @@ namespace App\MetadataResolver; use GuzzleHttp\Client; +use Symfony\Component\DomCrawler\Crawler; class NijieResolver implements Resolver { @@ -30,23 +31,31 @@ class NijieResolver implements Resolver $url = preg_replace('~view_popup\.php~', 'view.php', $url); } - $client = $this->client; - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $metadata = $this->ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse((string) $res->getBody()); + $html = (string) $res->getBody(); + $crawler = new Crawler($html); - $dom = new \DOMDocument(); - @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); - $xpath = new \DOMXPath($dom); - $dataNode = $xpath->query('//script[substring(@type, string-length(@type) - 3, 4) = "json"]'); - foreach ($dataNode as $node) { - // 改行がそのまま入っていることがあるのでデコード前にエスケープが必要 - $imageData = json_decode(preg_replace('/\r?\n/', '\n', $node->nodeValue), true); - if (isset($imageData['thumbnailUrl']) && !ends_with($imageData['thumbnailUrl'], '.gif') && !ends_with($imageData['thumbnailUrl'], '.mp4')) { - $metadata->image = preg_replace('~nijie\\.info/.*/nijie_picture/~', 'nijie.info/nijie_picture/', $imageData['thumbnailUrl']); - break; - } + // DomCrawler内でjson内の日本語がHTMLエンティティに変換されるのでhtml_entity_decode + $json = html_entity_decode($crawler->filter('script[type="application/ld+json"]')->first()->text()); + + // 改行がそのまま入っていることがあるのでデコード前にエスケープが必要 + $data = json_decode(preg_replace('/\r?\n/', '\n', $json), true); + + $tags = $crawler->filter('#view-tag span.tag_name')->extract('_text'); + + $metadata->title = $data['name']; + $metadata->description = '投稿者: ' . $data['author']['name'] . PHP_EOL . $data['description']; + if ( + isset($data['thumbnailUrl']) && + !ends_with($data['thumbnailUrl'], '.gif') && + !ends_with($data['thumbnailUrl'], '.mp4') + ) { + // サムネイルからメイン画像に + $metadata->image = str_replace('__rs_l160x160/', '', $data['thumbnailUrl']); } + $metadata->tags = $tags; return $metadata; } else { diff --git a/tests/Unit/MetadataResolver/NijieResolverTest.php b/tests/Unit/MetadataResolver/NijieResolverTest.php index eaf50b0..a7bed13 100644 --- a/tests/Unit/MetadataResolver/NijieResolverTest.php +++ b/tests/Unit/MetadataResolver/NijieResolverTest.php @@ -25,10 +25,10 @@ class NijieResolverTest extends TestCase $this->createResolver(NijieResolver::class, $responseText); $metadata = $this->resolver->resolve('https://nijie.info/view.php?id=66384'); - $this->assertEquals('チンポップくんの日常ep.1「チンポップくんと釣り」 | ニジエ運営', $metadata->title); - $this->assertEquals("メールマガジン漫画のバックナンバー第一話です!\r\n最新話はメールマガジンより配信中です。", $metadata->description); - $this->assertRegExp('/pic\d+\.nijie\.info/', $metadata->image); - $this->assertNotRegExp('~/diff/main/~', $metadata->image); + $this->assertSame('チンポップくんの日常ep.1「チンポップくんと釣り」', $metadata->title); + $this->assertSame('投稿者: ニジエ運営' . PHP_EOL . 'メールマガジン漫画のバックナンバー第一話です!' . PHP_EOL . '最新話はメールマガジンより配信中です。', $metadata->description); + $this->assertSame('https://pic.nijie.net/04/nijie_picture/38_20131130155623.png', $metadata->image); + $this->assertSame(['ニジエたん', '釣り', 'チンポップ君の日常', '公式漫画'], $metadata->tags); if ($this->shouldUseMock()) { $this->assertSame('https://nijie.info/view.php?id=66384', (string) $this->handler->getLastRequest()->getUri()); } @@ -41,10 +41,10 @@ class NijieResolverTest extends TestCase $this->createResolver(NijieResolver::class, $responseText); $metadata = $this->resolver->resolve('https://nijie.info/view.php?id=202707'); - $this->assertEquals('ニジエ壁紙 | ニジエ運営', $metadata->title); - $this->assertEquals("ニジエのPCとiphone用(4.7inch推奨)の壁紙です。\r\n保存してご自由にお使いくださいませ。", $metadata->description); - $this->assertRegExp('/pic\d+\.nijie\.info/', $metadata->image); - $this->assertNotRegExp('~/diff/main/~', $metadata->image); + $this->assertSame('ニジエ壁紙', $metadata->title); + $this->assertSame('投稿者: ニジエ運営' . PHP_EOL . 'ニジエのPCとiphone用(4.7inch推奨)の壁紙です。' . PHP_EOL . '保存してご自由にお使いくださいませ。', $metadata->description); + $this->assertSame('https://pic.nijie.net/03/nijie_picture/38_20170209185801_0.png', $metadata->image); + $this->assertSame(['ニジエたん', '壁紙'], $metadata->tags); if ($this->shouldUseMock()) { $this->assertSame('https://nijie.info/view.php?id=202707', (string) $this->handler->getLastRequest()->getUri()); } @@ -57,9 +57,10 @@ class NijieResolverTest extends TestCase $this->createResolver(NijieResolver::class, $responseText); $metadata = $this->resolver->resolve('https://nijie.info/view.php?id=9537'); - $this->assertEquals('ニジエがgifに対応したんだってね 奥さん | 黒末アプコ', $metadata->title); - $this->assertEquals('アニメgifとか専門外なのでよくわかりませんでした', $metadata->description); - $this->assertRegExp('~/nijie\.info/pic/logo~', $metadata->image); + $this->assertSame('ニジエがgifに対応したんだってね 奥さん', $metadata->title); + $this->assertSame('投稿者: 黒末アプコ' . PHP_EOL . 'アニメgifとか専門外なのでよくわかりませんでした', $metadata->description); + $this->assertStringStartsWith('https://nijie.info/pic/logo/nijie_logo_og.png', $metadata->image); + $this->assertSame(['おっぱい', '陥没乳首', '眼鏡', 'GIFアニメ', 'ぶるんぶるん', 'アニメgif'], $metadata->tags); if ($this->shouldUseMock()) { $this->assertSame('https://nijie.info/view.php?id=9537', (string) $this->handler->getLastRequest()->getUri()); } @@ -72,74 +73,60 @@ class NijieResolverTest extends TestCase $this->createResolver(NijieResolver::class, $responseText); $metadata = $this->resolver->resolve('https://nijie.info/view.php?id=256283'); - $this->assertEquals('てすと | ニジエ運営', $metadata->title); - $this->assertEquals("H264動画てすと あとで消します\r\n\r\n今の所、H264コーデックのみ、出力時に音声なしにしないと投稿できません\r\n動画は勝手にループします", $metadata->description); - $this->assertRegExp('~/nijie\.info/pic/logo~', $metadata->image); + $this->assertEquals('てすと', $metadata->title); + $this->assertEquals('投稿者: ニジエ運営' . PHP_EOL . 'H264動画てすと あとで消します' . PHP_EOL . PHP_EOL . '今の所、H264コーデックのみ、出力時に音声なしにしないと投稿できません' . PHP_EOL . '動画は勝手にループします', $metadata->description); + $this->assertStringStartsWith('https://nijie.info/pic/logo/nijie_logo_og.png', $metadata->image); + $this->assertSame([], $metadata->tags); if ($this->shouldUseMock()) { $this->assertSame('https://nijie.info/view.php?id=256283', (string) $this->handler->getLastRequest()->getUri()); } } - public function testStandardPictureSp() + public function testViewPopup() + { + $responseText = file_get_contents(__DIR__ . '/../../fixture/Nijie/testStandardPictureResponse.html'); + + $this->createResolver(NijieResolver::class, $responseText); + + $metadata = $this->resolver->resolve('https://nijie.info/view_popup.php?id=66384'); + $this->assertSame('チンポップくんの日常ep.1「チンポップくんと釣り」', $metadata->title); + $this->assertSame('投稿者: ニジエ運営' . PHP_EOL . 'メールマガジン漫画のバックナンバー第一話です!' . PHP_EOL . '最新話はメールマガジンより配信中です。', $metadata->description); + $this->assertSame('https://pic.nijie.net/04/nijie_picture/38_20131130155623.png', $metadata->image); + $this->assertSame(['ニジエたん', '釣り', 'チンポップ君の日常', '公式漫画'], $metadata->tags); + if ($this->shouldUseMock()) { + $this->assertSame('https://nijie.info/view.php?id=66384', (string) $this->handler->getLastRequest()->getUri()); + } + } + + public function testSp() { $responseText = file_get_contents(__DIR__ . '/../../fixture/Nijie/testStandardPictureResponse.html'); $this->createResolver(NijieResolver::class, $responseText); $metadata = $this->resolver->resolve('https://sp.nijie.info/view.php?id=66384'); - $this->assertEquals('チンポップくんの日常ep.1「チンポップくんと釣り」 | ニジエ運営', $metadata->title); - $this->assertEquals("メールマガジン漫画のバックナンバー第一話です!\r\n最新話はメールマガジンより配信中です。", $metadata->description); - $this->assertRegExp('/pic\d+\.nijie\.info/', $metadata->image); - $this->assertNotRegExp('~/diff/main/~', $metadata->image); + $this->assertSame('チンポップくんの日常ep.1「チンポップくんと釣り」', $metadata->title); + $this->assertSame('投稿者: ニジエ運営' . PHP_EOL . 'メールマガジン漫画のバックナンバー第一話です!' . PHP_EOL . '最新話はメールマガジンより配信中です。', $metadata->description); + $this->assertSame('https://pic.nijie.net/04/nijie_picture/38_20131130155623.png', $metadata->image); + $this->assertSame(['ニジエたん', '釣り', 'チンポップ君の日常', '公式漫画'], $metadata->tags); if ($this->shouldUseMock()) { $this->assertSame('https://nijie.info/view.php?id=66384', (string) $this->handler->getLastRequest()->getUri()); } } - public function testMultiplePictureSp() + public function testSpViewPopup() { - $responseText = file_get_contents(__DIR__ . '/../../fixture/Nijie/testMultiplePictureResponse.html'); + $responseText = file_get_contents(__DIR__ . '/../../fixture/Nijie/testStandardPictureResponse.html'); $this->createResolver(NijieResolver::class, $responseText); - $metadata = $this->resolver->resolve('https://sp.nijie.info/view.php?id=202707'); - $this->assertEquals('ニジエ壁紙 | ニジエ運営', $metadata->title); - $this->assertEquals("ニジエのPCとiphone用(4.7inch推奨)の壁紙です。\r\n保存してご自由にお使いくださいませ。", $metadata->description); - $this->assertRegExp('/pic\d+\.nijie\.info/', $metadata->image); - $this->assertNotRegExp('~/diff/main/~', $metadata->image); + $metadata = $this->resolver->resolve('https://sp.nijie.info/view_popup.php?id=66384'); + $this->assertSame('チンポップくんの日常ep.1「チンポップくんと釣り」', $metadata->title); + $this->assertSame('投稿者: ニジエ運営' . PHP_EOL . 'メールマガジン漫画のバックナンバー第一話です!' . PHP_EOL . '最新話はメールマガジンより配信中です。', $metadata->description); + $this->assertSame('https://pic.nijie.net/04/nijie_picture/38_20131130155623.png', $metadata->image); + $this->assertSame(['ニジエたん', '釣り', 'チンポップ君の日常', '公式漫画'], $metadata->tags); if ($this->shouldUseMock()) { - $this->assertSame('https://nijie.info/view.php?id=202707', (string) $this->handler->getLastRequest()->getUri()); - } - } - - public function testAnimationGifSp() - { - $responseText = file_get_contents(__DIR__ . '/../../fixture/Nijie/testAnimationGifResponse.html'); - - $this->createResolver(NijieResolver::class, $responseText); - - - $metadata = $this->resolver->resolve('https://nijie.info/view.php?id=9537'); - $this->assertEquals('ニジエがgifに対応したんだってね 奥さん | 黒末アプコ', $metadata->title); - $this->assertEquals('アニメgifとか専門外なのでよくわかりませんでした', $metadata->description); - $this->assertRegExp('~/nijie\.info/pic/logo~', $metadata->image); - if ($this->shouldUseMock()) { - $this->assertSame('https://nijie.info/view.php?id=9537', (string) $this->handler->getLastRequest()->getUri()); - } - } - - public function testMp4MovieSp() - { - $responseText = file_get_contents(__DIR__ . '/../../fixture/Nijie/testMp4MovieResponse.html'); - - $this->createResolver(NijieResolver::class, $responseText); - - $metadata = $this->resolver->resolve('https://sp.nijie.info/view.php?id=256283'); - $this->assertEquals('てすと | ニジエ運営', $metadata->title); - $this->assertEquals("H264動画てすと あとで消します\r\n\r\n今の所、H264コーデックのみ、出力時に音声なしにしないと投稿できません\r\n動画は勝手にループします", $metadata->description); - $this->assertRegExp('~/nijie\.info/pic/logo~', $metadata->image); - if ($this->shouldUseMock()) { - $this->assertSame('https://nijie.info/view.php?id=256283', (string) $this->handler->getLastRequest()->getUri()); + $this->assertSame('https://nijie.info/view.php?id=66384', (string) $this->handler->getLastRequest()->getUri()); } } } diff --git a/tests/fixture/Nijie/testAnimationGifResponse.html b/tests/fixture/Nijie/testAnimationGifResponse.html index f618032..66eb16e 100644 --- a/tests/fixture/Nijie/testAnimationGifResponse.html +++ b/tests/fixture/Nijie/testAnimationGifResponse.html @@ -1,11 +1,78 @@ -ニジエがgifに対応したんだってね 奥さん | 黒末アプコ | ニジエ - - - - - - - - - - +ニジエがgifに対応したんだってね 奥さん | 黒末アプコ | ニジエ +

ニジエがgifに対応したんだってね 奥さん | 黒末アプコ

+
+ + +
+ +
+ diff --git a/tests/fixture/Nijie/testMp4MovieResponse.html b/tests/fixture/Nijie/testMp4MovieResponse.html index f80c7c1..fa63667 100644 --- a/tests/fixture/Nijie/testMp4MovieResponse.html +++ b/tests/fixture/Nijie/testMp4MovieResponse.html @@ -1,30 +1,108 @@ -てすと | ニジエ運営 | ニジエ - - - てすと | ニジエ運営 | ニジエ +

てすと | ニジエ運営

+ - - - -

 

good job

名無しのチンポップ

2018-04-10

- - - - +動画は勝手にループします","interactionCount": "3705 UserPlays, 6 UserComments","datePublished": "Sun Apr 1 23:07:03 2018","uploadDate": "Sun Apr 1 23:07:03 2018","dateModified": "Sun Apr 1 23:20:49 2018","copyrightYear": "2018","genre":"Image","contentLocation":"Japan","width":160,"height":90,"thumbnailUrl": "https://pic.nijie.net/01/__rs_l160x160/nijie_picture/38_20180401230701_0.mp4","author": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"creator": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"editor": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"copyrightHolder": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"}}
+ + +
+ +
+ diff --git a/tests/fixture/Nijie/testMultiplePictureResponse.html b/tests/fixture/Nijie/testMultiplePictureResponse.html index 70d7a8e..b0068fe 100644 --- a/tests/fixture/Nijie/testMultiplePictureResponse.html +++ b/tests/fixture/Nijie/testMultiplePictureResponse.html @@ -1,12 +1,83 @@ - - ニジエ壁紙 | ニジエ運営 | ニジエ - - +ニジエ壁紙 | ニジエ運営 | ニジエ +

ニジエ壁紙 | ニジエ運営

- - - +保存してご自由にお使いくださいませ。","interactionCount": "2613 UserPlays, 5 UserComments","datePublished": "Thu Feb 9 18:58:03 2017","uploadDate": "Thu Feb 9 18:58:03 2017","dateModified": "Thu Feb 9 18:58:03 2017","copyrightYear": "2017","genre":"Image","contentLocation":"Japan","width":160,"height":90,"thumbnailUrl": "https://pic.nijie.net/03/__rs_l160x160/nijie_picture/38_20170209185801_0.png","author": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"creator": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"editor": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"copyrightHolder": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"}}
+ + +
+ +
+ diff --git a/tests/fixture/Nijie/testStandardPictureResponse.html b/tests/fixture/Nijie/testStandardPictureResponse.html index 9d356db..b371920 100644 --- a/tests/fixture/Nijie/testStandardPictureResponse.html +++ b/tests/fixture/Nijie/testStandardPictureResponse.html @@ -1,16 +1,85 @@ -チンポップくんの日常ep.1「チンポップくんと釣り」 | ニジエ運営 | ニジエ - - - チンポップくんの日常ep.1「チンポップくんと釣り」 | ニジエ運営 | ニジエ +

チンポップくんの日常ep.1「チンポップくんと釣り」 | ニジエ運営

+ - - - - - - - - +最新話はメールマガジンより配信中です。","interactionCount": "13799 UserPlays, 24 UserComments","datePublished": "Sat Nov 30 15:56:26 2013","uploadDate": "Sat Nov 30 15:56:26 2013","dateModified": "Sat Nov 30 15:56:26 2013","copyrightYear": "2013","genre":"Image","contentLocation":"Japan","width":160,"height":90,"thumbnailUrl": "https://pic.nijie.net/04/__rs_l160x160/nijie_picture/38_20131130155623.png","author": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"creator": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"editor": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"},"copyrightHolder": {"@type": "Person","name": "ニジエ運営","description": "ニジエンジョイ!","sameAs": "https://nijie.info/members.php?id=38","image": "https://pic.nijie.net/04/__rs_cs150x150/members_picture/38_1511696698.png"}}
+ + +
+ +
+