2019-01-19 16:19:34 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
2019-01-19 16:19:34 +09:00
|
|
|
class DeviantArtResolver implements Resolver
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
/**
|
|
|
|
* @var OGPResolver
|
|
|
|
*/
|
|
|
|
private $ogpResolver;
|
|
|
|
|
|
|
|
public function __construct(Client $client, OGPResolver $ogpResolver)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
$this->ogpResolver = $ogpResolver;
|
|
|
|
}
|
|
|
|
|
2019-01-19 16:19:34 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-08-07 19:22:54 +09:00
|
|
|
$res = $this->client->get('https://backend.deviantart.com/oembed?url=' . $url);
|
2019-01-19 16:19:34 +09:00
|
|
|
if ($res->getStatusCode() === 200) {
|
2019-08-07 19:22:54 +09:00
|
|
|
$data = json_decode($res->getBody()->getContents(), true);
|
|
|
|
$metadata = new Metadata();
|
2019-01-19 16:19:34 +09:00
|
|
|
|
2019-08-07 19:22:54 +09:00
|
|
|
if (preg_match('~\.wixmp\.com$~', parse_url($data['url'])['host'])) {
|
2019-08-27 21:36:44 +09:00
|
|
|
// アスペクト比を保ったまま、縦か横が最大1024pxになる画像を取得する。
|
2019-01-19 16:19:34 +09:00
|
|
|
// Ref: https://support.wixmp.com/en/article/image-service-3835799
|
2019-08-27 21:36:44 +09:00
|
|
|
// 作成されていない画像が参照されると403を返すようなので、サイト内で使用されている1024pxにした。
|
2019-08-07 19:22:54 +09:00
|
|
|
if (strpos($data['url'], '/v1/fill/')) {
|
2019-08-27 21:36:44 +09:00
|
|
|
$metadata->image = preg_replace('~/v1/fill/w_\d+,h_\d+(?:,q_\d+),strp/.+\.(jpg|png|webp|gif)~', '/v1/fit/w_1024,h_1024,strp/image.jpg', $data['url']);
|
2019-01-19 16:19:34 +09:00
|
|
|
} else {
|
2019-08-27 20:49:04 +09:00
|
|
|
$queryStartPos = strpos($data['url'], '?');
|
2019-08-27 21:36:44 +09:00
|
|
|
$metadata->image = substr_replace($data['url'], '/v1/fit/w_1024,h_1024,strp/image.jpg', $queryStartPos, 0);
|
2019-01-19 16:19:34 +09:00
|
|
|
}
|
2019-08-07 19:22:54 +09:00
|
|
|
} else {
|
|
|
|
$metadata->image = $data['url'];
|
2019-01-19 16:19:34 +09:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:22:54 +09:00
|
|
|
$metadata->title = $data['title'] ?? '';
|
|
|
|
$metadata->description = 'By ' . $data['author_name'];
|
2019-01-19 16:19:34 +09:00
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|