GooglePlusPostBridge.php 4.1 KB

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