Merge pull request #149 from eai04191/feature/resolver-fc2contents

FC2ContentsResolverを追加
This commit is contained in:
shibafu 2019-03-13 22:57:45 +09:00 committed by GitHub
commit 2dd5cbd072
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace App\MetadataResolver;
use GuzzleHttp\Client;
class FC2ContentsResolver implements Resolver
{
/**
* @var Client
*/
private $client;
/**
* @var OGPResolver
*/
private $ogpResolver;
public function __construct(Client $client, OGPResolver $ogpResolver)
{
$this->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");
}
}
}

View File

@ -25,6 +25,7 @@ class MetadataResolver implements Resolver
'~\.syosetu\.com/n\d+[a-z]{2,}~' => NarouResolver::class, '~\.syosetu\.com/n\d+[a-z]{2,}~' => NarouResolver::class,
'~ci-en\.jp/creator/\d+/article/\d+~' => CienResolver::class, '~ci-en\.jp/creator/\d+/article/\d+~' => CienResolver::class,
'~www\.plurk\.com\/p\/.*~' => PlurkResolver::class, '~www\.plurk\.com\/p\/.*~' => PlurkResolver::class,
'~(adult\.)?contents\.fc2\.com\/article_search\.php\?id=\d+~' => FC2ContentsResolver::class,
]; ];
public $mimeTypes = [ public $mimeTypes = [

View File

@ -0,0 +1,50 @@
<?php
namespace Tests\Unit\MetadataResolver;
use App\MetadataResolver\FC2ContentsResolver;
use Tests\TestCase;
class FC2ContentsResolverTest extends TestCase
{
use CreateMockedResolver;
public function setUp()
{
parent::setUp();
if (!$this->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カップ魔女っ子の連挟射しても続けちゃうパイズリ', $metadata->title);
$this->assertEquals('個人撮影「ぱいずりオアトリート♡」Jカップ魔女っ子の連挟射しても続けちゃうパイズリ - イベントコスチュームということもあり、大ボリュームだった前回、前々回の パイズリ役cupメイド と ナースパイズリを超え 今回さらに超ボリューム&超密度の内容になってます! -------- …', $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());
}
}
}

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<title>個人撮影「ぱいずりオアトリート♡」Jカップ魔女っ子の連挟射しても続けちゃうパイズリ</title>
<meta charset="UTF-8">
<meta name="description" content="個人撮影「ぱいずりオアトリート♡」Jカップ魔女っ子の連挟射しても続けちゃうパイズリ - イベントコスチュームということもあり、大ボリュームだった前回、前々回の パイズリ役cupメイド と ナースパイズリを超え 今回さらに超ボリューム&超密度の内容になってます! -------- …">
</head>
<body>
<div class="main_thum_img">
<a class="analyticsLinkClick_mainThum" href="http://storage2000.contents.fc2.com/file/104/10362633/1477676255.72.png">
<img src="//contents-thumbnail2.fc2.com/w276/storage2000.contents.fc2.com/file/104/10362633/1477676255.72.png">
</a>
</div>
</body>
</html>

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<title>ゆかいなどうぶつたち ~オオカミ・キツネ・タヌキ~</title>
<meta charset="UTF-8" />
<meta name="description" content="ゆかいなどうぶつたち ~オオカミ・キツネ・タヌキ~ - 今回のおともだちは、オオカミ・キツネ・タヌキだよ。地球上に住んでいるたくさんのおともだち、みんなにどんどん紹介するからたのしみにしてね!"/>
</head>
<body>
<div class="main_thum_img">
<a class="analyticsLinkClick_mainThum" href="http://storage6000.contents.fc2.com/file/300/29917555/1519118184.65.jpg">
<img src="//contents-thumbnail2.fc2.com/w276/storage6000.contents.fc2.com/file/300/29917555/1519118184.65.jpg"/>
</a>
</div>
</body>
</html>