TwitterBridge.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * RssBridgeTwitter
  4. * Based on https://github.com/mitsukarenai/twitterbridge-noapi
  5. *
  6. * @name Twitter Bridge
  7. * @description Returns user timelines or keyword/hashtag search results (without using their API).
  8. * @use1(q="keyword or #hashtag")
  9. * @use2(u="username")
  10. */
  11. class TwitterBridge extends BridgeAbstract{
  12. private $request;
  13. public function collectData(array $param){
  14. $html = '';
  15. if (isset($param['q'])) { /* keyword search mode */
  16. $this->request = $param['q'];
  17. $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);
  18. }
  19. elseif (isset($param['u'])) { /* user timeline mode */
  20. $this->request = $param['u'];
  21. $html = file_get_html('http://twitter.com/'.urlencode($this->request)) 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.tweet') as $tweet) {
  27. $item = new \Item();
  28. $item->username = trim(substr($tweet->find('span.username', 0)->plaintext, 1)); // extract username and sanitize
  29. $item->fullname = $tweet->getAttribute('data-name'); // extract fullname (pseudonym)
  30. $item->avatar = $tweet->find('img', 0)->src; // get avatar link
  31. $item->id = $tweet->getAttribute('data-tweet-id'); // get TweetID
  32. $item->uri = 'https://twitter.com'.$tweet->find('a.details', 0)->getAttribute('href'); // get tweet link
  33. $item->timestamp = $tweet->find('span._timestamp', 0)->getAttribute('data-time'); // extract tweet timestamp
  34. $item->content = str_replace('href="/', 'href="https://twitter.com/', strip_tags($tweet->find('p.tweet-text', 0)->innertext, '<a>')); // extract tweet text
  35. $item->title = $item->fullname . ' (@'. $item->username . ') | ' . $item->content;
  36. $this->items[] = $item;
  37. }
  38. }
  39. public function getName(){
  40. return (!empty($this->request) ? $this->request .' - ' : '') .'Twitter Bridge';
  41. }
  42. public function getURI(){
  43. return 'http://twitter.com';
  44. }
  45. public function getCacheDuration(){
  46. return 300; // 5 minutes
  47. }
  48. }