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'] ?? '?');
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|