TwitterBridgeExtended.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. class TwitterBridgeExtended extends BridgeAbstract{
  3. public function loadMetadatas() {
  4. $this->maintainer = "mitsukarenai";
  5. $this->name = "Twitter Bridge Extended";
  6. $this->uri = "https://twitter.com/";
  7. $this->description = "(same as Twitter Bridge, but with avatar, replies and RTs)";
  8. $this->update = "2014-05-25";
  9. $this->parameters["By keyword or hashtag"] =
  10. '[
  11. {
  12. "name" : "Keyword or #hashtag",
  13. "identifier" : "q"
  14. }
  15. ]';
  16. $this->parameters["By username"] =
  17. '[
  18. {
  19. "name" : "username",
  20. "identifier" : "u"
  21. }
  22. ]';
  23. }
  24. public function collectData(array $param){
  25. $html = '';
  26. if (isset($param['q'])) { /* keyword search mode */
  27. $html = $this->file_get_html('https://twitter.com/search?q='.urlencode($param['q']).'&f=tweets') or $this->returnError('No results for this query.', 404);
  28. }
  29. elseif (isset($param['u'])) { /* user timeline mode */
  30. $html = $this->file_get_html('https://twitter.com/'.urlencode($param['u']).'/with_replies') or $this->returnError('Requested username can\'t be found.', 404);
  31. }
  32. else {
  33. $this->returnError('You must specify a keyword (?q=...) or a Twitter username (?u=...).', 400);
  34. }
  35. foreach($html->find('div.js-stream-tweet') as $tweet) {
  36. $item = new \Item();
  37. // extract username and sanitize
  38. $item->username = $tweet->getAttribute('data-screen-name');
  39. // extract fullname (pseudonym)
  40. $item->fullname = $tweet->getAttribute('data-name');
  41. // get avatar link
  42. $item->avatar = $tweet->find('img', 0)->src;
  43. // get TweetID
  44. $item->id = $tweet->getAttribute('data-tweet-id');
  45. // get tweet link
  46. $item->uri = 'https://twitter.com'.$tweet->find('a.js-permalink', 0)->getAttribute('href');
  47. // extract tweet timestamp
  48. $item->timestamp = $tweet->find('span.js-short-timestamp', 0)->getAttribute('data-time');
  49. // extract plaintext
  50. $item->content_simple = str_replace('href="/', 'href="https://twitter.com/', html_entity_decode(strip_tags($tweet->find('p.js-tweet-text', 0)->innertext, '<a>')));
  51. // processing content links
  52. foreach($tweet->find('a') as $link) {
  53. if($link->hasAttribute('data-expanded-url') ) {
  54. $link->href = $link->getAttribute('data-expanded-url');
  55. }
  56. $link->removeAttribute('data-expanded-url');
  57. $link->removeAttribute('data-query-source');
  58. $link->removeAttribute('rel');
  59. $link->removeAttribute('class');
  60. $link->removeAttribute('target');
  61. $link->removeAttribute('title');
  62. }
  63. // get tweet text
  64. $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>';
  65. // generate the title
  66. $item->title = $item->fullname . ' (@'. $item->username . ') | ' . $item->content_simple;
  67. // put out
  68. $this->items[] = $item;
  69. }
  70. }
  71. public function getName(){
  72. return 'Twitter Bridge Extended';
  73. }
  74. public function getURI(){
  75. return 'http://twitter.com';
  76. }
  77. public function getCacheDuration(){
  78. return 300; // 5 minutes
  79. }
  80. }