TwitterBridge.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. class TwitterBridge extends BridgeAbstract {
  3. const NAME = 'Twitter Bridge';
  4. const URI = 'https://twitter.com/';
  5. const CACHE_TIMEOUT = 300; // 5min
  6. const DESCRIPTION = 'returns tweets';
  7. const MAINTAINER = 'pmaziere';
  8. const PARAMETERS = array(
  9. 'global' => array(
  10. 'nopic' => array(
  11. 'name' => 'Hide profile pictures',
  12. 'type' => 'checkbox',
  13. 'title' => 'Activate to hide profile pictures in content'
  14. )
  15. ),
  16. 'By keyword or hashtag' => array(
  17. 'q' => array(
  18. 'name' => 'Keyword or #hashtag',
  19. 'required' => true,
  20. 'exampleValue' => 'rss-bridge, #rss-bridge',
  21. 'title' => 'Insert a keyword or hashtag'
  22. )
  23. ),
  24. 'By username' => array(
  25. 'u' => array(
  26. 'name' => 'username',
  27. 'required' => true,
  28. 'exampleValue' => 'sebsauvage',
  29. 'title' => 'Insert a user name'
  30. ),
  31. 'norep' => array(
  32. 'name' => 'Without replies',
  33. 'type' => 'checkbox',
  34. 'title' => 'Only return initial tweets'
  35. )
  36. )
  37. );
  38. public function getName(){
  39. switch($this->queriedContext){
  40. case 'By keyword or hashtag':
  41. $specific = 'search ';
  42. $param = 'q';
  43. break;
  44. case 'By username':
  45. $specific = '@';
  46. $param = 'u';
  47. break;
  48. }
  49. return 'Twitter ' . $specific . $this->getInput($param);
  50. }
  51. public function getURI(){
  52. switch($this->queriedContext){
  53. case 'By keyword or hashtag':
  54. return self::URI
  55. . 'search?q='
  56. . urlencode($this->getInput('q'))
  57. . '&f=tweets';
  58. case 'By username':
  59. return self::URI
  60. . urlencode($this->getInput('u'))
  61. . ($this->getInput('norep') ? '' : '/with_replies');
  62. }
  63. }
  64. public function collectData(){
  65. $html = '';
  66. $html = getSimpleHTMLDOM($this->getURI());
  67. if(!$html){
  68. switch($this->queriedContext){
  69. case 'By keyword or hashtag':
  70. returnServerError('No results for this query.');
  71. case 'By username':
  72. returnServerError('Requested username can\'t be found.');
  73. }
  74. }
  75. $hidePictures = $this->getInput('nopic');
  76. foreach($html->find('div.js-stream-tweet') as $tweet){
  77. $item = array();
  78. // extract username and sanitize
  79. $item['username'] = $tweet->getAttribute('data-screen-name');
  80. // extract fullname (pseudonym)
  81. $item['fullname'] = $tweet->getAttribute('data-name');
  82. // get author
  83. $item['author'] = $item['fullname'] . ' (@' . $item['username'] . ')';
  84. // get avatar link
  85. $item['avatar'] = $tweet->find('img', 0)->src;
  86. // get TweetID
  87. $item['id'] = $tweet->getAttribute('data-tweet-id');
  88. // get tweet link
  89. $item['uri'] = self::URI . $tweet->find('a.js-permalink', 0)->getAttribute('href');
  90. // extract tweet timestamp
  91. $item['timestamp'] = $tweet->find('span.js-short-timestamp', 0)->getAttribute('data-time');
  92. // generate the title
  93. $item['title'] = strip_tags(
  94. html_entity_decode(
  95. $tweet->find('p.js-tweet-text', 0)->innertext,
  96. ENT_QUOTES,
  97. 'UTF-8'
  98. )
  99. );
  100. // processing content links
  101. foreach($tweet->find('a') as $link){
  102. if($link->hasAttribute('data-expanded-url')){
  103. $link->href = $link->getAttribute('data-expanded-url');
  104. }
  105. $link->removeAttribute('data-expanded-url');
  106. $link->removeAttribute('data-query-source');
  107. $link->removeAttribute('rel');
  108. $link->removeAttribute('class');
  109. $link->removeAttribute('target');
  110. $link->removeAttribute('title');
  111. }
  112. // process emojis (reduce size)
  113. foreach($tweet->find('img.Emoji') as $img){
  114. $img->style .= ' height: 1em;';
  115. }
  116. // get tweet text
  117. $cleanedTweet = str_replace(
  118. 'href="/',
  119. 'href="' . self::URI,
  120. $tweet->find('p.js-tweet-text', 0)->innertext
  121. );
  122. // Add picture to content
  123. $picture_html = '';
  124. if(!$hidePictures){
  125. $picture_html = <<<EOD
  126. <a href="https://twitter.com/{$item['username']}">
  127. <img
  128. style="align: top; width:75 px; border: 1px solid black;"
  129. alt="{$item['username']}"
  130. src="{$item['avatar']}"
  131. title="{$item['fullname']}" />
  132. </a>
  133. EOD;
  134. }
  135. // add content
  136. $item['content'] = <<<EOD
  137. <div style="display: inline-block; vertical-align: top;">
  138. {$picture_html}
  139. </div>
  140. <div style="display: inline-block; vertical-align: top;">
  141. <blockquote>{$cleanedTweet}</blockquote>
  142. </div>
  143. EOD;
  144. // put out
  145. $this->items[] = $item;
  146. }
  147. }
  148. }