TwitterBridgeExtended.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * RssBridgeTwitter
  4. * Based on https://github.com/mitsukarenai/twitterbridge-noapi
  5. * 2014-05-25
  6. *
  7. * @name Twitter Bridge Extended
  8. * @homepage https://twitter.com/
  9. * @description (same as Twitter Bridge, but with avatar, replies and RTs)
  10. * @maintainer mitsukarenai
  11. * @use1(q="keyword or hashtag")
  12. * @use2(u="username")
  13. */
  14. class TwitterBridgeExtended extends BridgeAbstract{
  15. public function collectData(array $param){
  16. $html = '';
  17. if (isset($param['q'])) { /* keyword search mode */
  18. $html = file_get_html('https://twitter.com/search/realtime?q='.urlencode($param['q']).'+include:retweets&src=typd') or $this->returnError('No results for this query.', 404);
  19. }
  20. elseif (isset($param['u'])) { /* user timeline mode */
  21. $html = file_get_html('https://twitter.com/'.urlencode($param['u']).'/with_replies') or $this->returnError('Requested username can\'t be found.', 404);
  22. }
  23. else {
  24. $this->returnError('You must specify a keyword (?q=...) or a Twitter username (?u=...).', 400);
  25. }
  26. foreach($html->find('div.js-stream-tweet') as $tweet) {
  27. $item = new \Item();
  28. // extract username and sanitize
  29. $item->username = $tweet->getAttribute('data-screen-name');
  30. // extract fullname (pseudonym)
  31. $item->fullname = $tweet->getAttribute('data-name');
  32. // get avatar link
  33. $item->avatar = $tweet->find('img', 0)->src;
  34. // get TweetID
  35. $item->id = $tweet->getAttribute('data-tweet-id');
  36. // get tweet link
  37. $item->uri = 'https://twitter.com'.$tweet->find('a.js-permalink', 0)->getAttribute('href');
  38. // extract tweet timestamp
  39. $item->timestamp = $tweet->find('span.js-short-timestamp', 0)->getAttribute('data-time');
  40. // extract plaintext
  41. $item->content_simple = str_replace('href="/', 'href="https://twitter.com/', html_entity_decode(strip_tags($tweet->find('p.js-tweet-text', 0)->innertext, '<a>')));
  42. // processing content links
  43. foreach($tweet->find('a') as $link) {
  44. if($link->hasAttribute('data-expanded-url') ) {
  45. $link->href = $link->getAttribute('data-expanded-url');
  46. }
  47. $link->removeAttribute('data-expanded-url');
  48. $link->removeAttribute('data-query-source');
  49. $link->removeAttribute('rel');
  50. $link->removeAttribute('class');
  51. $link->removeAttribute('target');
  52. $link->removeAttribute('title');
  53. }
  54. // get tweet text
  55. $item->content = '<a href="https://twitter.com/'.$item->username.'"><img style="align:top;width:75px;" alt="avatar" src="'.$item->avatar.'" />'.$item->username.'</a> '.$item->fullname.'<br/><blockquote>'.str_replace('href="/', 'href="https://twitter.com/', $tweet->find('p.js-tweet-text', 0)->innertext).'</blockquote>';
  56. // generate the title
  57. $item->title = $item->fullname . ' (@'. $item->username . ') | ' . $item->content_simple;
  58. // put out
  59. $this->items[] = $item;
  60. }
  61. }
  62. public function getName(){
  63. return 'Twitter Bridge Extended';
  64. }
  65. public function getURI(){
  66. return 'http://twitter.com';
  67. }
  68. public function getCacheDuration(){
  69. return 300; // 5 minutes
  70. }
  71. }