TwitterBridgeTweaked.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * RssBridgeTwitter
  4. * Based on https://github.com/mitsukarenai/twitterbridge-noapi
  5. * 2014-12-05
  6. *
  7. * @name Twitter Bridge Tweaked
  8. * @homepage https://twitter.com/
  9. * @description (same as Twitter Bridge Extended, but with cleaned title & content)
  10. * @maintainer kraoc
  11. * @use1(q="keyword or hashtag")
  12. * @use2(u="username")
  13. */
  14. class TwitterBridgeTweaked extends BridgeAbstract{
  15. private function containsTLD($string) {
  16. preg_match(
  17. "/(AC($|\/)|\.AD($|\/)|\.AE($|\/)|\.AERO($|\/)|\.AF($|\/)|\.AG($|\/)|\.AI($|\/)|\.AL($|\/)|\.AM($|\/)|\.AN($|\/)|\.AO($|\/)|\.AQ($|\/)|\.AR($|\/)|\.ARPA($|\/)|\.AS($|\/)|\.ASIA($|\/)|\.AT($|\/)|\.AU($|\/)|\.AW($|\/)|\.AX($|\/)|\.AZ($|\/)|\.BA($|\/)|\.BB($|\/)|\.BD($|\/)|\.BE($|\/)|\.BF($|\/)|\.BG($|\/)|\.BH($|\/)|\.BI($|\/)|\.BIZ($|\/)|\.BJ($|\/)|\.BM($|\/)|\.BN($|\/)|\.BO($|\/)|\.BR($|\/)|\.BS($|\/)|\.BT($|\/)|\.BV($|\/)|\.BW($|\/)|\.BY($|\/)|\.BZ($|\/)|\.CA($|\/)|\.CAT($|\/)|\.CC($|\/)|\.CD($|\/)|\.CF($|\/)|\.CG($|\/)|\.CH($|\/)|\.CI($|\/)|\.CK($|\/)|\.CL($|\/)|\.CM($|\/)|\.CN($|\/)|\.CO($|\/)|\.COM($|\/)|\.COOP($|\/)|\.CR($|\/)|\.CU($|\/)|\.CV($|\/)|\.CX($|\/)|\.CY($|\/)|\.CZ($|\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XN--0ZWM56D($|\/)|\.XN--11B5BS3A9AJ6G($|\/)|\.XN--80AKHBYKNJ4F($|\/)|\.XN--9T4B11YI5A($|\/)|\.XN--DEBA0AD($|\/)|\.XN--G6W251D($|\/)|\.XN--HGBK6AJ7F53BBA($|\/)|\.XN--HLCJ6AYA9ESC7A($|\/)|\.XN--JXALPDLP($|\/)|\.XN--KGBECHTV($|\/)|\.XN--ZCKZAH($|\/)|\.YE($|\/)|\.YT($|\/)|\.YU($|\/)|\.ZA($|\/)|\.ZM($|\/)|\.ZW)/i",
  18. $string,
  19. $M
  20. );
  21. $has_tld = (count($M) > 0) ? true : false;
  22. return $has_tld;
  23. }
  24. private function cleaner($url) {
  25. $U = explode(' ', $url);
  26. $W =array();
  27. foreach ($U as $k => $u) {
  28. if (stristr($u,".")) { //only preg_match if there is a dot
  29. if ($this->containsTLD($u) === true) {
  30. unset($U[$k]);
  31. return $this->cleaner( implode(' ', $U) );
  32. }
  33. }
  34. }
  35. return implode(' ', $U);
  36. }
  37. // (c) Kraoc / urlclean
  38. // https://github.com/kraoc/Leed-market/blob/master/urlclean/urlclean.plugin.disabled.php
  39. private function resolve_url($link) {
  40. // fallback to crawl to real url (slowest method and unsecure to privacy)
  41. if (function_exists('curl_init') && !ini_get('safe_mode')) {
  42. curl_setopt($ch, CURLOPT_USERAGENT, $ua);
  43. curl_setopt($ch, CURLOPT_URL, $link);
  44. curl_setopt($ch, CURLOPT_HEADER, true);
  45. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  47. // >>> anonimization
  48. curl_setopt($ch, CURLOPT_COOKIESESSION, true);
  49. curl_setopt($ch, CURLOPT_REFERER, '');
  50. // <<< anonimization
  51. $ch = curl_init();
  52. $ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.16 (KHTML, like Gecko) Chrome/24.0.1304.0 Safari/537.16';
  53. $a = curl_exec($ch);
  54. $link = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  55. }
  56. $link = preg_replace("/[&#?]xtor=(.)+/", "", $link); // remove: xtor
  57. $link = preg_replace("/utm_([^&#]|(&amp;))+&*/", "", $link); // remove: utm_
  58. // cleanup end of url
  59. $link = preg_replace("/\?&/", "", $link);
  60. if (isset($link[strlen($link) -1])){
  61. if ($link[strlen($link) -1] == '?')
  62. $link = substr($link, 0, strlen($link) -1);
  63. }
  64. return $link;
  65. }
  66. public function collectData(array $param){
  67. $html = '';
  68. if (isset($param['q'])) { /* keyword search mode */
  69. $html = file_get_html('https://twitter.com/search/realtime?q='.urlencode($param['q']).'+include:retweets&src=typd') or $this->returnError('No results for this query.', 404);
  70. }
  71. elseif (isset($param['u'])) { /* user timeline mode */
  72. $html = file_get_html('https://twitter.com/'.urlencode($param['u']).'/with_replies') or $this->returnError('Requested username can\'t be found.', 404);
  73. }
  74. else {
  75. $this->returnError('You must specify a keyword (?q=...) or a Twitter username (?u=...).', 400);
  76. }
  77. foreach($html->find('div.js-stream-tweet') as $tweet) {
  78. $item = new \Item();
  79. // extract username and sanitize
  80. $item->username = $tweet->getAttribute('data-screen-name');
  81. // extract fullname (pseudonym)
  82. $item->fullname = $tweet->getAttribute('data-name');
  83. // get avatar link
  84. $item->avatar = $tweet->find('img', 0)->src;
  85. // get TweetID
  86. $item->id = $tweet->getAttribute('data-tweet-id');
  87. // get tweet link
  88. $item->uri = 'https://twitter.com'.$tweet->find('a.js-permalink', 0)->getAttribute('href');
  89. // extract tweet timestamp
  90. $item->timestamp = $tweet->find('span.js-short-timestamp', 0)->getAttribute('data-time');
  91. // extract plaintext
  92. $item->content_simple = str_replace('href="/', 'href="https://twitter.com/', html_entity_decode(strip_tags($tweet->find('p.js-tweet-text', 0)->innertext, '<a>')));
  93. // processing content links
  94. foreach($tweet->find('a') as $link) {
  95. if($link->hasAttribute('data-expanded-url') ) {
  96. $link->href = $link->getAttribute('data-expanded-url');
  97. }
  98. $link->removeAttribute('data-expanded-url');
  99. $link->removeAttribute('data-query-source');
  100. $link->removeAttribute('rel');
  101. $link->removeAttribute('class');
  102. $link->removeAttribute('target');
  103. $link->removeAttribute('title');
  104. }
  105. // get tweet text
  106. $item->content = '<a href="https://twitter.com/'.$item->username.'"><img style="align:top;width:75px;" alt="avatar" src="'.$item->avatar.'" />'.$item->username.'</a> '.$item->fullname.'<br/><blockquote>'.str_replace('href="/', 'href="https://twitter.com/', $tweet->find('p.js-tweet-text', 0)->innertext).'</blockquote>';
  107. // generate the title
  108. // $item->title = $item->fullname . ' (@'. $item->username . ') | ' . $item->content_simple;
  109. $item->title = $item->content_simple;
  110. $item->title = preg_replace('|https?://www\.[a-z\.0-9]+|i', '', $item->title); // remove http(s) links
  111. $item->title = preg_replace('|www\.[a-z\.0-9]+|i', '', $item->title); // remove www. links
  112. $item->title = $this->cleaner($item->title); // remove all remaining links
  113. $item->title = trim($item->title); // remove extra spaces at beginning and end
  114. // convert all content links to real ones
  115. $regex = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  116. $item->content = preg_replace_callback($regex, function($url) {
  117. // do stuff with $url[0] here
  118. return $this->resolve_url($url[0]);
  119. }, $item->content);
  120. // put out
  121. $this->items[] = $item;
  122. }
  123. }
  124. public function getName(){
  125. return 'Twitter Bridge Tweaked';
  126. }
  127. public function getURI(){
  128. return 'http://twitter.com';
  129. }
  130. public function getCacheDuration(){
  131. return 300; // 5 minutes
  132. }
  133. public function getUsername(){
  134. return $this->items[0]->username;
  135. }
  136. }