[GooglePlusPostBridge] code simplification

Signed-off-by: Pierre Mazière <pierre.maziere@gmx.com>
This commit is contained in:
Pierre Mazière 2016-08-29 00:36:07 +02:00
parent 88f5014a16
commit 2496f1374d

View file

@ -6,7 +6,7 @@ class GooglePlusPostBridge extends BridgeAbstract
public $maintainer = "Grummfy"; public $maintainer = "Grummfy";
public $name = "Google Plus Post Bridge"; public $name = "Google Plus Post Bridge";
public $uri = "http://plus.google.com/"; public $uri = "https://plus.google.com/";
public $description = "Returns user public post (without API)."; public $description = "Returns user public post (without API).";
public $parameters = array( array( public $parameters = array( array(
@ -16,20 +16,11 @@ class GooglePlusPostBridge extends BridgeAbstract
) )
)); ));
const GOOGLE_PLUS_BASE_URL = 'https://plus.google.com/';
public function collectData() public function collectData()
{ {
$param=$this->parameters[$queriedContext];
if (!$this->getInput('username'))
{
$this->returnClientError('You must specify a username (?username=...).');
}
$this->request = $this->getInput('username');
// get content parsed // get content parsed
// $html = $this->getSimpleHTMLDOM(__DIR__ . '/../posts2.html' // $html = $this->getSimpleHTMLDOM(__DIR__ . '/../posts2.html'
$html = $this->getSimpleHTMLDOM(self::GOOGLE_PLUS_BASE_URL . urlencode($this->request) . '/posts' $html = $this->getSimpleHTMLDOM($this->uri . urlencode($this->getInput('username')) . '/posts'
// force language // force language
, false, stream_context_create(array('http'=> array( , false, stream_context_create(array('http'=> array(
'header' => 'Accept-Language: fr,fr-be,fr-fr;q=0.8,en;q=0.4,en-us;q=0.2;*' . "\r\n" 'header' => 'Accept-Language: fr,fr-be,fr-fr;q=0.8,en;q=0.4,en-us;q=0.2;*' . "\r\n"
@ -57,7 +48,7 @@ class GooglePlusPostBridge extends BridgeAbstract
// $item['title'] = $item['fullname'] = $post->find('header.lea', 0)->plaintext; // $item['title'] = $item['fullname'] = $post->find('header.lea', 0)->plaintext;
$item['avatar'] = $post->find('div.ys img', 0)->src; $item['avatar'] = $post->find('div.ys img', 0)->src;
// var_dump((($post->find('a.o-U-s', 0)->getAllAttributes()))); // var_dump((($post->find('a.o-U-s', 0)->getAllAttributes())));
$item['uri'] = self::GOOGLE_PLUS_BASE_URL . $post->find('a.o-U-s', 0)->href; $item['uri'] = $this->uri . $post->find('a.o-U-s', 0)->href;
$item['timestamp'] = strtotime($post->find('a.o-U-s', 0)->plaintext); $item['timestamp'] = strtotime($post->find('a.o-U-s', 0)->plaintext);
$this->items[] = $item; $this->items[] = $item;
@ -65,21 +56,21 @@ class GooglePlusPostBridge extends BridgeAbstract
$hashtags = array(); $hashtags = array();
foreach($post->find('a.d-s') as $hashtag) foreach($post->find('a.d-s') as $hashtag)
{ {
$hashtags[ trim($hashtag->plaintext) ] = self::GOOGLE_PLUS_BASE_URL . $hashtag->href; $hashtags[ trim($hashtag->plaintext) ] = $this->uri . $hashtag->href;
} }
$item['content'] = ''; $item['content'] = '';
// avatar display // avatar display
$item['content'] .= '<div style="float:left; margin: 0 0.5em 0.5em 0;"><a href="' . self::GOOGLE_PLUS_BASE_URL . urlencode($this->request); $item['content'] .= '<div style="float:left; margin: 0 0.5em 0.5em 0;"><a href="' . $this->uri . urlencode($this->getInput('username'));
$item['content'] .= '"><img align="top" alt="avatar" src="' . $item['avatar'].'" />' . $item['username'] . '</a></div>'; $item['content'] .= '"><img align="top" alt="avatar" src="' . $item['avatar'].'" />' . $item['username'] . '</a></div>';
$content = $post->find('div.Al', 0); $content = $post->find('div.Al', 0);
// alter link // alter link
// $content = $content->innertext; // $content = $content->innertext;
// $content = str_replace('href="./', 'href="' . self::GOOGLE_PLUS_BASE_URL, $content); // $content = str_replace('href="./', 'href="' . $this->uri, $content);
// $content = str_replace('href="photos', 'href="' . self::GOOGLE_PLUS_BASE_URL . 'photos', $content); // $content = str_replace('href="photos', 'href="' . $this->uri . 'photos', $content);
// 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 // 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
foreach($content->find('a') as $link) foreach($content->find('a') as $link)
{ {
@ -95,7 +86,7 @@ class GooglePlusPostBridge extends BridgeAbstract
{ {
$link->href = substr($link->href, 1); $link->href = substr($link->href, 1);
} }
$link->href = self::GOOGLE_PLUS_BASE_URL . $link->href; $link->href = $this->uri . $link->href;
} }
} }
$content = $content->innertext; $content = $content->innertext;
@ -116,7 +107,7 @@ class GooglePlusPostBridge extends BridgeAbstract
public function getURI() public function getURI()
{ {
return $this->_url ?: 'http://plus.google.com/'; return $this->_url ?: $this->uri;
} }
public function getCacheDuration() public function getCacheDuration()