tissue/app/MetadataResolver/XtubeResolver.php

47 lines
1.5 KiB
PHP
Raw Normal View History

2019-08-06 22:44:38 +09:00
<?php
namespace App\MetadataResolver;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
2019-08-06 22:44:38 +09:00
class XtubeResolver implements Resolver
{
/**
* @var Client
*/
private $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function resolve(string $url): Metadata
{
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");
}
$res = $this->client->get($url);
2019-08-06 22:44:38 +09:00
if ($res->getStatusCode() === 200) {
$html = (string) $res->getBody();
2019-08-06 22:44:38 +09:00
$metadata = new Metadata();
$crawler = new Crawler($html);
2019-08-06 22:44:38 +09:00
// poster URL抽出
$playerConfig = explode("\n", trim($crawler->filter('#playerWrapper script')->last()->text()));
preg_match('~https:\\\/\\\/cdn\d+-s-hw-e5\.xtube\.com\\\/m=(?P<size>.{8})\\\/videos\\\/\d{6}\\\/\d{2}\\\/.{5}-.{4}-\\\/original\\\/\d+\.jpg~', $playerConfig[0], $matches);
$metadata->image = str_replace('\/', '/', $matches[0]);
$metadata->title = trim($crawler->filter('.underPlayerRateForm h1')->text(''));
$metadata->description = trim($crawler->filter('.fullDescription ')->text(''));
$metadata->tags = $crawler->filter('.tagsCategories a')->extract('_text');
2019-08-06 22:44:38 +09:00
return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
}
}