GooglePlusPostBridge.php 3.8 KB

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