TwitterBridge.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. //Based on https://github.com/mitsukarenai/twitterbridge-noapi
  3. class TwitterBridge extends BridgeAbstract{
  4. private $request;
  5. public function loadMetadatas() {
  6. $this->maintainer = "mitsukarenai";
  7. $this->name = "Twitter Bridge";
  8. $this->uri = "http://twitter.com/";
  9. $this->description = "Returns user timelines or keyword/hashtag search results (without using their API).";
  10. $this->update = "2014-05-25";
  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. }
  59. $item->content = str_replace('href="/', 'href="https://twitter.com/', strip_tags($tweet->find('p.js-tweet-text', 0)->innertext, '<a>')); // extract tweet text
  60. $item->title = $item->fullname . ' (@' . $item->username . ') | ' . html_entity_decode(strip_tags($item->content),ENT_QUOTES,'UTF-8');
  61. $this->items[] = $item;
  62. }
  63. }
  64. public function getName(){
  65. return (!empty($this->request) ? $this->request .' - ' : '') .'Twitter Bridge';
  66. }
  67. public function getURI(){
  68. return 'http://twitter.com';
  69. }
  70. public function getCacheDuration(){
  71. return 300; // 5 minutes
  72. }
  73. }