2019-01-18 00:16:02 +09:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
|
use GuzzleHttp\Client;
|
2019-09-12 11:39:18 +09:00
|
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
2019-02-17 02:58:36 +09:00
|
|
|
|
|
2019-01-18 00:16:02 +09:00
|
|
|
|
class FanzaResolver 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-09-12 11:39:18 +09:00
|
|
|
|
/**
|
|
|
|
|
* arrayの各要素をtrim・スペースの_置換をした後、重複した値を削除してキーを詰め直す
|
|
|
|
|
*
|
|
|
|
|
* @param array $array
|
|
|
|
|
*
|
|
|
|
|
* @return array 処理されたarray
|
|
|
|
|
*/
|
2019-09-13 01:05:09 +09:00
|
|
|
|
public function array_finish(array $array): array
|
2019-09-12 11:39:18 +09:00
|
|
|
|
{
|
|
|
|
|
$array = array_map('trim', $array);
|
|
|
|
|
$array = array_map((function ($value) {
|
|
|
|
|
return str_replace(' ', '_', $value);
|
|
|
|
|
}), $array);
|
|
|
|
|
$array = array_unique($array);
|
|
|
|
|
$array = array_values($array);
|
2019-09-12 11:58:21 +09:00
|
|
|
|
|
2019-09-12 11:39:18 +09:00
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 00:16:02 +09:00
|
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
|
$res = $this->client->get($url);
|
2019-09-12 11:39:18 +09:00
|
|
|
|
$html = (string) $res->getBody();
|
|
|
|
|
$crawler = new Crawler($html);
|
|
|
|
|
|
|
|
|
|
// 動画
|
|
|
|
|
if (preg_match('~www\.dmm\.co\.jp/digital/(videoa|videoc|anime)/-/detail~', $url)) {
|
|
|
|
|
$metadata = new Metadata();
|
|
|
|
|
$metadata->title = trim($crawler->filter('#title')->text(''));
|
|
|
|
|
$metadata->description = trim($crawler->filter('.box-rank+table+div+div')->text(''));
|
|
|
|
|
$metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $crawler->filter('meta[property="og:image"]')->attr('content'));
|
|
|
|
|
$metadata->tags = $this->array_finish($crawler->filter('.box-rank+table a:not([href="#review"])')->extract(['_text']));
|
|
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
|
}
|
2019-01-18 00:16:02 +09:00
|
|
|
|
|
2019-09-12 11:39:18 +09:00
|
|
|
|
// 同人
|
|
|
|
|
if (mb_strpos($url, 'www.dmm.co.jp/dc/doujin/-/detail/') !== false) {
|
|
|
|
|
$genre = $this->array_finish($crawler->filter('.m-productInformation a:not([href="#update-top"])')->extract(['_text']));
|
|
|
|
|
$genre = array_filter($genre, (function ($text) {
|
|
|
|
|
return !preg_match('~%OFF対象$~', $text);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
$metadata = new Metadata();
|
|
|
|
|
$metadata->title = $crawler->filter('meta[property="og:title"]')->attr('content');
|
|
|
|
|
$metadata->description = trim($crawler->filter('.summary__txt')->text(''));
|
|
|
|
|
$metadata->image = $crawler->filter('meta[property="og:image"]')->attr('content');
|
|
|
|
|
$metadata->tags = array_merge($genre, [$crawler->filter('.circleName__txt')->text('')]);
|
|
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 電子書籍
|
|
|
|
|
if (mb_strpos($url, 'book.dmm.co.jp/detail/') !== false) {
|
|
|
|
|
$metadata = new Metadata();
|
|
|
|
|
$metadata->title = trim($crawler->filter('#title')->text(''));
|
|
|
|
|
$metadata->description = trim($crawler->filter('.m-boxDetailProduct__info__story')->text(''));
|
|
|
|
|
$metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $crawler->filter('meta[property="og:image"]')->attr('content'));
|
2019-09-13 01:00:26 +09:00
|
|
|
|
$metadata->tags = $this->array_finish($crawler->filter('.m-boxDetailProductInfoMainList__description__list__item, .m-boxDetailProductInfo__list__description__item a')->extract(['_text']));
|
2019-09-12 11:39:18 +09:00
|
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PCゲーム
|
|
|
|
|
if (mb_strpos($url, 'dlsoft.dmm.co.jp/detail/') !== false) {
|
|
|
|
|
$metadata = new Metadata();
|
|
|
|
|
$metadata->title = trim($crawler->filter('#title')->text(''));
|
|
|
|
|
$metadata->description = trim($crawler->filter('.area-detail-read .text-overflow')->text(''));
|
|
|
|
|
$metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $crawler->filter('meta[property="og:image"]')->attr('content'));
|
|
|
|
|
$metadata->tags = $this->array_finish($crawler->filter('.area-bskt table a:not([href="#review"])')->extract(['_text']));
|
|
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上で特に対応しなかったURL 画像の置換くらいはしておく
|
|
|
|
|
$metadata = $this->ogpResolver->parse($html);
|
|
|
|
|
$metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $metadata->image);
|
2019-09-12 11:58:21 +09:00
|
|
|
|
|
2019-09-10 07:03:46 +09:00
|
|
|
|
return $metadata;
|
2019-01-18 00:16:02 +09:00
|
|
|
|
}
|
|
|
|
|
}
|