GooglePlusPostBridge.php 4.1 KB

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