1
0

TwitterBridgeClean.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * RssBridgeTwitterClean
  4. * Based on https://github.com/mitsukarenai/twitterbridge-noapi
  5. *
  6. * @name Twitter Bridge Clean
  7. * @homepage http://twitter.com/
  8. * @description Returns user timelines without username in title or search results for keywords/hashtags (without using their API).
  9. * @maintainer vinzv
  10. * @update 2015-03-07
  11. * @use1(q="keyword or #hashtag")
  12. * @use2(u="username")
  13. */
  14. class TwitterBridgeClean 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. $link->removeAttribute('dir');
  49. }
  50. $item->content = str_replace('pic.twitter.com', 'https://pic.twitter.com', strip_tags($tweet->find('p.js-tweet-text', 0)->innertext)); // extract tweet text
  51. $item->title = $item->content;
  52. $this->items[] = $item;
  53. }
  54. }
  55. public function getName(){
  56. return (!empty($this->request) ? $this->request .' - ' : '') .'Twitter Bridge';
  57. }
  58. public function getURI(){
  59. return 'http://twitter.com';
  60. }
  61. public function getCacheDuration(){
  62. return 300; // 5 minutes
  63. }
  64. }