GooglePlusPostBridge.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. $username = $this->getInput('username');
  18. // Usernames start with a + if it's not an ID
  19. if(!is_numeric($username) && substr($username, 0, 1) !== '+') {
  20. $username = '+' . $username;
  21. }
  22. // get content parsed
  23. $html = getSimpleHTMLDOMCached(self::URI . urlencode($username) . '/posts')
  24. or returnServerError('No results for this query.');
  25. // get title, url, ... there is a lot of intresting stuff in meta
  26. $this->_title = $html->find('meta[property=og:title]', 0)->getAttribute('content');
  27. $this->_url = $html->find('meta[property=og:url]', 0)->getAttribute('content');
  28. // I don't even know where to start with this discusting html...
  29. foreach($html->find('div[jsname=WsjYwc]') as $post) {
  30. $item = array();
  31. $item['author'] = $item['fullname'] = $post->find('div div div div a', 0)->innertext;
  32. $item['id'] = $post->find('div div div', 0)->getAttribute('id');
  33. $item['avatar'] = $post->find('div img', 0)->src;
  34. $item['uri'] = self::URI . $post->find('div div div a', 1)->href;
  35. $timestamp = $post->find('a.qXj2He span', 0);
  36. if($timestamp) {
  37. $item['timestamp'] = strtotime('+' . preg_replace(
  38. '/[^0-9A-Za-z]/',
  39. '',
  40. $timestamp->getAttribute('aria-label')));
  41. }
  42. // hashtag to treat : https://plus.google.com/explore/tag
  43. // $hashtags = array();
  44. // foreach($post->find('a.d-s') as $hashtag){
  45. // $hashtags[trim($hashtag->plaintext)] = self::URI . $hashtag->href;
  46. // }
  47. $item['content'] = '';
  48. // avatar display
  49. $item['content'] .= '<div style="float:left; margin: 0 0.5em 0.5em 0;"><a href="'
  50. . self::URI
  51. . urlencode($this->getInput('username'));
  52. $item['content'] .= '"><img align="top" alt="'
  53. . $item['author']
  54. . '" src="'
  55. . $item['avatar']
  56. . '" /></a></div>';
  57. $content = $post->find('div[jsname=EjRJtf]', 0);
  58. // extract plaintext
  59. $item['content_simple'] = $content->plaintext;
  60. $item['title'] = substr($item['content_simple'], 0, 72) . '...';
  61. // XXX ugly but I don't have any idea how to do a better stuff,
  62. // str_replace on link doesn't work as expected and ask too many checks
  63. foreach($content->find('a') as $link) {
  64. $hasHttp = strpos($link->href, 'http');
  65. $hasDoubleSlash = strpos($link->href, '//');
  66. if((!$hasHttp && !$hasDoubleSlash)
  67. || (false !== $hasHttp && strpos($link->href, 'http') != 0)
  68. || (false === $hasHttp && false !== $hasDoubleSlash && $hasDoubleSlash != 0)) {
  69. // skipp bad link, for some hashtag or other stuff
  70. if(strpos($link->href, '/') == 0) {
  71. $link->href = substr($link->href, 1);
  72. }
  73. $link->href = self::URI . $link->href;
  74. }
  75. }
  76. $content = $content->innertext;
  77. $item['content'] .= '<div style="margin-top: -1.5em">' . $content . '</div>';
  78. $item['content'] = trim(strip_tags($item['content'], '<a><p><div><img>'));
  79. $this->items[] = $item;
  80. }
  81. }
  82. public function getName(){
  83. return $this->_title ?: 'Google Plus Post Bridge';
  84. }
  85. public function getURI(){
  86. return $this->_url ?: parent::getURI();
  87. }
  88. }