rss-bridge-twitter.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. require_once('rss-bridge-lib.php');
  3. /**
  4. * RssBridgeTwitter
  5. * Based on https://github.com/mitsukarenai/twitterbridge-noapi
  6. */
  7. class RssBridgeTwitter extends RssBridgeAbstractClass
  8. {
  9. protected $bridgeName = 'Twitter Bridge';
  10. protected $bridgeURI = 'http://twitter.com';
  11. protected $bridgeDescription = 'Returns user timelines or keyword search from http://twitter.com without using their API.';
  12. protected $cacheDuration = 5; // 5 minutes
  13. protected function collectData($request) {
  14. $html = '';
  15. if (isset($request['q'])) { /* keyword search mode */
  16. $html = file_get_html('http://twitter.com/search/realtime?q='.urlencode($request['q']).'+include:retweets&src=typd') or $this->returnError('404 Not Found', 'ERROR: no results for this query.');
  17. } elseif (isset($request['u'])) { /* user timeline mode */
  18. $html = file_get_html('http://twitter.com/'.urlencode($request['u'])) or $this->returnError('404 Not Found', 'ERROR: requested username can\'t be found.');
  19. } else {
  20. $this->returnError('400 Bad Request', 'ERROR: You must specify a keyword (?q=...) or a Twitter username (?u=...).');
  21. }
  22. $this->items = Array();
  23. foreach($html->find('div.tweet') as $tweet) {
  24. $item['username'] = trim(substr($tweet->find('span.username', 0)->plaintext, 1)); // extract username and sanitize
  25. $item['fullname'] = $tweet->getAttribute('data-name'); // extract fullname (pseudonym)
  26. $item['avatar'] = $tweet->find('img', 0)->src; // get avatar link
  27. $item['id'] = $tweet->getAttribute('data-tweet-id'); // get TweetID
  28. $item['uri'] = 'https://twitter.com'.$tweet->find('a.details', 0)->getAttribute('href'); // get tweet link
  29. $item['timestamp'] = $tweet->find('span._timestamp', 0)->getAttribute('data-time'); // extract tweet timestamp
  30. $item['content'] = str_replace('href="/', 'href="https://twitter.com/', strip_tags($tweet->find('p.tweet-text', 0)->innertext, '<a>')); // extract tweet text
  31. $item['title'] = $item['fullname'] . ' (@'.$item['username'] . ') | ' . $item['content'];
  32. $this->items[] = $item;
  33. }
  34. }
  35. }
  36. $bridge = new RssBridgeTwitter();
  37. $bridge->process();
  38. ?>