FC2ContentsResolverを追加
This commit is contained in:
parent
16005931fc
commit
2db45951f4
44
app/MetadataResolver/FC2ContentsResolver.php
Normal file
44
app/MetadataResolver/FC2ContentsResolver.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\MetadataResolver;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
|
class FC2ContentsResolver 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
|
||||||
|
{
|
||||||
|
$res = $this->client->get($url);
|
||||||
|
if ($res->getStatusCode() === 200) {
|
||||||
|
$metadata = $this->ogpResolver->parse($res->getBody());
|
||||||
|
|
||||||
|
$dom = new \DOMDocument();
|
||||||
|
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
|
||||||
|
$xpath = new \DOMXPath($dom);
|
||||||
|
|
||||||
|
$thumbnailNode = $xpath->query('//*[@class="main_thum_img"]/a')->item(0);
|
||||||
|
if ($thumbnailNode) {
|
||||||
|
$metadata->image = preg_replace('~^http:~', "https:", $thumbnailNode->getAttribute('href'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $metadata;
|
||||||
|
} else {
|
||||||
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -25,6 +25,7 @@ class MetadataResolver implements Resolver
|
|||||||
'~\.syosetu\.com/n\d+[a-z]{2,}~' => NarouResolver::class,
|
'~\.syosetu\.com/n\d+[a-z]{2,}~' => NarouResolver::class,
|
||||||
'~ci-en\.jp/creator/\d+/article/\d+~' => CienResolver::class,
|
'~ci-en\.jp/creator/\d+/article/\d+~' => CienResolver::class,
|
||||||
'~www\.plurk\.com\/p\/.*~' => PlurkResolver::class,
|
'~www\.plurk\.com\/p\/.*~' => PlurkResolver::class,
|
||||||
|
'~(adult\.)?contents\.fc2\.com\/article_search\.php\?id=\d+~' => FC2ContentsResolver::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
public $mimeTypes = [
|
public $mimeTypes = [
|
||||||
|
Loading…
Reference in New Issue
Block a user