ElloBridge.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. class ElloBridge extends BridgeAbstract {
  3. const MAINTAINER = 'teromene';
  4. const NAME = 'Ello Bridge';
  5. const URI = 'https://ello.co/';
  6. const CACHE_TIMEOUT = 4800; //2hours
  7. const DESCRIPTION = 'Returns the newest posts for Ello';
  8. const PARAMETERS = array(
  9. 'By User' => array(
  10. 'u' => array(
  11. 'name' => 'Username',
  12. 'required' => true,
  13. 'title' => 'Username'
  14. )
  15. ),
  16. 'Search' => array(
  17. 's' => array(
  18. 'name' => 'Search',
  19. 'required' => true,
  20. 'title' => 'Search'
  21. )
  22. )
  23. );
  24. public function collectData() {
  25. $header = array(
  26. 'Authorization: Bearer ' . $this->getAPIKey()
  27. );
  28. if(!empty($this->getInput('u'))) {
  29. $postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header) or
  30. returnServerError('Unable to query Ello API.');
  31. } else {
  32. $postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header) or
  33. returnServerError('Unable to query Ello API.');
  34. }
  35. $postData = json_decode($postData);
  36. $count = 0;
  37. foreach($postData->posts as $post) {
  38. $item = array();
  39. $item['author'] = $this->getUsername($post, $postData);
  40. $item['timestamp'] = strtotime($post->created_at);
  41. $item['title'] = strip_tags($this->findText($post->summary));
  42. $item['content'] = $this->getPostContent($post->body);
  43. $item['enclosures'] = $this->getEnclosures($post, $postData);
  44. $item['uri'] = self::URI . $item['author'] . '/post/' . $post->token;
  45. $content = $post->body;
  46. $this->items[] = $item;
  47. $count += 1;
  48. }
  49. }
  50. private function findText($path) {
  51. foreach($path as $summaryElement) {
  52. if($summaryElement->kind == 'text') {
  53. return $summaryElement->data;
  54. }
  55. }
  56. return '';
  57. }
  58. private function getPostContent($path) {
  59. $content = '';
  60. foreach($path as $summaryElement) {
  61. if($summaryElement->kind == 'text') {
  62. $content .= $summaryElement->data;
  63. } elseif ($summaryElement->kind == 'image') {
  64. $alt = '';
  65. if(property_exists($summaryElement->data, 'alt')) {
  66. $alt = $summaryElement->data->alt;
  67. }
  68. $content .= '<img src="' . $summaryElement->data->url . '" alt="' . $alt . '" />';
  69. }
  70. }
  71. return $content;
  72. }
  73. private function getEnclosures($post, $postData) {
  74. $assets = [];
  75. foreach($post->links->assets as $asset) {
  76. foreach($postData->linked->assets as $assetLink) {
  77. if($asset == $assetLink->id) {
  78. $assets[] = $assetLink->attachment->original->url;
  79. break;
  80. }
  81. }
  82. }
  83. return $assets;
  84. }
  85. private function getUsername($post, $postData) {
  86. foreach($postData->linked->users as $user) {
  87. if($user->id == $post->links->author->id) {
  88. return $user->username;
  89. }
  90. }
  91. }
  92. private function getAPIKey() {
  93. $cache = Cache::create('FileCache');
  94. $cache->setPath(CACHE_DIR);
  95. $cache->setParameters(['key']);
  96. $key = $cache->loadData();
  97. if($key == null) {
  98. $keyInfo = getContents(self::URI . 'api/webapp-token') or
  99. returnServerError('Unable to get token.');
  100. $key = json_decode($keyInfo)->token->access_token;
  101. $cache->saveData($key);
  102. }
  103. return $key;
  104. }
  105. public function getName(){
  106. if(!is_null($this->getInput('u'))) {
  107. return $this->getInput('u') . ' - Ello Bridge';
  108. }
  109. return parent::getName();
  110. }
  111. }