Compare commits

...

1 Commits

Author SHA1 Message Date
eai04191
7dac5df24b PornHubResolverを追加 2019-10-04 00:34:51 +09:00
2 changed files with 56 additions and 0 deletions

View File

@ -33,6 +33,7 @@ class MetadataResolver implements Resolver
'~store\.steampowered\.com/app/\d+~' => SteamResolver::class,
'~www\.xtube\.com/video-watch/.*-\d+$~'=> XtubeResolver::class,
'~ss\.kb10uy\.org/posts/\d+$~' => Kb10uyShortStoryServerResolver::class,
'~(..|www)\.pornhub\.com/view_video\.php\?viewkey=.+$~'=> PornHubResolver::class,
];
public $mimeTypes = [

View File

@ -0,0 +1,55 @@
<?php
namespace App\MetadataResolver;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class PornHubResolver implements Resolver
{
/**
* @var Client
*/
private $client;
/**
* @var OGPResolver
*/
private $ogpResolver;
public function __construct(Client $client, OGPResolver $ogpResolver)
{
$this->client = $client;
$this->ogpResolver = $ogpResolver;
}
public function resolve(string $url): Metadata
{
// if (preg_match('~www\.xtube\.com/video-watch/.*-(\d+)$~', $url, $matches) !== 1) {
// throw new \RuntimeException("Unmatched URL Pattern: $url");
// }
// $videoid = $matches[1];
$res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$html = (string) $res->getBody();
$metadata = $this->ogpResolver->parse($html);
$crawler = new Crawler($html);
$js = $crawler->filter('#player script')->text();
if (preg_match('~({.+});~', $js, $matches)) {
$json = $matches[1];
$data = json_decode($json, true);
$metadata->title = $data['video_title'];
$metadata->image = $data['image_url'];
}
$metadata->tags = $crawler->filter('.video-detailed-info a:not(.add-btn-small)')->extract('_text');
return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
}
}