2018-11-20 23:31:57 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
2019-08-21 05:08:17 +09:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
2019-02-17 02:58:36 +09:00
|
|
|
|
2018-11-20 23:31:57 +09:00
|
|
|
class IwaraResolver implements Resolver
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
public function __construct(Client $client)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
2018-11-20 23:31:57 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
$res = $this->client->get($url);
|
2018-11-20 23:31:57 +09:00
|
|
|
if ($res->getStatusCode() === 200) {
|
|
|
|
$metadata = new Metadata();
|
2019-08-21 05:08:17 +09:00
|
|
|
$html = (string) $res->getBody();
|
|
|
|
$crawler = new Crawler($html);
|
|
|
|
|
|
|
|
$descriptionElement = $crawler->filter('#video-player + div, .field-name-field-video-url + div, .field-name-field-images + div');
|
|
|
|
$title = $descriptionElement->filter('h1.title')->text();
|
|
|
|
$author = $descriptionElement->filter('.username')->text();
|
2019-08-22 07:28:28 +09:00
|
|
|
$description = '';
|
|
|
|
if ($descriptionElement->filter('.field-type-text-with-summary')->count()) {
|
|
|
|
$description = $descriptionElement->filter('.field-type-text-with-summary')->text();
|
|
|
|
}
|
2019-08-21 05:08:17 +09:00
|
|
|
$tags = $descriptionElement->filter('a[href^="/video-categories"], a[href^="/images"]')->extract('_text');
|
2019-08-22 05:17:23 +09:00
|
|
|
// 役に立たないタグを削除する
|
|
|
|
$tags = array_values(array_diff($tags, ['Uncategorized', 'Other']));
|
2019-08-22 05:23:28 +09:00
|
|
|
array_push($tags, $author);
|
2019-08-21 05:08:17 +09:00
|
|
|
|
|
|
|
$metadata->title = $title;
|
|
|
|
$metadata->description = '投稿者: ' . $author . PHP_EOL . $description;
|
|
|
|
$metadata->tags = $tags;
|
|
|
|
|
|
|
|
// iwara video
|
|
|
|
if ($crawler->filter('#video-player')->count()) {
|
|
|
|
$metadata->image = 'https:' . $crawler->filter('#video-player')->attr('poster');
|
2018-11-20 23:31:57 +09:00
|
|
|
}
|
|
|
|
|
2019-08-21 05:08:17 +09:00
|
|
|
// youtube
|
|
|
|
if ($crawler->filter('iframe[src^="//www.youtube.com"]')->count()) {
|
|
|
|
if (preg_match('~youtube\.com/embed/(\S+)\?~', $crawler->filter('iframe[src^="//www.youtube.com"]')->attr('src'), $matches) === 1) {
|
|
|
|
$youtubeId = $matches[1];
|
|
|
|
$metadata->image = 'https://img.youtube.com/vi/' . $youtubeId . '/maxresdefault.jpg';
|
2018-11-20 23:31:57 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 05:08:17 +09:00
|
|
|
// images
|
|
|
|
if ($crawler->filter('.field-name-field-images')->count()) {
|
|
|
|
$metadata->image = 'https:' . $crawler->filter('.field-name-field-images a')->first()->attr('href');
|
2018-11-20 23:31:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|