2019-02-03 01:10:09 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
2019-02-03 01:10:09 +09:00
|
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
|
|
|
|
|
|
class NarouResolver implements Resolver
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
/**
|
|
|
|
* @var OGPResolver
|
|
|
|
*/
|
|
|
|
private $ogpResolver;
|
|
|
|
|
|
|
|
public function __construct(Client $client, OGPResolver $ogpResolver)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
$this->ogpResolver = $ogpResolver;
|
|
|
|
}
|
|
|
|
|
2019-02-03 01:10:09 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
|
|
|
$cookieJar = CookieJar::fromArray(['over18' => 'yes'], '.syosetu.com');
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
$res = $this->client->get($url, ['cookies' => $cookieJar]);
|
2019-02-03 01:10:09 +09:00
|
|
|
if ($res->getStatusCode() === 200) {
|
2019-02-17 02:58:36 +09:00
|
|
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
2019-02-04 02:35:50 +09:00
|
|
|
$metadata->description = '';
|
2019-02-03 01:10:09 +09:00
|
|
|
|
2019-02-04 02:35:50 +09:00
|
|
|
$dom = new \DOMDocument();
|
|
|
|
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'ASCII,JIS,UTF-8,eucJP-win,SJIS-win'));
|
|
|
|
$xpath = new \DOMXPath($dom);
|
|
|
|
|
|
|
|
$description = [];
|
|
|
|
|
|
|
|
// 作者名
|
|
|
|
$writerNodes = $xpath->query('//*[contains(@class, "novel_writername")]');
|
|
|
|
if ($writerNodes->length !== 0 && !empty($writerNodes->item(0)->textContent)) {
|
|
|
|
$description[] = trim($writerNodes->item(0)->textContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// あらすじ
|
|
|
|
$exNodes = $xpath->query('//*[@id="novel_ex"]');
|
|
|
|
if ($exNodes->length !== 0 && !empty($exNodes->item(0)->textContent)) {
|
|
|
|
$summary = trim($exNodes->item(0)->textContent);
|
|
|
|
$description[] = mb_strimwidth($summary, 0, 101, '…'); // 100 + '…'(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata->description = implode(' / ', $description);
|
|
|
|
|
|
|
|
return $metadata;
|
2019-02-03 01:10:09 +09:00
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|