1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
<?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',
'required' => true,
'exampleValue' => 'zteph',
'title' => 'Username'
]
],
'Search' => [
's' => [
'name' => 'Search',
'required' => true,
'exampleValue' => 'bird',
'title' => 'Search'
]
]
];
public function collectData()
{
$header = [
'Authorization: Bearer ' . $this->getAPIKey()
];
if (!empty($this->getInput('u'))) {
$postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header);
} else {
$postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header);
}
$postData = json_decode($postData);
$count = 0;
foreach ($postData->posts as $post) {
$item = [];
$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;
$content = $post->body;
$this->items[] = $item;
$count += 1;
}
}
private function findText($path)
{
foreach ($path as $summaryElement) {
if ($summaryElement->kind == 'text') {
return $summaryElement->data;
}
}
return '';
}
private 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;
}
private 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;
}
private function getUsername($post, $postData)
{
foreach ($postData->linked->users as $user) {
if ($user->id == $post->links->author->id) {
return $user->username;
}
}
}
private function getAPIKey()
{
$cacheKey = 'ElloBridge_key';
$apiKey = $this->cache->get($cacheKey);
if (!$apiKey) {
$keyInfo = getContents(self::URI . 'api/webapp-token');
$apiKey = json_decode($keyInfo)->token->access_token;
$ttl = 60 * 60 * 20;
$this->cache->set($cacheKey, $apiKey, $ttl);
}
return $apiKey;
}
public function getName()
{
if (!is_null($this->getInput('u'))) {
return $this->getInput('u') . ' - Ello Bridge';
}
return parent::getName();
}
}
|