2018-04-15 02:05:41 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
2019-09-10 00:32:14 +09:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
2019-02-17 02:58:36 +09:00
|
|
|
|
2018-04-15 02:05:41 +09:00
|
|
|
class NicoSeigaResolver 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;
|
|
|
|
}
|
|
|
|
|
2018-04-15 02:05:41 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
$res = $this->client->get($url);
|
2019-09-10 01:01:47 +09:00
|
|
|
$html = (string)$res->getBody();
|
|
|
|
$metadata = $this->ogpResolver->parse($html);
|
|
|
|
$crawler = new Crawler($html);
|
|
|
|
|
|
|
|
// タグ
|
|
|
|
$excludeTags = ['R-15'];
|
|
|
|
$metadata->tags = array_values(array_diff($crawler->filter('.tag')->extract(['_text']), $excludeTags));
|
|
|
|
|
|
|
|
// ページURLからサムネイルURLに変換
|
|
|
|
preg_match('~https?://(?:(?:sp\\.)?seiga\\.nicovideo\\.jp/seiga(?:/#!)?|nico\\.ms)/im(\\d+)~', $url, $matches);
|
|
|
|
$metadata->image = "https://lohas.nicoseiga.jp/thumb/${matches[1]}l?";
|
|
|
|
|
|
|
|
return $metadata;
|
2018-04-15 02:05:41 +09:00
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|