123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?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 = array(
- 'By User' => array(
- 'u' => array(
- 'name' => 'Username',
- 'required' => true,
- 'title' => 'Username'
- )
- ),
- 'Search' => array(
- 's' => array(
- 'name' => 'Search',
- 'required' => true,
- 'title' => 'Search'
- )
- )
- );
- public function collectData() {
- $header = array(
- 'Authorization: Bearer ' . $this->getAPIKey()
- );
- if(!empty($this->getInput('u'))) {
- $postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header) or
- returnServerError('Unable to query Ello API.');
- } else {
- $postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header) or
- returnServerError('Unable to query Ello API.');
- }
- $postData = json_decode($postData);
- $count = 0;
- foreach($postData->posts as $post) {
- $item = array();
- $item['author'] = $this->getUsername($post, $postData);
- $item['timestamp'] = strtotime($post->created_at);
- $item['title'] = $this->findText($post->summary);
- $item['content'] = $this->getPostContent($post->body);
- $item['enclosures'] = $this->getEnclosures($post, $postData);
- $content = $post->body;
- $this->items[] = $item;
- $count += 1;
- }
- }
- public function findText($path) {
- foreach($path as $summaryElement) {
- if($summaryElement->kind == 'text') {
- return $summaryElement->data;
- }
- }
- return '';
- }
- public function getPostContent($path) {
- $content = '';
- foreach($path as $summaryElement) {
- if($summaryElement->kind == 'text') {
- $content .= $summaryElement->data;
- } elseif ($summaryElement->kind == 'image') {
- $alt = '';
- if(property_exists($summaryElement->data, 'alt')) {
- $alt = $summaryElement->data->alt;
- }
- $content .= '<img src="' . $summaryElement->data->url . '" alt="' . $alt . '" />';
- }
- }
- return $content;
- }
- public function getEnclosures($post, $postData) {
- $assets = [];
- foreach($post->links->assets as $asset) {
- foreach($postData->linked->assets as $assetLink) {
- if($asset == $assetLink->id) {
- $assets[] = $assetLink->attachment->original->url;
- break;
- }
- }
- }
- return $assets;
- }
- public function getUsername($post, $postData) {
- foreach($postData->linked->users as $user) {
- if($user->id == $post->links->author->id) {
- return $user->username;
- }
- }
- }
- public function getAPIKey() {
- $cache = Cache::create('FileCache');
- $cache->setPath(CACHE_DIR);
- $cache->setParameters(['key']);
- $key = $cache->loadData();
- 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);
- }
- return $key;
- }
- public function getName(){
- if(!is_null($this->getInput('u'))) {
- return $this->getInput('u') . ' - Ello Bridge';
- }
- return parent::getName();
- }
- }
|