GooglePlusPostBridge.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. class GooglePlusPostBridge extends BridgeAbstract{
  3. protected $_title;
  4. protected $_url;
  5. const MAINTAINER = 'Grummfy, logmanoriginal';
  6. const NAME = 'Google Plus Post Bridge';
  7. const URI = 'https://plus.google.com';
  8. const CACHE_TIMEOUT = 600; //10min
  9. const DESCRIPTION = 'Returns user public post (without API).';
  10. const PARAMETERS = array( array(
  11. 'username' => array(
  12. 'name' => 'username or Id',
  13. 'required' => true
  14. )
  15. ));
  16. public function collectData(){
  17. $username = $this->getInput('username');
  18. // Usernames start with a + if it's not an ID
  19. if(!is_numeric($username) && substr($username, 0, 1) !== '+') {
  20. $username = '+' . $username;
  21. }
  22. // get content parsed
  23. $html = getSimpleHTMLDOMCached(static::URI . '/' . urlencode($username) . '/posts')
  24. or returnServerError('No results for this query.');
  25. $html = defaultLinkTo($html, static::URI);
  26. // get title, url, ... there is a lot of intresting stuff in meta
  27. $this->_title = $html->find('meta[property=og:title]', 0)->getAttribute('content');
  28. $this->_url = $html->find('meta[property=og:url]', 0)->getAttribute('content');
  29. // I don't even know where to start with this discusting html...
  30. foreach($html->find('div[jsname=WsjYwc]') as $post) {
  31. $item = array();
  32. $item['author'] = $item['fullname'] = $post->find('div div div div a', 0)->innertext;
  33. $item['id'] = $post->find('div div div', 0)->getAttribute('id');
  34. $item['avatar'] = $post->find('div img', 0)->src;
  35. $item['uri'] = $post->find('div div div a', 1)->href;
  36. $timestamp = $post->find('a.qXj2He span', 0);
  37. if($timestamp) {
  38. $item['timestamp'] = strtotime('+' . preg_replace(
  39. '/[^0-9A-Za-z]/',
  40. '',
  41. $timestamp->getAttribute('aria-label')));
  42. }
  43. $item['content'] = '';
  44. // avatar display
  45. $item['content'] .= '<div style="float:left; margin: 0 0.5em 0.5em 0;"><a href="'
  46. . static::URI
  47. . '/'
  48. . urlencode($this->getInput('username'));
  49. $item['content'] .= '"><img align="top" alt="'
  50. . $item['author']
  51. . '" src="'
  52. . $item['avatar']
  53. . '" /></a></div>';
  54. $content = $post->find('div[jsname=EjRJtf]', 0);
  55. // extract plaintext
  56. $item['content_simple'] = $content->plaintext;
  57. $item['title'] = substr($item['content_simple'], 0, 72) . '...';
  58. $content = $content->innertext;
  59. $item['content'] .= '<div style="margin-top: -1.5em">' . $content . '</div>';
  60. $item['content'] = trim(strip_tags($item['content'], '<a><p><div><img>'));
  61. $media = $post->find('[jsname="MTOxpb"]', 0);
  62. if($media) {
  63. $item['enclosures'] = array();
  64. foreach($media->find('img') as $img) {
  65. $item['enclosures'][] = $this->fixImage($img)->src;
  66. }
  67. }
  68. $this->items[] = $item;
  69. }
  70. }
  71. public function getName(){
  72. return $this->_title ?: 'Google Plus Post Bridge';
  73. }
  74. public function getURI(){
  75. return $this->_url ?: parent::getURI();
  76. }
  77. private function fixImage($img) {
  78. // There are certain images like .gif which link to a static picture and
  79. // get replaced dynamically via JS in the browser. If we want the "real"
  80. // image we need to account for that.
  81. $urlparts = parse_url($img->src);
  82. if(array_key_exists('host', $urlparts)) {
  83. // For some reason some URIs don't contain the scheme, assume https
  84. if(!array_key_exists('scheme', $urlparts)) {
  85. $urlparts['scheme'] = 'https';
  86. }
  87. $pathelements = explode('/', $urlparts['path']);
  88. switch($urlparts['host']) {
  89. case 'lh3.googleusercontent.com':
  90. if(pathinfo(end($pathelements), PATHINFO_EXTENSION)) {
  91. // The second to last element of the path specifies the
  92. // image format. The URL is still valid if we remove it.
  93. unset($pathelements[count($pathelements) - 2]);
  94. } elseif(strrpos(end($pathelements), '=') !== false) {
  95. // Some images go throug a proxy. For those images they
  96. // add size information after an equal sign.
  97. // Example: '=w530-h298-n'. Again this can safely be
  98. // removed to get the original image.
  99. $pathelements[count($pathelements) - 1] = substr(
  100. end($pathelements),
  101. 0,
  102. strrpos(end($pathelements), '=')
  103. );
  104. }
  105. break;
  106. }
  107. $urlparts['path'] = implode('/', $pathelements);
  108. }
  109. echo $img->src . '<br>';
  110. $img->src = $this->build_url($urlparts);
  111. echo $img->src . '<br><br>';
  112. return $img;
  113. }
  114. /**
  115. * From: https://gist.github.com/Ellrion/f51ba0d40ae1d62eeae44fd1adf7b704
  116. * slightly adjusted to work with PHP < 7.0
  117. * @param array $parts
  118. * @return string
  119. */
  120. private function build_url(array $parts)
  121. {
  122. $scheme = isset($parts['scheme']) ? ($parts['scheme'] . '://') : '';
  123. $host = isset($parts['host']) ? $parts['host'] : '';
  124. $port = isset($parts['port']) ? (':' . $parts['port']) : '';
  125. $user = isset($parts['user']) ? $parts['user'] : '';
  126. $pass = isset($parts['pass']) ? (':' . $parts['pass']) : '';
  127. $pass = ($user || $pass) ? ($pass . '@') : '';
  128. $path = isset($parts['path']) ? $parts['path'] : '';
  129. $query = isset($parts['query']) ? ('?' . $parts['query']) : '';
  130. $fragment = isset($parts['fragment']) ? ('#' . $parts['fragment']) : '';
  131. return implode('', [$scheme, $user, $pass, $host, $port, $path, $query, $fragment]);
  132. }
  133. }