2018-06-08 00:44:42 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
2018-06-08 00:44:42 +09:00
|
|
|
class KomifloResolver implements Resolver
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
public function __construct(Client $client)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
2018-06-08 00:44:42 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
|
|
|
if (preg_match('~komiflo\.com(?:/#!)?/comics/(\\d+)~', $url, $matches) !== 1) {
|
|
|
|
throw new \RuntimeException("Unmatched URL Pattern: $url");
|
|
|
|
}
|
|
|
|
$id = $matches[1];
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
$res = $this->client->get('https://api.komiflo.com/content/id/' . $id);
|
2018-06-08 00:44:42 +09:00
|
|
|
if ($res->getStatusCode() === 200) {
|
|
|
|
$json = json_decode($res->getBody()->getContents(), true);
|
|
|
|
$metadata = new Metadata();
|
|
|
|
|
|
|
|
$metadata->title = $json['content']['data']['title'] ?? '';
|
|
|
|
$metadata->description = ($json['content']['attributes']['artists']['children'][0]['data']['name'] ?? '?') .
|
|
|
|
' - ' .
|
|
|
|
($json['content']['parents'][0]['data']['title'] ?? '?');
|
2019-02-26 22:55:55 +09:00
|
|
|
$metadata->image = 'https://t.komiflo.com/564_mobile_large_3x/' . $json['content']['named_imgs']['cover']['filename'];
|
2018-06-08 00:44:42 +09:00
|
|
|
|
2019-04-29 14:49:34 +09:00
|
|
|
// 作者情報
|
|
|
|
if (!empty($json['content']['attributes']['artists']['children'])) {
|
|
|
|
foreach ($json['content']['attributes']['artists']['children'] as $artist) {
|
|
|
|
$metadata->tags[] = preg_replace('/\s/', '_', $artist['data']['name']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// タグ
|
|
|
|
if (!empty($json['content']['attributes']['tags']['children'])) {
|
|
|
|
foreach ($json['content']['attributes']['tags']['children'] as $tag) {
|
|
|
|
$metadata->tags[] = preg_replace('/\s/', '_', $tag['data']['name']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 00:44:42 +09:00
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|