More work on parsing microformats
This commit is contained in:
1 parent
4179ee10e9
commit
2735abde64
4 files changed
+103
-54
No files matched your search
+45
-15
@@ -54,42 +54,72 @@ class Endpoint {
|
||||
|
||||
if ($response->getStatusCode() < 400)
|
||||
{
|
||||
[$type, $content] = $this->parseContent($response->getContent(), $target);
|
||||
$author = $this->parseAuthor($response->getContent());
|
||||
$document = new Crawler($response->getContent());
|
||||
|
||||
$webmention = new Webmention(null, $target, $source, $type, $content, $author);
|
||||
if (!$this->hasMention($target, $document))
|
||||
{
|
||||
throw new TargetNotMentionedException();
|
||||
}
|
||||
|
||||
$container = $this->getContainer($target, $document);
|
||||
$type = $this->parseMentionType($target, $container);
|
||||
$content = $this->parseContent($target, $container, $type);
|
||||
$author = $this->parseAuthor($container);
|
||||
|
||||
$webmention = new Webmention(null, $target, $source, $type, null, $author);
|
||||
$this->gateway->save($webmention);
|
||||
} else {
|
||||
throw new SourceNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function parseContent(string $content, string $target) : array
|
||||
private function hasMention(string $target, Crawler $document) : bool
|
||||
{
|
||||
$body = new Crawler($content);
|
||||
$anchors = $body->filter('a[href="'. $target . '"]');
|
||||
|
||||
if (!$anchors->count()) {
|
||||
throw new TargetNotMentionedException();
|
||||
}
|
||||
$type = $this->classToMentionType($anchors->attr('class'));
|
||||
return [$type, null];
|
||||
return $document->filter('a[href="' . $target . '"]')->count() > 0;
|
||||
}
|
||||
|
||||
private function classToMentionType(string $class = "") : MentionType
|
||||
private function getContainer(string $target, Crawler $document) : Crawler
|
||||
{
|
||||
return $document->filter('a[href="' . $target . '"]')->closest('.h-entry') ?? $document;
|
||||
}
|
||||
|
||||
private function parseMentionType(string $target, Crawler $document) : MentionType
|
||||
{
|
||||
$class = $document->filter('a[href="'. $target . '"]')->attr('class');
|
||||
|
||||
if (str_contains($class, "u-like-of")) {
|
||||
return MentionType::Like;
|
||||
} else if (str_contains($class, "u-in-reply-to")) {
|
||||
return MentionType::Reply;
|
||||
} else if (str_contains($class, "u-repost-of")) {
|
||||
return MentionType::Repost;
|
||||
}
|
||||
|
||||
return MentionType::Mention;
|
||||
}
|
||||
|
||||
private function parseAuthor(string $author) : ?string
|
||||
private function parseContent(string $target, Crawler $document, MentionType $type) : ?string
|
||||
{
|
||||
return match ($type) {
|
||||
MentionType::Like => "Liked this post",
|
||||
MentionType::Reply => $document->innerText(),
|
||||
MentionType::Repost => "Reposted this post",
|
||||
MentionType::Mention => $document->closest('a[href="' . $target . '"]')->innerText()
|
||||
};
|
||||
}
|
||||
|
||||
private function parseAuthor(Crawler $document) : ?string
|
||||
{
|
||||
$card = $document->filter('.p-author.h-card')->eq(0);
|
||||
|
||||
if ($card)
|
||||
{
|
||||
$name = $card->filter('.p-name')?->text();
|
||||
$url = $card->filter('.u-url')?->text();
|
||||
$photo = $card->filter('.u-photo')?->attr('src');
|
||||
|
||||
return implode(", ", [$name, $url, $photo]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user