Merge pull request #283 from eai04191/feature/resolver-fantiaResolver-improve
FantiaResolverを更新・テストを追加
This commit is contained in:
commit
a20f690cdd
@ -3,7 +3,6 @@
|
|||||||
namespace App\MetadataResolver;
|
namespace App\MetadataResolver;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
|
|
||||||
class FantiaResolver implements Resolver
|
class FantiaResolver implements Resolver
|
||||||
{
|
{
|
||||||
@ -11,41 +10,30 @@ class FantiaResolver implements Resolver
|
|||||||
* @var Client
|
* @var Client
|
||||||
*/
|
*/
|
||||||
private $client;
|
private $client;
|
||||||
/**
|
|
||||||
* @var OGPResolver
|
|
||||||
*/
|
|
||||||
private $ogpResolver;
|
|
||||||
|
|
||||||
public function __construct(Client $client, OGPResolver $ogpResolver)
|
public function __construct(Client $client)
|
||||||
{
|
{
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
$this->ogpResolver = $ogpResolver;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolve(string $url): Metadata
|
public function resolve(string $url): Metadata
|
||||||
{
|
{
|
||||||
preg_match("~\d+~", $url, $match);
|
preg_match("~posts/(\d+)~", $url, $match);
|
||||||
$postId = $match[0];
|
$postId = $match[1];
|
||||||
|
|
||||||
$res = $this->client->get($url);
|
$res = $this->client->get("https://fantia.jp/api/v1/posts/{$postId}");
|
||||||
$metadata = $this->ogpResolver->parse($res->getBody());
|
$data = json_decode(str_replace('\r\n', '\n', (string) $res->getBody()), true);
|
||||||
|
$post = $data['post'];
|
||||||
|
|
||||||
$dom = new \DOMDocument();
|
$tags = array_map(function ($tag) {
|
||||||
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
|
return $tag['name'];
|
||||||
$xpath = new \DOMXPath($dom);
|
}, $post['tags']);
|
||||||
|
|
||||||
$node = $xpath->query("//meta[@property='twitter:image']")->item(0);
|
$metadata = new Metadata();
|
||||||
$ogpUrl = $node->getAttribute('content');
|
$metadata->title = $post['title'] ?? '';
|
||||||
|
$metadata->description = 'サークル: ' . $post['fanclub']['fanclub_name_with_creator_name'] . PHP_EOL . $post['comment'];
|
||||||
// 投稿に画像がない場合(ogp.jpgでない場合)のみ大きい画像に変換する
|
$metadata->image = str_replace('micro', 'main', $post['thumb_micro']) ?? '';
|
||||||
if ($ogpUrl != 'http://fantia.jp/images/ogp.jpg') {
|
$metadata->tags = array_merge($tags, [$post['fanclub']['creator_name']]);
|
||||||
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}";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $metadata;
|
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