GooglePlusPostBridge.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. class GooglePlusPostBridge extends BridgeAbstract{
  3. private $title;
  4. private $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. 'include_media' => array(
  16. 'name' => 'Include media',
  17. 'type' => 'checkbox',
  18. 'title' => 'Enable to include media in the feed content'
  19. )
  20. ));
  21. public function collectData(){
  22. $username = $this->getInput('username');
  23. // Usernames start with a + if it's not an ID
  24. if(!is_numeric($username) && substr($username, 0, 1) !== '+') {
  25. $username = '+' . $username;
  26. }
  27. $html = getSimpleHTMLDOM(static::URI . '/' . urlencode($username) . '/posts')
  28. or returnServerError('No results for this query.');
  29. $html = defaultLinkTo($html, static::URI);
  30. $this->title = $html->find('meta[property=og:title]', 0)->getAttribute('content');
  31. $this->url = $html->find('meta[property=og:url]', 0)->getAttribute('content');
  32. foreach($html->find('div[jsname=WsjYwc]') as $post) {
  33. $item = array();
  34. $item['author'] = $post->find('div div div div a', 0)->innertext;
  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. $message = $post->find('div[jsname=EjRJtf]', 0);
  44. // Empty messages are not supported right now
  45. if(!$message) {
  46. continue;
  47. }
  48. $item['content'] = '<div style="float: left; padding: 0 10px 10px 0;"><a href="'
  49. . $this->url
  50. . '"><img align="top" alt="'
  51. . $item['author']
  52. . '" src="'
  53. . $post->find('div img', 0)->src
  54. . '" /></a></div><div>'
  55. . trim(strip_tags($message, '<a><p><div><img>'))
  56. . '</div>';
  57. // Make title at least 50 characters long, but don't add '...' if it is shorter!
  58. if(strlen($message->plaintext) > 50) {
  59. $end = strpos($message->plaintext, ' ', 50);
  60. }
  61. if(strlen(substr($message->plaintext, 0, $end)) === strlen($message->plaintext)) {
  62. $item['title'] = $message->plaintext;
  63. } else {
  64. $item['title'] = substr($message->plaintext, 0, $end) . '...';
  65. }
  66. $media = $post->find('[jsname="MTOxpb"]', 0);
  67. if($media) {
  68. $item['enclosures'] = array();
  69. foreach($media->find('img') as $img) {
  70. $item['enclosures'][] = $this->fixImage($img)->src;
  71. }
  72. if($this->getInput('include_media') === true && count($item['enclosures'] > 0)) {
  73. $item['content'] .= '<div style="clear: both;"><a href="'
  74. . $item['enclosures'][0]
  75. . '"><img src="'
  76. . $item['enclosures'][0]
  77. . '" /></a></div>';
  78. }
  79. }
  80. // Add custom parameters (only useful for JSON or Plaintext)
  81. $item['fullname'] = $item['author'];
  82. $item['avatar'] = $post->find('div img', 0)->src;
  83. $item['id'] = $post->find('div div div', 0)->getAttribute('id');
  84. $item['content_simple'] = $message->plaintext;
  85. $this->items[] = $item;
  86. }
  87. }
  88. public function getName(){
  89. return $this->title ?: 'Google Plus Post Bridge';
  90. }
  91. public function getURI(){
  92. return $this->url ?: parent::getURI();
  93. }
  94. private function fixImage($img) {
  95. // There are certain images like .gif which link to a static picture and
  96. // get replaced dynamically via JS in the browser. If we want the "real"
  97. // image we need to account for that.
  98. $urlparts = parse_url($img->src);
  99. if(array_key_exists('host', $urlparts)) {
  100. // For some reason some URIs don't contain the scheme, assume https
  101. if(!array_key_exists('scheme', $urlparts)) {
  102. $urlparts['scheme'] = 'https';
  103. }
  104. $pathelements = explode('/', $urlparts['path']);
  105. switch($urlparts['host']) {
  106. case 'lh3.googleusercontent.com':
  107. if(pathinfo(end($pathelements), PATHINFO_EXTENSION)) {
  108. // The second to last element of the path specifies the
  109. // image format. The URL is still valid if we remove it.
  110. unset($pathelements[count($pathelements) - 2]);
  111. } elseif(strrpos(end($pathelements), '=') !== false) {
  112. // Some images go throug a proxy. For those images they
  113. // add size information after an equal sign.
  114. // Example: '=w530-h298-n'. Again this can safely be
  115. // removed to get the original image.
  116. $pathelements[count($pathelements) - 1] = substr(
  117. end($pathelements),
  118. 0,
  119. strrpos(end($pathelements), '=')
  120. );
  121. }
  122. break;
  123. }
  124. $urlparts['path'] = implode('/', $pathelements);
  125. }
  126. $img->src = $this->build_url($urlparts);
  127. return $img;
  128. }
  129. /**
  130. * From: https://gist.github.com/Ellrion/f51ba0d40ae1d62eeae44fd1adf7b704
  131. * slightly adjusted to work with PHP < 7.0
  132. * @param array $parts
  133. * @return string
  134. */
  135. private function build_url(array $parts)
  136. {
  137. $scheme = isset($parts['scheme']) ? ($parts['scheme'] . '://') : '';
  138. $host = isset($parts['host']) ? $parts['host'] : '';
  139. $port = isset($parts['port']) ? (':' . $parts['port']) : '';
  140. $user = isset($parts['user']) ? $parts['user'] : '';
  141. $pass = isset($parts['pass']) ? (':' . $parts['pass']) : '';
  142. $pass = ($user || $pass) ? ($pass . '@') : '';
  143. $path = isset($parts['path']) ? $parts['path'] : '';
  144. $query = isset($parts['query']) ? ('?' . $parts['query']) : '';
  145. $fragment = isset($parts['fragment']) ? ('#' . $parts['fragment']) : '';
  146. return implode('', [$scheme, $user, $pass, $host, $port, $path, $query, $fragment]);
  147. }
  148. }