tissue/app/MetadataResolver/MetadataResolver.php

63 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\MetadataResolver;
class MetadataResolver implements Resolver
{
public $rules = [
'~(((sp\.)?seiga\.nicovideo\.jp/seiga(/#!)?|nico\.ms))/im~' => NicoSeigaResolver::class,
'~nijie\.info/view(_popup)?\.php~' => NijieResolver::class,
'~komiflo\.com(/#!)?/comics/(\\d+)~' => KomifloResolver::class,
'~www\.melonbooks\.co\.jp/detail/detail\.php~' => MelonbooksResolver::class,
'~ec\.toranoana\.jp/tora_r/ec/item/.*~' => ToranoanaResolver::class,
2018-11-20 23:31:57 +09:00
'~iwara\.tv/videos/.*~' => IwaraResolver::class,
2019-01-14 04:22:40 +09:00
'~www\.dlsite\.com/.*/work/=/product_id/..\d+\.html~' => DLsiteResolver::class,
2019-01-14 16:01:58 +09:00
'~www\.pixiv\.net/member_illust\.php\?illust_id=\d+~' => PixivResolver::class,
2019-01-14 22:55:01 +09:00
'~fantia\.jp/posts/\d+~' => FantiaResolver::class,
'~dmm\.co\.jp/~' => FanzaResolver::class,
2019-02-03 20:35:59 +09:00
'~www\.patreon\.com/~' => PatreonResolver::class,
2019-01-19 16:19:34 +09:00
'~www\.deviantart\.com/.*/art/.*~' => DeviantArtResolver::class,
2019-02-03 01:10:09 +09:00
'~\.syosetu\.com/n\d+[a-z]{2,}~' => NarouResolver::class,
2019-02-09 04:04:41 +09:00
];
public $mimeTypes = [
'application/activity+json' => ActivityPubResolver::class,
'application/ld+json' => ActivityPubResolver::class,
'text/html' => OGPResolver::class
];
public function resolve(string $url): Metadata
{
foreach ($this->rules as $pattern => $class) {
if (preg_match($pattern, $url) === 1) {
2019-01-15 00:05:01 +09:00
$resolver = new $class();
return $resolver->resolve($url);
}
}
2019-02-09 04:04:41 +09:00
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $url, [
'headers' => [
'Accept' => implode(', ', array_keys($this->mimeTypes))
]
]);
if ($res->getStatusCode() === 200) {
preg_match('/^[^;\s]+/', $res->getHeaderLine('Content-Type'), $matches);
$mimeType = $matches[0];
if (isset($this->mimeTypes[$mimeType])) {
$class = $this->mimeTypes[$mimeType];
$parser = new $class();
return $parser->parse($res->getBody());
} else {
throw new \UnexpectedValueException('URL not matched.');
}
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
}
2019-01-15 00:05:01 +09:00
}