TwitterBridge.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. class TwitterBridge extends BridgeAbstract{
  3. public function loadMetadatas() {
  4. $this->maintainer = "mitsukarenai";
  5. $this->name = "Twitter Bridge";
  6. $this->uri = "https://twitter.com/";
  7. $this->description = "Returns tweets by keyword/hashtag or user name";
  8. $this->parameters["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. $this->parameters["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. $this->parameters["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 collectData(array $param){
  38. $html = '';
  39. if (isset($param['q'])) { /* keyword search mode */
  40. $html = $this->getSimpleHTMLDOM('https://twitter.com/search?q='.urlencode($param['q']).'&f=tweets') or $this->returnServerError('No results for this query.');
  41. }
  42. elseif (isset($param['u'])) { /* user timeline mode */
  43. $html = $this->getSimpleHTMLDOM('https://twitter.com/'.urlencode($param['u']).(isset($param['norep'])?'':'/with_replies')) or $this->returnServerError('Requested username can\'t be found.');
  44. }
  45. else {
  46. $this->returnClientError('You must specify a keyword (?q=...) or a Twitter username (?u=...).');
  47. }
  48. $hidePictures = false;
  49. if (isset($param['nopic']))
  50. $hidePictures = $param['nopic'] === 'on';
  51. foreach($html->find('div.js-stream-tweet') as $tweet) {
  52. $item = array();
  53. // extract username and sanitize
  54. $item['username'] = $tweet->getAttribute('data-screen-name');
  55. // extract fullname (pseudonym)
  56. $item['fullname'] = $tweet->getAttribute('data-name');
  57. // get author
  58. $item['author'] = $item['fullname'] . ' (@' . $item['username'] . ')';
  59. // get avatar link
  60. $item['avatar'] = $tweet->find('img', 0)->src;
  61. // get TweetID
  62. $item['id'] = $tweet->getAttribute('data-tweet-id');
  63. // get tweet link
  64. $item['uri'] = 'https://twitter.com'.$tweet->find('a.js-permalink', 0)->getAttribute('href');
  65. // extract tweet timestamp
  66. $item['timestamp'] = $tweet->find('span.js-short-timestamp', 0)->getAttribute('data-time');
  67. // generate the title
  68. $item['title'] = strip_tags(html_entity_decode($tweet->find('p.js-tweet-text', 0)->innertext,ENT_QUOTES,'UTF-8'));
  69. // processing content links
  70. foreach($tweet->find('a') as $link) {
  71. if($link->hasAttribute('data-expanded-url') ) {
  72. $link->href = $link->getAttribute('data-expanded-url');
  73. }
  74. $link->removeAttribute('data-expanded-url');
  75. $link->removeAttribute('data-query-source');
  76. $link->removeAttribute('rel');
  77. $link->removeAttribute('class');
  78. $link->removeAttribute('target');
  79. $link->removeAttribute('title');
  80. }
  81. // process emojis (reduce size)
  82. foreach($tweet->find('img.Emoji') as $img){
  83. $img->style .= ' height: 1em;';
  84. }
  85. // get tweet text
  86. $cleanedTweet = str_replace('href="/', 'href="https://twitter.com/', $tweet->find('p.js-tweet-text', 0)->innertext);
  87. // Add picture to content
  88. $picture_html = '';
  89. if(!$hidePictures){
  90. $picture_html = <<<EOD
  91. <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>
  92. EOD;
  93. }
  94. // add content
  95. $item['content'] = <<<EOD
  96. <div style="display: inline-block; vertical-align: top;">
  97. {$picture_html}
  98. </div>
  99. <div style="display: inline-block; vertical-align: top;">
  100. <blockquote>{$cleanedTweet}</blockquote>
  101. </div>
  102. EOD;
  103. // put out
  104. $this->items[] = $item;
  105. }
  106. }
  107. public function getCacheDuration(){
  108. return 300; // 5 minutes
  109. }
  110. }