TwitterBridge.php 4.4 KB

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