TwitterBridge.php 4.3 KB

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