TwitterBridgeClean.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. //Based on https://github.com/mitsukarenai/twitterbridge-noapi
  3. class TwitterBridgeClean extends BridgeAbstract{
  4. private $request;
  5. public function loadMetadatas() {
  6. $this->maintainer = "vinzv";
  7. $this->name = "Twitter Bridge Clean";
  8. $this->uri = "http://twitter.com/";
  9. $this->description = "Returns user timelines without username in title or search results for keywords/hashtags (without using their API).";
  10. $this->update = "2015-03-07";
  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. $item->title = html_entity_decode($item->content,ENT_QUOTES,'UTF-8');
  62. $this->items[] = $item;
  63. }
  64. }
  65. public function getName(){
  66. return (!empty($this->request) ? $this->request .' - ' : '') .'Twitter Bridge';
  67. }
  68. public function getURI(){
  69. return 'http://twitter.com';
  70. }
  71. public function getCacheDuration(){
  72. return 300; // 5 minutes
  73. }
  74. }