1
0

TwitterBridge.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. public function collectData(array $param){
  13. $html = '';
  14. if (isset($param['q'])) { /* keyword search mode */
  15. $html = file_get_html('http://twitter.com/search/realtime?q='.urlencode($param['q']).'+include:retweets&src=typd') or $this->returnError('No results for this query.', 404);
  16. }
  17. elseif (isset($param['u'])) { /* user timeline mode */
  18. $html = file_get_html('http://twitter.com/'.urlencode($param['u'])) or $this->returnError('Requested username can\'t be found.', 404);
  19. }
  20. else {
  21. $this->returnError('You must specify a keyword (?q=...) or a Twitter username (?u=...).', 400);
  22. }
  23. foreach($html->find('div.tweet') as $tweet) {
  24. $item = new \Item();
  25. $item->username = trim(substr($tweet->find('span.username', 0)->plaintext, 1)); // extract username and sanitize
  26. $item->fullname = $tweet->getAttribute('data-name'); // extract fullname (pseudonym)
  27. $item->avatar = $tweet->find('img', 0)->src; // get avatar link
  28. $item->id = $tweet->getAttribute('data-tweet-id'); // get TweetID
  29. $item->uri = 'https://twitter.com'.$tweet->find('a.details', 0)->getAttribute('href'); // get tweet link
  30. $item->timestamp = $tweet->find('span._timestamp', 0)->getAttribute('data-time'); // extract tweet timestamp
  31. $item->content = str_replace('href="/', 'href="https://twitter.com/', strip_tags($tweet->find('p.tweet-text', 0)->innertext, '<a>')); // extract tweet text
  32. $item->title = $item->fullname . ' (@'. $item->username . ') | ' . $item->content;
  33. $this->items[] = $item;
  34. }
  35. }
  36. public function getName(){
  37. return 'Twitter Bridge';
  38. }
  39. public function getURI(){
  40. return 'http://twitter.com';
  41. }
  42. public function getCacheDuration(){
  43. return 300; // 5 minutes
  44. }
  45. }