TwitterBridgeCleanExtended.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. //Based on https://github.com/sebsauvage/rss-bridge/blob/master/bridges/TwitterBridgeClean.php by vinzv
  3. class TwitterBridgeCleanExtended extends BridgeAbstract{
  4. private $request;
  5. public function loadMetadatas() {
  6. $this->maintainer = "Max Mehl";
  7. $this->name = "Twitter Bridge Clean Extended";
  8. $this->uri = "http://twitter.com/";
  9. $this->description = "Returns user timelines showing RTs correctly or search results for keywords/hashtags (without using their API).";
  10. $this->update = "2016-01-27";
  11. $this->parameters["By keyword or hashtag"] =
  12. '[
  13. {
  14. "name" : "Keyword or #hashtag",
  15. "identifier" : "q"
  16. }
  17. ]';
  18. $this->parameters["By username"] =
  19. '[
  20. {
  21. "name" : "username",
  22. "identifier" : "u"
  23. }
  24. ]';
  25. }
  26. public function collectData(array $param){
  27. $html = '';
  28. if (isset($param['q'])) { /* keyword search mode */
  29. $this->request = $param['q'];
  30. $html = $this->file_get_html('https://twitter.com/search?q='.urlencode($this->request).'&f=tweets') or $this->returnError('No results for this query.', 404);
  31. }
  32. elseif (isset($param['u'])) { /* user timeline mode */
  33. $this->request = $param['u'];
  34. $html = $this->file_get_html('http://twitter.com/'.urlencode($this->request)) or $this->returnError('Requested username can\'t be found.', 404);
  35. }
  36. else {
  37. $this->returnError('You must specify a keyword (?q=...) or a Twitter username (?u=...).', 400);
  38. }
  39. foreach($html->find('div.js-stream-tweet') as $tweet) {
  40. $item = new \Item();
  41. $item->username = $tweet->getAttribute('data-screen-name'); // extract username and sanitize
  42. $item->fullname = $tweet->getAttribute('data-name'); // extract fullname (pseudonym)
  43. $item->avatar = $tweet->find('img', 0)->src; // get avatar link
  44. $item->id = $tweet->getAttribute('data-tweet-id'); // get TweetID
  45. $item->uri = 'https://twitter.com'.$tweet->find('a.js-permalink', 0)->getAttribute('href'); // get tweet link
  46. $item->timestamp = $tweet->find('span.js-short-timestamp', 0)->getAttribute('data-time'); // extract tweet timestamp
  47. // processing content links
  48. foreach($tweet->find('a') as $link) {
  49. if($link->hasAttribute('data-expanded-url') ) {
  50. $link->href = $link->getAttribute('data-expanded-url');
  51. }
  52. $link->removeAttribute('data-expanded-url');
  53. $link->removeAttribute('data-query-source');
  54. $link->removeAttribute('rel');
  55. $link->removeAttribute('class');
  56. $link->removeAttribute('target');
  57. $link->removeAttribute('title');
  58. $link->removeAttribute('dir');
  59. }
  60. $item->content = str_replace('pic.twitter.com', 'https://pic.twitter.com', strip_tags($tweet->find('p.js-tweet-text', 0)->innertext)); // extract tweet text
  61. if(isset($param['u'])) {
  62. if($item->username != $param['u']) {
  63. $item->content = '&#9851; @' . $item->username . ': ' . $item->content;
  64. }
  65. }
  66. $item->title = html_entity_decode($item->content,ENT_QUOTES,'UTF-8');
  67. $this->items[] = $item;
  68. }
  69. }
  70. public function getName(){
  71. return (!empty($this->request) ? $this->request : '');
  72. }
  73. public function getURI(){
  74. return 'http://twitter.com';
  75. }
  76. public function getCacheDuration(){
  77. return 300; // 5 minutes
  78. }
  79. }