2019-01-14 22:55:01 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
2019-02-17 02:58:36 +09:00
|
|
|
use GuzzleHttp\Client;
|
2019-01-14 22:55:01 +09:00
|
|
|
|
|
|
|
class FantiaResolver implements Resolver
|
|
|
|
{
|
2019-02-17 02:58:36 +09:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
2019-09-12 06:56:28 +09:00
|
|
|
public function __construct(Client $client)
|
2019-02-17 02:58:36 +09:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
2019-03-03 00:29:43 +09:00
|
|
|
|
2019-01-14 22:55:01 +09:00
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-09-12 06:56:28 +09:00
|
|
|
preg_match("~posts/(\d+)~", $url, $match);
|
|
|
|
$postId = $match[1];
|
|
|
|
|
|
|
|
$res = $this->client->get("https://fantia.jp/api/v1/posts/{$postId}");
|
|
|
|
$data = json_decode(str_replace('\r\n', '\n', (string) $res->getBody()), true);
|
|
|
|
$post = $data['post'];
|
|
|
|
|
|
|
|
$tags = array_map(function ($tag) {
|
|
|
|
return $tag['name'];
|
|
|
|
}, $post['tags']);
|
|
|
|
|
|
|
|
$metadata = new Metadata();
|
|
|
|
$metadata->title = $post['title'] ?? '';
|
|
|
|
$metadata->description = 'サークル: ' . $post['fanclub']['fanclub_name_with_creator_name'] . PHP_EOL . $post['comment'];
|
|
|
|
$metadata->image = str_replace('micro', 'main', $post['thumb_micro']) ?? '';
|
|
|
|
$metadata->tags = array_merge($tags, [$post['fanclub']['creator_name']]);
|
2019-09-10 07:03:46 +09:00
|
|
|
|
|
|
|
return $metadata;
|
2019-01-14 22:55:01 +09:00
|
|
|
}
|
|
|
|
}
|