TwitterBridge.php 2.9 KB

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