データの取得方法をAPIからスクレイピングに変更
より多くのタグと高画質なサムネイルを取得するように変更
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\MetadataResolver;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
class XtubeResolver implements Resolver
|
||||
{
|
||||
@@ -18,20 +19,24 @@ class XtubeResolver implements Resolver
|
||||
|
||||
public function resolve(string $url): Metadata
|
||||
{
|
||||
if (preg_match('~www\.xtube\.com/video-watch/.*-(\d+)$~', $url, $matches) !== 1) {
|
||||
if (preg_match('~www\.xtube\.com/video-watch/.*-(\d+)$~', $url) !== 1) {
|
||||
throw new \RuntimeException("Unmatched URL Pattern: $url");
|
||||
}
|
||||
$videoid = $matches[1];
|
||||
|
||||
$res = $this->client->get('https://www.xtube.com/webmaster/api/getvideobyid?video_id=' . $videoid);
|
||||
$res = $this->client->get($url);
|
||||
if ($res->getStatusCode() === 200) {
|
||||
$data = json_decode($res->getBody()->getContents(), true);
|
||||
$html = (string) $res->getBody();
|
||||
$metadata = new Metadata();
|
||||
$crawler = new Crawler($html);
|
||||
|
||||
$metadata->title = $data['title'] ?? '';
|
||||
$metadata->description = strip_tags(str_replace('\n', PHP_EOL, html_entity_decode($data['description'] ?? '')));
|
||||
$metadata->image = str_replace('eSuQ8f', 'eSK08f', $data['thumb'] ?? ''); // 300x169 to 300x210
|
||||
$metadata->tags = array_values(array_unique($data['tags']));
|
||||
// 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');
|
||||
|
||||
return $metadata;
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user