GooglePlusPostBridge.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. class GooglePlusPostBridge extends BridgeAbstract
  3. {
  4. protected $_title;
  5. protected $_url;
  6. const MAINTAINER = "Grummfy";
  7. const NAME = "Google Plus Post Bridge";
  8. const URI = "https://plus.google.com/";
  9. const CACHE_TIMEOUT = 600; //10min
  10. const DESCRIPTION = "Returns user public post (without API).";
  11. const PARAMETERS = array( array(
  12. 'username'=>array(
  13. 'name'=>'username or Id',
  14. 'required'=>true
  15. )
  16. ));
  17. public function collectData()
  18. {
  19. // get content parsed
  20. // $html = getSimpleHTMLDOM(__DIR__ . '/../posts2.html'
  21. $html = getSimpleHTMLDOM(self::URI . urlencode($this->getInput('username')) . '/posts'
  22. // force language
  23. , false, stream_context_create(array('http'=> array(
  24. 'header' => 'Accept-Language: fr,fr-be,fr-fr;q=0.8,en;q=0.4,en-us;q=0.2;*' . "\r\n"
  25. )))
  26. ) OR returnServerError('No results for this query.');
  27. // get title, url, ... there is a lot of intresting stuff in meta
  28. $this->_title = $html->find('meta[property]', 0)->getAttribute('content');
  29. $this->_url = $html->find('meta[itemprop=url]', 0)->getAttribute('content');
  30. // foreach ($html->find('meta') as $e)
  31. // {
  32. // $item = array();
  33. // $item['content'] = var_export($e->attr, true);
  34. // $this->items[] = $item;
  35. // }
  36. // div[jsmodel=XNmfOc]
  37. foreach($html->find('div.yt') as $post)
  38. {
  39. $item = array();
  40. // $item['content'] = $post->find('div.Al', 0)->innertext;
  41. $item['username'] = $item['fullname'] = $post->find('header.lea h3 a', 0)->innertext;
  42. $item['id'] = $post->getAttribute('id');
  43. // $item['title'] = $item['fullname'] = $post->find('header.lea', 0)->plaintext;
  44. $item['avatar'] = $post->find('div.ys img', 0)->src;
  45. // var_dump((($post->find('a.o-U-s', 0)->getAllAttributes())));
  46. $item['uri'] = self::URI . $post->find('a.o-U-s', 0)->href;
  47. $item['timestamp'] = strtotime($post->find('a.o-U-s', 0)->plaintext);
  48. $this->items[] = $item;
  49. // hashtag to treat : https://plus.google.com/explore/tag
  50. $hashtags = array();
  51. foreach($post->find('a.d-s') as $hashtag)
  52. {
  53. $hashtags[ trim($hashtag->plaintext) ] = self::URI . $hashtag->href;
  54. }
  55. $item['content'] = '';
  56. // avatar display
  57. $item['content'] .= '<div style="float:left; margin: 0 0.5em 0.5em 0;"><a href="' . self::URI . urlencode($this->getInput('username'));
  58. $item['content'] .= '"><img align="top" alt="avatar" src="' . $item['avatar'].'" />' . $item['username'] . '</a></div>';
  59. $content = $post->find('div.Al', 0);
  60. // alter link
  61. // $content = $content->innertext;
  62. // $content = str_replace('href="./', 'href="' . self::URI, $content);
  63. // $content = str_replace('href="photos', 'href="' . self::URI . 'photos', $content);
  64. // 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
  65. foreach($content->find('a') as $link)
  66. {
  67. $hasHttp = strpos($link->href, 'http');
  68. $hasDoubleSlash = strpos($link->href, '//');
  69. if ((!$hasHttp && !$hasDoubleSlash)
  70. || (false !== $hasHttp && strpos($link->href, 'http') != 0)
  71. || (false === $hasHttp && false !== $hasDoubleSlash && $hasDoubleSlash != 0))
  72. {
  73. // skipp bad link, for some hashtag or other stuff
  74. if (strpos($link->href, '/') == 0)
  75. {
  76. $link->href = substr($link->href, 1);
  77. }
  78. $link->href = self::URI . $link->href;
  79. }
  80. }
  81. $content = $content->innertext;
  82. $item['content'] .= '<div style="margin-top: -1.5em">' . $content . '</div>';
  83. // extract plaintext
  84. $item['content_simple'] = $post->find('div.Al', 0)->plaintext;
  85. }
  86. // $html->save(__DIR__ . '/../posts2.html');
  87. }
  88. public function getName()
  89. {
  90. return $this->_title ?: 'Google Plus Post Bridge';
  91. }
  92. public function getURI()
  93. {
  94. return $this->_url ?: self::URI;
  95. }
  96. }