GooglePlusPostBridge.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Google Plus Post Bridge
  4. * Freely inspired by tweeter bridge
  5. * 2014-07-20
  6. *
  7. * @name Google Plus Post Bridge
  8. * @homepage http://plus.google.com/
  9. * @description Returns user public post (without API).
  10. * @maintainer Grummfy
  11. * @use1(username="usernameOrId")
  12. */
  13. class GooglePlusPostBridge extends BridgeAbstract
  14. {
  15. protected $_title;
  16. protected $_url;
  17. const GOOGLE_PLUS_BASE_URL = 'https://plus.google.com/';
  18. public function collectData(array $param)
  19. {
  20. if (!isset($param['username']))
  21. {
  22. $this->returnError('You must specify a username (?username=...).', 400);
  23. }
  24. $this->request = $param['username'];
  25. // get content parsed
  26. // $html = file_get_html(__DIR__ . '/../posts2.html'
  27. $html = file_get_html(self::GOOGLE_PLUS_BASE_URL . urlencode($this->request) . '/posts'
  28. // force language
  29. , false, stream_context_create(array('http'=> array(
  30. 'header' => 'Accept-Language: fr,fr-be,fr-fr;q=0.8,en;q=0.4,en-us;q=0.2;*' . "\r\n"
  31. )))
  32. ) OR $this->returnError('No results for this query.', 404);
  33. // get title, url, ... there is a lot of intresting stuff in meta
  34. $this->_title = $html->find('meta[property]', 0)->getAttribute('content');
  35. $this->_url = $html->find('meta[itemprop=url]', 0)->getAttribute('content');
  36. // foreach ($html->find('meta') as $e)
  37. // {
  38. // $item = new \Item();
  39. // $item->content = var_export($e->attr, true);
  40. // $this->items[] = $item;
  41. // }
  42. // div[jsmodel=XNmfOc]
  43. foreach($html->find('div.yt') as $post)
  44. {
  45. $item = new \Item();
  46. // $item->content = $post->find('div.Al', 0)->innertext;
  47. $item->username = $item->fullname = $post->find('header.lea h3 a', 0)->innertext;
  48. $item->id = $post->getAttribute('id');
  49. // $item->title = $item->fullname = $post->find('header.lea', 0)->plaintext;
  50. $item->avatar = $post->find('div.ys img', 0)->src;
  51. // var_dump((($post->find('a.o-U-s', 0)->getAllAttributes())));
  52. $item->uri = $post->find('a.o-U-s', 0)->href;
  53. $item->timestamp = strtotime($post->find('a.o-U-s', 0)->plaintext);
  54. $this->items[] = $item;
  55. // hashtag to treat : https://plus.google.com/explore/tag
  56. $hashtags = array();
  57. foreach($post->find('a.d-s') as $hashtag)
  58. {
  59. $hashtags[ trim($hashtag->plaintext) ] = self::GOOGLE_PLUS_BASE_URL . $hashtag->href;
  60. }
  61. $item->content = '';
  62. // avatar display
  63. $item->content .= '<div style="float:left; margin: 0 0.5em 0.5em 0;"><a href="' . self::GOOGLE_PLUS_BASE_URL . urlencode($this->request);
  64. $item->content .= '"><img align="top" alt="avatar" src="' . $item->avatar.'" />' . $item->username . '</a></div>';
  65. $content = $post->find('div.Al', 0);
  66. // alter link
  67. // $content = $content->innertext;
  68. // $content = str_replace('href="./', 'href="' . self::GOOGLE_PLUS_BASE_URL, $content);
  69. // $content = str_replace('href="photos', 'href="' . self::GOOGLE_PLUS_BASE_URL . 'photos', $content);
  70. // XXX ugly but I don't have any idea how to do a better stuff, str_replace on link doesn't work as expected and ask too many checks
  71. foreach($content->find('a') as $link)
  72. {
  73. $hasHttp = strpos($link->href, 'http');
  74. $hasDoubleSlash = strpos($link->href, '//');
  75. if ((!$hasHttp && !$hasDoubleSlash)
  76. || (false !== $hasHttp && strpos($link->href, 'http') != 0)
  77. || (false === $hasHttp && false !== $hasDoubleSlash && $hasDoubleSlash != 0))
  78. {
  79. // skipp bad link, for some hashtag or other stuff
  80. if (strpos($link->href, '/') == 0)
  81. {
  82. $link->href = substr($link->href, 1);
  83. }
  84. $link->href = self::GOOGLE_PLUS_BASE_URL . $link->href;
  85. }
  86. }
  87. $content = $content->innertext;
  88. $item->content .= '<div style="margin-top: -1.5em">' . $content . '</div>';
  89. // extract plaintext
  90. $item->content_simple = $post->find('div.Al', 0)->plaintext;
  91. }
  92. // $html->save(__DIR__ . '/../posts2.html');
  93. }
  94. public function getName()
  95. {
  96. return $this->_title ?: 'Google Plus Post Bridge';
  97. }
  98. public function getURI()
  99. {
  100. return $this->_url ?: 'http://plus.google.com/';
  101. }
  102. public function getCacheDuration()
  103. {
  104. return 1; // 600; // 10 minutes
  105. }
  106. }