From c1cc4da8ef66253d6bde57d6e577f8cca800604d Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 17 Sep 2016 19:09:33 +0200 Subject: [PATCH 1/3] [Pinterest] Cleanup code --- bridges/PinterestBridge.php | 158 ++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 80 deletions(-) diff --git a/bridges/PinterestBridge.php b/bridges/PinterestBridge.php index 12e907a8..bae59da2 100644 --- a/bridges/PinterestBridge.php +++ b/bridges/PinterestBridge.php @@ -1,101 +1,99 @@ array( - 'u'=>array( - 'name'=>'username', - 'required'=>true - ), - 'b'=>array( - 'name'=>'board', - 'required'=>true - ) - ), - 'From search' => array( - 'q'=>array( - 'name'=>'Keyword', - 'required'=>true - ) - ) - ); + const PARAMETERS = array( + 'By username and board' => array( + 'u' => array( + 'name' => 'username', + 'required' => true + ), + 'b' => array( + 'name' => 'board', + 'required' => true + ) + ), + 'From search' => array( + 'q' => array( + 'name' => 'Keyword', + 'required' => true + ) + ) + ); + public function collectData(){ + $html = $this->getSimpleHTMLDOM($this->getURI()); + if(!$html){ + switch($this->queriedContext){ + case 'By username and board': + $this->returnServerError('Username and/or board not found'); + case 'From search': + $this->returnServerError('Could not request Pinterest.'); + } + } - public function collectData(){ - $html = $this->getSimpleHTMLDOM($this->getURI()) ; - if(!$html){ - switch($this->queriedContext){ - case 'By username and board': - $this->returnServerError('Username and/or board not found'); - case 'From search': - $this->returnServerError('Could not request Pinterest.'); - } - } + foreach($html->find('div.pinWrapper') as $div){ + $a = $div->find('a.pinImageWrapper', 0); + $img = $a->find('img', 0); - foreach($html->find('div.pinWrapper') as $div) - { - $a = $div->find('a.pinImageWrapper',0); + $item = array(); + $item['uri'] = $this->getURI() . $a->getAttribute('href'); + $item['content'] = ''; - $img = $a->find('img', 0); - - $item = array(); - $item['uri'] = $this->getURI().$a->getAttribute('href'); - $item['content'] = ''; - - - if ($this->queriedContext==='From search') - { - $avatar = $div->find('div.creditImg', 0)->find('img', 0); + if($this->queriedContext === 'From search'){ + $avatar = $div->find('div.creditImg', 0)->find('img', 0); $avatar = $avatar->getAttribute('data-src'); $avatar = str_replace("\\", "", $avatar); + $username = $div->find('div.creditName', 0); + $board = $div->find('div.creditTitle', 0); - $username = $div->find('div.creditName', 0); - $board = $div->find('div.creditTitle', 0); + $item['username'] = $username->innertext; + $item['fullname'] = $board->innertext; + $item['avatar'] = $avatar; - $item['username'] =$username->innertext; - $item['fullname'] = $board->innertext; - $item['avatar'] = $avatar; + $item['content'] .= '
' + . $item['username'] + . '' + . '
' + . $item['fullname']; + } - $item['content'] .= '
'.$item['username'].''; - $item['content'] .= '
'.$item['fullname']; - } + $item['title'] = $img->getAttribute('alt'); - $item['title'] = $img->getAttribute('alt'); + $this->items[] = $item; + } + } - //$item['timestamp'] = $media->created_time; - $this->items[] = $item; + public function getURI(){ + switch($this->queriedContext){ + case 'By username and board': + $uri = self::URI . urlencode($this->getInput('u')) . '/' . urlencode($this->getInput('b')); + break; + case 'From search': + $uri = self::URI . 'search/?q=' . urlencode($this->getInput('q')); + break; + } + return $uri; + } - } - } - - public function getURI(){ - switch($this->queriedContext){ - case 'By username and board': - $uri = self::URI.urlencode($this->getInput('u')).'/'.urlencode($this->getInput('b')); - break; - case 'From search': - $uri = self::URI.'search/?q='.urlencode($this->getInput('q')); - break; - } - - return $uri; - } - - public function getName(){ - switch($this->queriedContext){ - case 'By username and board': - $specific = $this->getInput('u').'-'.$this->getInput('b'); - break; - case 'From search': - $specific = $this->getInput('q'); - break; - } - return $specific .' - '.self::NAME; - } + public function getName(){ + switch($this->queriedContext){ + case 'By username and board': + $specific = $this->getInput('u') . '-' . $this->getInput('b'); + break; + case 'From search': + $specific = $this->getInput('q'); + break; + } + return $specific . ' - ' . self::NAME; + } } From f8e0a4afbc96abfa2517982323811785314e86f8 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 17 Sep 2016 19:14:05 +0200 Subject: [PATCH 2/3] [Pinterest] Move all existing code into 'From search' section 'By username and board' requires a different search algorithm --- bridges/PinterestBridge.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/bridges/PinterestBridge.php b/bridges/PinterestBridge.php index bae59da2..4907f40a 100644 --- a/bridges/PinterestBridge.php +++ b/bridges/PinterestBridge.php @@ -36,17 +36,18 @@ class PinterestBridge extends BridgeAbstract { } } - foreach($html->find('div.pinWrapper') as $div){ - $a = $div->find('a.pinImageWrapper', 0); - $img = $a->find('img', 0); + if($this->queriedContext === 'From search'){ + foreach($html->find('div.pinWrapper') as $div){ + $item = array(); - $item = array(); - $item['uri'] = $this->getURI() . $a->getAttribute('href'); - $item['content'] = ''; + $a = $div->find('a.pinImageWrapper', 0); + $img = $a->find('img', 0); + + $item['uri'] = $this->getURI() . $a->getAttribute('href'); + $item['content'] = ''; - if($this->queriedContext === 'From search'){ $avatar = $div->find('div.creditImg', 0)->find('img', 0); $avatar = $avatar->getAttribute('data-src'); $avatar = str_replace("\\", "", $avatar); @@ -65,11 +66,10 @@ class PinterestBridge extends BridgeAbstract { . '' . '
' . $item['fullname']; + + $item['title'] = $img->getAttribute('alt'); + $this->items[] = $item; } - - $item['title'] = $img->getAttribute('alt'); - - $this->items[] = $item; } } From c3a1cbe98a36dbb0229b0ef79270bc7e9f28d581 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 17 Sep 2016 20:10:00 +0200 Subject: [PATCH 3/3] [Pinterest] Add implementation for user/board The data is no longer provided in HTML upon request, but rather encoded as JSON in a SCRIPT section and decoded via Javascript on the client side. The bridge now decodes the data and returns valid feeds again. --- bridges/PinterestBridge.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/bridges/PinterestBridge.php b/bridges/PinterestBridge.php index 4907f40a..59bbf122 100644 --- a/bridges/PinterestBridge.php +++ b/bridges/PinterestBridge.php @@ -68,6 +68,28 @@ class PinterestBridge extends BridgeAbstract { . $item['fullname']; $item['title'] = $img->getAttribute('alt'); + $this->items[] = $item; + } + } elseif($this->queriedContext === 'By username and board'){ + $container = $html->find('SCRIPT[type="application/ld+json"]', 0) + or $this->returnServerError('Unable to find data container!'); + + $json = json_decode($container->innertext, true); + + foreach($json['itemListElement'] as $element){ + $item = array(); + + $item['uri'] = $element['item']['sharedContent']['author']['url']; + $item['title'] = $element['item']['name']; + $item['author'] = $element['item']['user']['name']; + $item['timestamp'] = strtotime($element['item']['datePublished']); + $item['content'] = << + + +

{$element['item']['text']}

+EOD; + $this->items[] = $item; } }