FantiaResolverを更新・テストを追加
APIを見つけたのでAPIを使用するように タグを追加 テストを追加
This commit is contained in:
parent
92847fefe0
commit
a9a0f3b99a
@ -3,7 +3,6 @@
|
||||
namespace App\MetadataResolver;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class FantiaResolver implements Resolver
|
||||
{
|
||||
@ -11,41 +10,30 @@ class FantiaResolver implements Resolver
|
||||
* @var Client
|
||||
*/
|
||||
private $client;
|
||||
/**
|
||||
* @var OGPResolver
|
||||
*/
|
||||
private $ogpResolver;
|
||||
|
||||
public function __construct(Client $client, OGPResolver $ogpResolver)
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->ogpResolver = $ogpResolver;
|
||||
}
|
||||
|
||||
public function resolve(string $url): Metadata
|
||||
{
|
||||
preg_match("~\d+~", $url, $match);
|
||||
$postId = $match[0];
|
||||
preg_match("~posts/(\d+)~", $url, $match);
|
||||
$postId = $match[1];
|
||||
|
||||
$res = $this->client->get($url);
|
||||
$metadata = $this->ogpResolver->parse($res->getBody());
|
||||
$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'];
|
||||
|
||||
$dom = new \DOMDocument();
|
||||
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
|
||||
$xpath = new \DOMXPath($dom);
|
||||
$tags = array_map(function ($tag) {
|
||||
return $tag['name'];
|
||||
}, $post['tags']);
|
||||
|
||||
$node = $xpath->query("//meta[@property='twitter:image']")->item(0);
|
||||
$ogpUrl = $node->getAttribute('content');
|
||||
|
||||
// 投稿に画像がない場合(ogp.jpgでない場合)のみ大きい画像に変換する
|
||||
if ($ogpUrl != 'http://fantia.jp/images/ogp.jpg') {
|
||||
preg_match("~https://fantia\.s3\.amazonaws\.com/uploads/post/file/{$postId}/ogp_(.*?)\.(jpg|png)~", $ogpUrl, $match);
|
||||
$uuid = $match[1];
|
||||
$extension = $match[2];
|
||||
|
||||
// 大きい画像に変換
|
||||
$metadata->image = "https://c.fantia.jp/uploads/post/file/{$postId}/main_{$uuid}.{$extension}";
|
||||
}
|
||||
$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']]);
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
36
tests/Unit/MetadataResolver/FantiaResolverTest.php
Normal file
36
tests/Unit/MetadataResolver/FantiaResolverTest.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\MetadataResolver;
|
||||
|
||||
use App\MetadataResolver\FantiaResolver;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FantiaResolverTest extends TestCase
|
||||
{
|
||||
use CreateMockedResolver;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
if (!$this->shouldUseMock()) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
$responseText = file_get_contents(__DIR__ . '/../../fixture/Fantia/test.json');
|
||||
|
||||
$this->createResolver(FantiaResolver::class, $responseText);
|
||||
|
||||
$metadata = $this->resolver->resolve('https://fantia.jp/posts/206561');
|
||||
$this->assertSame('召喚士アルドラ', $metadata->title);
|
||||
$this->assertSame('サークル: サークルぬるま湯 (ナナナナ)' . PHP_EOL . 'コミッション' . PHP_EOL . 'クイーンズブレイドリベリオンの召喚士アルドラです。', $metadata->description);
|
||||
$this->assertSame('https://c.fantia.jp/uploads/post/file/206561/main_dbcc59e5-4090-4650-b969-8855a721c6a5.jpg', $metadata->image);
|
||||
$this->assertSame(['ふたなり', '超乳', '超根', 'クイーンズブレイド', 'ナナナナ'], $metadata->tags);
|
||||
if ($this->shouldUseMock()) {
|
||||
$this->assertSame('https://fantia.jp/api/v1/posts/206561', (string) $this->handler->getLastRequest()->getUri());
|
||||
}
|
||||
}
|
||||
}
|
1
tests/fixture/Fantia/test.json
vendored
Normal file
1
tests/fixture/Fantia/test.json
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user