fix: use parlers new api (#3061)

This commit is contained in:
Dag 2022-09-24 00:02:19 +02:00 committed by GitHub
parent aacba5b1a8
commit 8cc5e44be6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 45 deletions

View File

@ -22,59 +22,32 @@ final class ParlerBridge extends BridgeAbstract
public function collectData()
{
$user = trim($this->getInput('user'));
if (preg_match('#^https?://parler\.com/(\w+)#i', $user, $m)) {
$user = $m[1];
}
$posts = $this->fetchParlerProfileFeed($user);
foreach ($posts as $post) {
// For some reason, the post data is placed inside primary attribute
$primary = $post->primary;
$json = getContents(sprintf('https://api.parler.com/v0/public/user/%s/feed/?page=1&limit=20&media_only=0', $user));
$response = Json::decode($json, false);
$data = $response->data ?? null;
if (!$data) {
throw new \Exception('The returned data is empty');
}
foreach ($data as $post) {
$item = [
'title' => mb_substr($primary->body, 0, 100),
'uri' => sprintf('https://parler.com/feed/%s', $primary->uuid),
'author' => $primary->username,
'uid' => $primary->uuid,
'content' => nl2br($primary->full_body),
'title' => $post->body,
'uri' => sprintf('https://parler.com/feed/%s', $post->postuuid),
'author' => $post->user->username,
'uid' => $post->postuuid,
'content' => $post->body,
];
$date = DateTimeImmutable::createFromFormat('m/d/YH:i A', $primary->date_str . $primary->time_str);
if ($date) {
$item['timestamp'] = $date->getTimestamp();
} else {
Debug::log(sprintf('Unable to parse data from Parler.com: "%s"', $date));
$date = $post->date_created;
$createdAt = date_create($date);
if ($createdAt) {
$item['timestamp'] = $createdAt->getTimestamp();
}
if (isset($primary->image)) {
$item['enclosures'][] = $primary->image;
$item['content'] .= sprintf('<img loading="lazy" src="%s">', $primary->image);
if (isset($post->image)) {
$item['content'] .= sprintf('<img loading="lazy" src="%s">', $post->image);
}
$this->items[] = $item;
}
}
private function fetchParlerProfileFeed(string $user): array
{
$json = getContents('https://parler.com/open-api/ProfileFeedEndpoint.php', [], [
CURLOPT_POSTFIELDS => http_build_query([
'user' => $user,
'page' => '1',
]),
]);
$response = json_decode($json);
if ($response === false) {
throw new \Exception('Unable to decode json from Parler');
}
if ($response->status !== 'ok') {
throw new \Exception('Did not get OK from Parler');
}
if ($response->data === []) {
throw new \Exception('Unknown Parler username');
}
return $response->data;
}
}