ActivityPubResolverを追加
This commit is contained in:
parent
7337f60491
commit
5750eeb3a5
71
app/MetadataResolver/ActivityPubResolver.php
Normal file
71
app/MetadataResolver/ActivityPubResolver.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\MetadataResolver;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class ActivityPubResolver implements Resolver, Parser
|
||||
{
|
||||
/**
|
||||
* @var \GuzzleHttp\Client
|
||||
*/
|
||||
private $activityClient;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->activityClient = new \GuzzleHttp\Client([
|
||||
'headers' => [
|
||||
'Accept' => 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function resolve(string $url): Metadata
|
||||
{
|
||||
$res = $this->activityClient->get($url);
|
||||
if ($res->getStatusCode() === 200) {
|
||||
return $this->parse($res->getBody());
|
||||
} else {
|
||||
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
||||
}
|
||||
}
|
||||
|
||||
public function parse(string $json): Metadata
|
||||
{
|
||||
$activityOrObject = json_decode($json, true);
|
||||
$object = $activityOrObject['object'] ?? $activityOrObject;
|
||||
|
||||
$metadata = new Metadata();
|
||||
|
||||
$metadata->title = isset($object['attributedTo']) ? $this->getTitleFromActor($object['attributedTo']) : '';
|
||||
$metadata->description = isset($object['content']) ? $this->html2text($object['content']) : '';
|
||||
$metadata->image = $object['attachment'][0]['url'] ?? '';
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
private function getTitleFromActor(string $url): string
|
||||
{
|
||||
$res = $this->activityClient->get($url);
|
||||
if ($res->getStatusCode() !== 200) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$actor = json_decode($res->getBody(), true);
|
||||
$title = $actor['name'] ?? '';
|
||||
if (isset($actor['preferredUsername'])) {
|
||||
$title .= ' (@' . $actor['preferredUsername'] . '@' . parse_url($actor['id'], PHP_URL_HOST) . ')';
|
||||
}
|
||||
|
||||
return $title;
|
||||
}
|
||||
|
||||
private function html2text(string $html): string
|
||||
{
|
||||
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
|
||||
$html = preg_replace('~<br\s*/?\s*>|</p>\s*<p[^>]*>~i', "\n", $html);
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||
return $dom->textContent;
|
||||
}
|
||||
}
|
@ -18,7 +18,12 @@ class MetadataResolver implements Resolver
|
||||
'~www\.patreon\.com/~' => PatreonResolver::class,
|
||||
'~www\.deviantart\.com/.*/art/.*~' => DeviantArtResolver::class,
|
||||
'~\.syosetu\.com/n\d+[a-z]{2,}~' => NarouResolver::class,
|
||||
'/.*/' => OGPResolver::class
|
||||
];
|
||||
|
||||
public $mimeTypes = [
|
||||
'application/activity+json' => ActivityPubResolver::class,
|
||||
'application/ld+json' => ActivityPubResolver::class,
|
||||
'text/html' => OGPResolver::class
|
||||
];
|
||||
|
||||
public function resolve(string $url): Metadata
|
||||
@ -31,6 +36,27 @@ class MetadataResolver implements Resolver
|
||||
}
|
||||
}
|
||||
|
||||
$client = new \GuzzleHttp\Client();
|
||||
$res = $client->request('GET', $url, [
|
||||
'headers' => [
|
||||
'Accept' => implode(', ', array_keys($this->mimeTypes))
|
||||
]
|
||||
]);
|
||||
|
||||
if ($res->getStatusCode() === 200) {
|
||||
preg_match('/^[^;\s]+/', $res->getHeaderLine('Content-Type'), $matches);
|
||||
$mimeType = $matches[0];
|
||||
|
||||
if (isset($this->mimeTypes[$mimeType])) {
|
||||
$class = $this->mimeTypes[$mimeType];
|
||||
$parser = new $class();
|
||||
|
||||
return $parser->parse($res->getBody());
|
||||
} else {
|
||||
throw new \UnexpectedValueException('URL not matched.');
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\MetadataResolver;
|
||||
|
||||
class OGPResolver implements Resolver
|
||||
class OGPResolver implements Resolver, Parser
|
||||
{
|
||||
public function resolve(string $url): Metadata
|
||||
{
|
||||
|
8
app/MetadataResolver/Parser.php
Normal file
8
app/MetadataResolver/Parser.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\MetadataResolver;
|
||||
|
||||
interface Parser
|
||||
{
|
||||
public function parse(string $body): Metadata;
|
||||
}
|
Loading…
Reference in New Issue
Block a user