1
0

GooglePlusPostBridge.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class GooglePlusPostBridge extends BridgeAbstract{
  3. protected $_title;
  4. protected $_url;
  5. const MAINTAINER = 'Grummfy';
  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. ));
  16. public function collectData(){
  17. // get content parsed
  18. $html = getSimpleHTMLDOMCached(self::URI . urlencode($this->getInput('username')) . '/posts'
  19. // force language
  20. , 84600
  21. , false
  22. , stream_context_create(array(
  23. '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. // div[jsmodel=XNmfOc]
  31. foreach($html->find('div.yt') as $post){
  32. $item = array();
  33. // $item['content'] = $post->find('div.Al', 0)->innertext;
  34. $item['author'] = $item['fullname'] = $post->find('header.lea h3 a', 0)->innertext;
  35. $item['id'] = $post->getAttribute('id');
  36. $item['title'] = $item['fullname'] = $post->find('header.lea', 0)->plaintext;
  37. $item['avatar'] = $post->find('div.ys img', 0)->src;
  38. $item['uri'] = self::URI . $post->find('a.o-U-s', 0)->href;
  39. $item['timestamp'] = strtotime($post->find('a.o-U-s', 0)->plaintext);
  40. // hashtag to treat : https://plus.google.com/explore/tag
  41. $hashtags = array();
  42. foreach($post->find('a.d-s') as $hashtag){
  43. $hashtags[trim($hashtag->plaintext)] = self::URI . $hashtag->href;
  44. }
  45. $item['content'] = '';
  46. // avatar display
  47. $item['content'] .= '<div style="float:left; margin: 0 0.5em 0.5em 0;"><a href="'
  48. . self::URI
  49. . urlencode($this->getInput('username'));
  50. $item['content'] .= '"><img align="top" alt="'
  51. . $item['author']
  52. . '" src="'
  53. . $item['avatar']
  54. . '" /></a></div>';
  55. $content = $post->find('div.Al', 0);
  56. // XXX ugly but I don't have any idea how to do a better stuff,
  57. // str_replace on link doesn't work as expected and ask too many checks
  58. foreach($content->find('a') as $link){
  59. $hasHttp = strpos($link->href, 'http');
  60. $hasDoubleSlash = strpos($link->href, '//');
  61. if((!$hasHttp && !$hasDoubleSlash)
  62. || (false !== $hasHttp && strpos($link->href, 'http') != 0)
  63. || (false === $hasHttp && false !== $hasDoubleSlash && $hasDoubleSlash != 0)){
  64. // skipp bad link, for some hashtag or other stuff
  65. if(strpos($link->href, '/') == 0){
  66. $link->href = substr($link->href, 1);
  67. }
  68. $link->href = self::URI . $link->href;
  69. }
  70. }
  71. $content = $content->innertext;
  72. $item['content'] .= '<div style="margin-top: -1.5em">' . $content . '</div>';
  73. $item['content'] = trim(strip_tags($item['content'], '<a><p><div><img>'));
  74. // extract plaintext
  75. $item['content_simple'] = $post->find('div.Al', 0)->plaintext;
  76. $this->items[] = $item;
  77. }
  78. }
  79. public function getName(){
  80. return $this->_title ?: 'Google Plus Post Bridge';
  81. }
  82. public function getURI(){
  83. return $this->_url ?: self::URI;
  84. }
  85. }