rss-bridge/bridges/ElloBridge.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
4 KiB
PHP
Raw Normal View History

2018-05-05 20:06:27 +03:00
<?php
class ElloBridge extends BridgeAbstract
{
const MAINTAINER = 'teromene';
const NAME = 'Ello Bridge';
const URI = 'https://ello.co/';
const CACHE_TIMEOUT = 4800; //2hours
const DESCRIPTION = 'Returns the newest posts for Ello';
const PARAMETERS = [
'By User' => [
'u' => [
'name' => 'Username',
2018-05-05 20:06:27 +03:00
'required' => true,
'exampleValue' => 'zteph',
2018-05-05 20:06:27 +03:00
'title' => 'Username'
]
],
2018-05-05 20:06:27 +03:00
'Search' => [
's' => [
2018-05-05 20:06:27 +03:00
'name' => 'Search',
'required' => true,
'exampleValue' => 'bird',
'title' => 'Search'
]
]
2018-05-05 20:06:27 +03:00
];
2018-05-05 20:06:27 +03:00
public function collectData()
{
$header = [
'Authorization: Bearer ' . $this->getAPIKey()
];
2018-05-05 20:06:27 +03:00
if (!empty($this->getInput('u'))) {
2018-07-19 18:07:54 +03:00
$postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header) or
returnServerError('Unable to query Ello API.');
} else {
2018-05-05 20:06:27 +03:00
$postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header) or
returnServerError('Unable to query Ello API.');
}
2018-05-05 20:06:27 +03:00
$postData = json_decode($postData);
$count = 0;
foreach ($postData->posts as $post) {
$item = [];
2018-05-05 20:06:27 +03:00
$item['author'] = $this->getUsername($post, $postData);
$item['timestamp'] = strtotime($post->created_at);
$item['title'] = strip_tags($this->findText($post->summary));
$item['content'] = $this->getPostContent($post->body);
$item['enclosures'] = $this->getEnclosures($post, $postData);
$item['uri'] = self::URI . $item['author'] . '/post/' . $post->token;
2018-08-05 16:53:45 +03:00
$content = $post->body;
2018-05-05 20:06:27 +03:00
$this->items[] = $item;
$count += 1;
}
}
2018-08-05 16:53:45 +03:00
private function findText($path)
{
2018-05-05 20:06:27 +03:00
foreach ($path as $summaryElement) {
if ($summaryElement->kind == 'text') {
return $summaryElement->data;
}
}
return '';
}
2018-08-05 16:53:45 +03:00
private function getPostContent($path)
{
2018-05-05 20:06:27 +03:00
$content = '';
foreach ($path as $summaryElement) {
if ($summaryElement->kind == 'text') {
$content .= $summaryElement->data;
} elseif ($summaryElement->kind == 'image') {
$alt = '';
2018-05-05 20:06:27 +03:00
if (property_exists($summaryElement->data, 'alt')) {
$alt = $summaryElement->data->alt;
}
$content .= '<img src="' . $summaryElement->data->url . '" alt="' . $alt . '" />';
}
}
2018-05-05 20:06:27 +03:00
return $content;
}
2018-08-05 16:53:45 +03:00
private function getEnclosures($post, $postData)
{
$assets = [];
2018-05-05 20:06:27 +03:00
foreach ($post->links->assets as $asset) {
foreach ($postData->linked->assets as $assetLink) {
if ($asset == $assetLink->id) {
$assets[] = $assetLink->attachment->original->url;
break;
}
}
}
2018-05-05 20:06:27 +03:00
return $assets;
}
2018-08-05 16:53:45 +03:00
private function getUsername($post, $postData)
{
2018-05-05 20:06:27 +03:00
foreach ($postData->linked->users as $user) {
if ($user->id == $post->links->author->id) {
return $user->username;
}
}
}
2018-08-05 16:53:45 +03:00
private function getAPIKey()
{
$cache = RssBridge::getCache();
$cache->setScope('ElloBridge');
$cache->setKey(['key']);
2018-05-05 20:06:27 +03:00
$key = $cache->loadData();
2018-05-05 20:06:27 +03:00
if ($key == null) {
$keyInfo = getContents(self::URI . 'api/webapp-token') or
returnServerError('Unable to get token.');
$key = json_decode($keyInfo)->token->access_token;
$cache->saveData($key);
}
2018-05-05 20:06:27 +03:00
return $key;
}
2018-05-05 20:06:27 +03:00
public function getName()
{
if (!is_null($this->getInput('u'))) {
return $this->getInput('u') . ' - Ello Bridge';
}
2018-05-05 20:06:27 +03:00
return parent::getName();
}
}