Kb10uyShortStoryServerResolver とテストを追加
This commit is contained in:
42
app/MetadataResolver/Kb10uyShortStoryServerResolver.php
Normal file
42
app/MetadataResolver/Kb10uyShortStoryServerResolver.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\MetadataResolver;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
class Kb10uyShortStoryServerResolver implements Resolver
|
||||
{
|
||||
protected const EXCLUDED_TAGS = ['R-15', 'R-18'];
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
private $client;
|
||||
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function resolve(string $url): Metadata
|
||||
{
|
||||
$res = $this->client->get($url);
|
||||
if ($res->getStatusCode() !== 200) {
|
||||
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
||||
|
||||
return $this->parse($res->getBody());
|
||||
}
|
||||
|
||||
$html = (string) $res->getBody();
|
||||
$crawler = new Crawler($html);
|
||||
$infoElement = $crawler->filter('div.post-info');
|
||||
|
||||
$metadata = new Metadata();
|
||||
$metadata->title = $infoElement->filter('h1')->text();
|
||||
$metadata->description = trim($infoElement->filter('p.summary')->text());
|
||||
$metadata->tags = array_values(array_diff($infoElement->filter('ul.tags > li.tag > a')->extract('_text'), self::EXCLUDED_TAGS));
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
}
|
@@ -32,6 +32,7 @@ class MetadataResolver implements Resolver
|
||||
'~(adult\.)?contents\.fc2\.com\/article_search\.php\?id=\d+~' => FC2ContentsResolver::class,
|
||||
'~store\.steampowered\.com/app/\d+~' => SteamResolver::class,
|
||||
'~www\.xtube\.com/video-watch/.*-\d+$~'=> XtubeResolver::class,
|
||||
'~ss\.kb10uy\.org/posts/\d+$~' => Kb10uyShortStoryServerResolver::class,
|
||||
];
|
||||
|
||||
public $mimeTypes = [
|
||||
|
Reference in New Issue
Block a user