From 2db45951f405a7de35e5e2191437927e2cc22ca3 Mon Sep 17 00:00:00 2001 From: eai04191 Date: Tue, 12 Mar 2019 03:30:38 +0900 Subject: [PATCH 1/3] =?UTF-8?q?FC2ContentsResolver=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/MetadataResolver/FC2ContentsResolver.php | 44 ++++++++++++++++++++ app/MetadataResolver/MetadataResolver.php | 1 + 2 files changed, 45 insertions(+) create mode 100644 app/MetadataResolver/FC2ContentsResolver.php diff --git a/app/MetadataResolver/FC2ContentsResolver.php b/app/MetadataResolver/FC2ContentsResolver.php new file mode 100644 index 0000000..76497ee --- /dev/null +++ b/app/MetadataResolver/FC2ContentsResolver.php @@ -0,0 +1,44 @@ +client = $client; + $this->ogpResolver = $ogpResolver; + } + + public function resolve(string $url): Metadata + { + $res = $this->client->get($url); + if ($res->getStatusCode() === 200) { + $metadata = $this->ogpResolver->parse($res->getBody()); + + $dom = new \DOMDocument(); + @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); + $xpath = new \DOMXPath($dom); + + $thumbnailNode = $xpath->query('//*[@class="main_thum_img"]/a')->item(0); + if ($thumbnailNode) { + $metadata->image = preg_replace('~^http:~', "https:", $thumbnailNode->getAttribute('href')); + } + + return $metadata; + } else { + throw new \RuntimeException("{$res->getStatusCode()}: $url"); + } + } +} diff --git a/app/MetadataResolver/MetadataResolver.php b/app/MetadataResolver/MetadataResolver.php index 1a8c6fe..181aab3 100644 --- a/app/MetadataResolver/MetadataResolver.php +++ b/app/MetadataResolver/MetadataResolver.php @@ -25,6 +25,7 @@ class MetadataResolver implements Resolver '~\.syosetu\.com/n\d+[a-z]{2,}~' => NarouResolver::class, '~ci-en\.jp/creator/\d+/article/\d+~' => CienResolver::class, '~www\.plurk\.com\/p\/.*~' => PlurkResolver::class, + '~(adult\.)?contents\.fc2\.com\/article_search\.php\?id=\d+~' => FC2ContentsResolver::class, ]; public $mimeTypes = [ From ec5a78db38e9b89f874f2d8573e3c992f1746223 Mon Sep 17 00:00:00 2001 From: eai04191 Date: Tue, 12 Mar 2019 03:31:16 +0900 Subject: [PATCH 2/3] =?UTF-8?q?FC2ContentsResolver=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FC2ContentsResolverTest.php | 50 +++++++++++++++++++ tests/fixture/FC2Contents/adult.html | 15 ++++++ tests/fixture/FC2Contents/general.html | 15 ++++++ 3 files changed, 80 insertions(+) create mode 100644 tests/Unit/MetadataResolver/FC2ContentsResolverTest.php create mode 100644 tests/fixture/FC2Contents/adult.html create mode 100644 tests/fixture/FC2Contents/general.html diff --git a/tests/Unit/MetadataResolver/FC2ContentsResolverTest.php b/tests/Unit/MetadataResolver/FC2ContentsResolverTest.php new file mode 100644 index 0000000..2cce224 --- /dev/null +++ b/tests/Unit/MetadataResolver/FC2ContentsResolverTest.php @@ -0,0 +1,50 @@ +shouldUseMock()) { + sleep(1); + } + } + + public function testAdult() + { + $responseText = file_get_contents(__DIR__.'/../../fixture/FC2Contents/adult.html'); + + $this->createResolver(FC2ContentsResolver::class, $responseText); + + $metadata = $this->resolver->resolve('https://adult.contents.fc2.com/article_search.php?id=401545'); + $this->assertEquals('個人撮影@「ぱいずりオアトリート♡」Jカップ魔女っ子の3連挟射しても続けちゃうパイズリ!', $metadata->title); + $this->assertEquals('個人撮影@「ぱいずりオアトリート♡」Jカップ魔女っ子の3連挟射しても続けちゃうパイズリ! - イベントコスチュームということもあり、大ボリュームだった前回、前々回の パイズリ役Jcupメイド と ナースパイズリを超え 今回さらに超ボリューム&超密度の内容になってます! -------- …', $metadata->description); + $this->assertEquals('https://storage2000.contents.fc2.com/file/104/10362633/1477676255.72.png', $metadata->image); + if ($this->shouldUseMock()) { + $this->assertSame('https://adult.contents.fc2.com/article_search.php?id=401545', (string) $this->handler->getLastRequest()->getUri()); + } + } + + public function testGeneral() + { + $responseText = file_get_contents(__DIR__.'/../../fixture/FC2Contents/general.html'); + + $this->createResolver(FC2ContentsResolver::class, $responseText); + + $metadata = $this->resolver->resolve('https://contents.fc2.com/article_search.php?id=336610'); + $this->assertEquals('ゆかいなどうぶつたち ~オオカミ・キツネ・タヌキ~', $metadata->title); + $this->assertEquals('ゆかいなどうぶつたち ~オオカミ・キツネ・タヌキ~ - 今回のおともだちは、オオカミ・キツネ・タヌキだよ。地球上に住んでいるたくさんのおともだち、みんなにどんどん紹介するからたのしみにしてね!', $metadata->description); + $this->assertEquals('https://storage6000.contents.fc2.com/file/300/29917555/1519118184.65.jpg', $metadata->image); + if ($this->shouldUseMock()) { + $this->assertSame('https://contents.fc2.com/article_search.php?id=336610', (string) $this->handler->getLastRequest()->getUri()); + } + } +} diff --git a/tests/fixture/FC2Contents/adult.html b/tests/fixture/FC2Contents/adult.html new file mode 100644 index 0000000..781163f --- /dev/null +++ b/tests/fixture/FC2Contents/adult.html @@ -0,0 +1,15 @@ + + + + 個人撮影@「ぱいずりオアトリート♡」Jカップ魔女っ子の3連挟射しても続けちゃうパイズリ! + + + + +
+ + + +
+ + \ No newline at end of file diff --git a/tests/fixture/FC2Contents/general.html b/tests/fixture/FC2Contents/general.html new file mode 100644 index 0000000..8510488 --- /dev/null +++ b/tests/fixture/FC2Contents/general.html @@ -0,0 +1,15 @@ + + + + ゆかいなどうぶつたち ~オオカミ・キツネ・タヌキ~ + + + + +
+ + + +
+ + From 3fa2d8050715285f4c65bdcb3153f99df057a585 Mon Sep 17 00:00:00 2001 From: eai04191 Date: Tue, 12 Mar 2019 03:38:05 +0900 Subject: [PATCH 3/3] Fix style --- app/MetadataResolver/FC2ContentsResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/MetadataResolver/FC2ContentsResolver.php b/app/MetadataResolver/FC2ContentsResolver.php index 76497ee..36a3b8b 100644 --- a/app/MetadataResolver/FC2ContentsResolver.php +++ b/app/MetadataResolver/FC2ContentsResolver.php @@ -33,7 +33,7 @@ class FC2ContentsResolver implements Resolver $thumbnailNode = $xpath->query('//*[@class="main_thum_img"]/a')->item(0); if ($thumbnailNode) { - $metadata->image = preg_replace('~^http:~', "https:", $thumbnailNode->getAttribute('href')); + $metadata->image = preg_replace('~^http:~', 'https:', $thumbnailNode->getAttribute('href')); } return $metadata;