2019-08-06 22:44:38 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2019-09-09 13:51:07 +09:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
2019-08-06 22:44:38 +09:00
|
|
|
|
|
|
|
class XtubeResolver implements Resolver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
2019-11-04 14:23:30 +09:00
|
|
|
/**
|
|
|
|
* @var OGPResolver
|
|
|
|
*/
|
|
|
|
private $ogpResolver;
|
2019-08-06 22:44:38 +09:00
|
|
|
|
2019-11-04 14:23:30 +09:00
|
|
|
public function __construct(Client $client, OGPResolver $ogpResolver)
|
2019-08-06 22:44:38 +09:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2019-11-04 14:23:30 +09:00
|
|
|
$this->ogpResolver = $ogpResolver;
|
2019-08-06 22:44:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-09-09 13:51:07 +09:00
|
|
|
if (preg_match('~www\.xtube\.com/video-watch/.*-(\d+)$~', $url) !== 1) {
|
2019-08-06 22:44:38 +09:00
|
|
|
throw new \RuntimeException("Unmatched URL Pattern: $url");
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:51:07 +09:00
|
|
|
$res = $this->client->get($url);
|
2019-09-09 13:52:32 +09:00
|
|
|
$html = (string) $res->getBody();
|
2019-11-04 14:23:30 +09:00
|
|
|
$metadata = $this->ogpResolver->parse($html);
|
2019-09-09 13:52:32 +09:00
|
|
|
$crawler = new Crawler($html);
|
|
|
|
|
|
|
|
$metadata->title = trim($crawler->filter('.underPlayerRateForm h1')->text(''));
|
|
|
|
$metadata->description = trim($crawler->filter('.fullDescription ')->text(''));
|
2019-11-04 14:23:30 +09:00
|
|
|
$metadata->image = str_replace('m=eSuQ8f', 'm=eaAaaEFb', $metadata->image);
|
|
|
|
$metadata->image = str_replace('240X180', 'original', $metadata->image);
|
2019-11-05 05:59:00 +09:00
|
|
|
$metadata->tags = array_map('trim', $crawler->filter('.tagsCategories a')->extract('_text'));
|
2019-09-09 13:52:32 +09:00
|
|
|
|
|
|
|
return $metadata;
|
2019-08-06 22:44:38 +09:00
|
|
|
}
|
|
|
|
}
|