2019-01-14 04:22:40 +09:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
|
2019-01-14 04:22:40 +09:00
|
|
|
|
class DLsiteResolver 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-01-14 04:22:40 +09:00
|
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
|
$res = $this->client->get($url);
|
2019-01-14 04:22:40 +09:00
|
|
|
|
if ($res->getStatusCode() === 200) {
|
2019-02-17 02:58:36 +09:00
|
|
|
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
2019-03-08 06:21:33 +09:00
|
|
|
|
|
2019-04-30 06:15:27 +09:00
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
|
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
|
|
|
|
|
$xpath = new \DOMXPath($dom);
|
|
|
|
|
|
2019-03-08 06:21:33 +09:00
|
|
|
|
// 抽出
|
2019-04-30 06:15:27 +09:00
|
|
|
|
$title = $xpath->query('//title')->item(0)->textContent;
|
|
|
|
|
preg_match('~\[(.+)\] \| DLsite.+$~', $title, $match);
|
2019-03-08 06:21:33 +09:00
|
|
|
|
$maker = $match[1];
|
|
|
|
|
|
2019-04-30 06:15:27 +09:00
|
|
|
|
// makerに一致するthのテキストを探す
|
|
|
|
|
$makerHead = $xpath->query('//a[contains(text(), "'.$maker.'")]/ancestor::tr/th')->item(0)->textContent;
|
|
|
|
|
|
2019-03-08 06:21:33 +09:00
|
|
|
|
// 余分な文を消す
|
2019-03-08 06:29:16 +09:00
|
|
|
|
$metadata->title = trim(preg_replace('~ \[.+\] \| DLsite$~', '', $metadata->title));
|
|
|
|
|
$metadata->description = trim(preg_replace('~「DLsite.+」は同人誌・同人ゲーム・同人音声のダウンロードショップ。お気に入りの作品をすぐダウンロードできてすぐ楽しめる!毎日更新しているのであなたが探している作品にきっと出会えます。国内最大級の二次元総合ダウンロードショップ「DLsite」!$~', '', $metadata->description));
|
2019-03-08 06:21:33 +09:00
|
|
|
|
|
|
|
|
|
// 整形
|
2019-04-30 06:15:27 +09:00
|
|
|
|
$metadata->description = $makerHead.': ' . $maker . PHP_EOL . $metadata->description;
|
2019-02-17 02:58:36 +09:00
|
|
|
|
$metadata->image = str_replace('img_sam.jpg', 'img_main.jpg', $metadata->image);
|
2019-01-15 00:05:01 +09:00
|
|
|
|
|
2019-01-14 04:22:40 +09:00
|
|
|
|
return $metadata;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
|
}
|