ElloBridge.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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'] = $this->findText($post->summary);
  42. $item['content'] = $this->getPostContent($post->body);
  43. $item['enclosures'] = $this->getEnclosures($post, $postData);
  44. $content = $post->body;
  45. $this->items[] = $item;
  46. $count += 1;
  47. }
  48. }
  49. public function findText($path) {
  50. foreach($path as $summaryElement) {
  51. if($summaryElement->kind == 'text') {
  52. return $summaryElement->data;
  53. }
  54. }
  55. return '';
  56. }
  57. public function getPostContent($path) {
  58. $content = '';
  59. foreach($path as $summaryElement) {
  60. if($summaryElement->kind == 'text') {
  61. $content .= $summaryElement->data;
  62. } elseif ($summaryElement->kind == 'image') {
  63. $alt = '';
  64. if(property_exists($summaryElement->data, 'alt')) {
  65. $alt = $summaryElement->data->alt;
  66. }
  67. $content .= '<img src="' . $summaryElement->data->url . '" alt="' . $alt . '" />';
  68. }
  69. }
  70. return $content;
  71. }
  72. public function getEnclosures($post, $postData) {
  73. $assets = [];
  74. foreach($post->links->assets as $asset) {
  75. foreach($postData->linked->assets as $assetLink) {
  76. if($asset == $assetLink->id) {
  77. $assets[] = $assetLink->attachment->original->url;
  78. break;
  79. }
  80. }
  81. }
  82. return $assets;
  83. }
  84. public function getUsername($post, $postData) {
  85. foreach($postData->linked->users as $user) {
  86. if($user->id == $post->links->author->id) {
  87. return $user->username;
  88. }
  89. }
  90. }
  91. public function getAPIKey() {
  92. $cache = Cache::create('FileCache');
  93. $cache->setPath(CACHE_DIR);
  94. $cache->setParameters(['key']);
  95. $key = $cache->loadData();
  96. if($key == null) {
  97. $keyInfo = getContents(self::URI . 'api/webapp-token') or
  98. returnServerError('Unable to get token.');
  99. $key = json_decode($keyInfo)->token->access_token;
  100. $cache->saveData($key);
  101. }
  102. return $key;
  103. }
  104. public function getName(){
  105. if(!is_null($this->getInput('u'))) {
  106. return $this->getInput('u') . ' - Ello Bridge';
  107. }
  108. return parent::getName();
  109. }
  110. }