2014-01-30 17:55:35 +04:00
|
|
|
<?php
|
2016-09-17 20:09:33 +03:00
|
|
|
class PinterestBridge extends BridgeAbstract {
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2016-08-30 12:23:55 +03:00
|
|
|
const MAINTAINER = "pauder";
|
|
|
|
const NAME = "Pinterest Bridge";
|
|
|
|
const URI = "http://www.pinterest.com/";
|
|
|
|
const DESCRIPTION = "Returns the newest images on a board";
|
2016-08-27 22:03:26 +03:00
|
|
|
|
2016-09-17 20:09:33 +03:00
|
|
|
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(){
|
2016-09-26 00:22:33 +03:00
|
|
|
$html = getSimpleHTMLDOM($this->getURI());
|
2016-09-17 20:09:33 +03:00
|
|
|
if(!$html){
|
|
|
|
switch($this->queriedContext){
|
|
|
|
case 'By username and board':
|
2016-09-26 00:22:33 +03:00
|
|
|
returnServerError('Username and/or board not found');
|
2016-09-17 20:09:33 +03:00
|
|
|
case 'From search':
|
2016-09-26 00:22:33 +03:00
|
|
|
returnServerError('Could not request Pinterest.');
|
2016-09-17 20:09:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 20:14:05 +03:00
|
|
|
if($this->queriedContext === 'From search'){
|
|
|
|
foreach($html->find('div.pinWrapper') as $div){
|
|
|
|
$item = array();
|
2016-09-17 20:09:33 +03:00
|
|
|
|
2016-09-17 20:14:05 +03:00
|
|
|
$a = $div->find('a.pinImageWrapper', 0);
|
|
|
|
$img = $a->find('img', 0);
|
|
|
|
|
|
|
|
$item['uri'] = $this->getURI() . $a->getAttribute('href');
|
|
|
|
$item['content'] = '<img src="'
|
|
|
|
. htmlentities(str_replace('/236x/', '/736x/', $img->getAttribute('src')))
|
|
|
|
. '" alt="" />';
|
2016-09-17 20:09:33 +03:00
|
|
|
|
|
|
|
$avatar = $div->find('div.creditImg', 0)->find('img', 0);
|
2016-02-26 17:58:03 +03:00
|
|
|
$avatar = $avatar->getAttribute('data-src');
|
|
|
|
$avatar = str_replace("\\", "", $avatar);
|
|
|
|
|
2016-09-17 20:09:33 +03:00
|
|
|
$username = $div->find('div.creditName', 0);
|
|
|
|
$board = $div->find('div.creditTitle', 0);
|
|
|
|
|
|
|
|
$item['username'] = $username->innertext;
|
|
|
|
$item['fullname'] = $board->innertext;
|
|
|
|
$item['avatar'] = $avatar;
|
|
|
|
|
|
|
|
$item['content'] .= '<br /><img align="left" style="margin: 2px 4px;" src="'
|
|
|
|
. htmlentities($item['avatar'])
|
|
|
|
. '" /> <strong>'
|
|
|
|
. $item['username']
|
|
|
|
. '</strong>'
|
|
|
|
. '<br />'
|
|
|
|
. $item['fullname'];
|
|
|
|
|
2016-09-17 20:14:05 +03:00
|
|
|
$item['title'] = $img->getAttribute('alt');
|
2016-09-17 21:10:00 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
} elseif($this->queriedContext === 'By username and board'){
|
|
|
|
$container = $html->find('SCRIPT[type="application/ld+json"]', 0)
|
2016-09-26 00:22:33 +03:00
|
|
|
or returnServerError('Unable to find data container!');
|
2016-09-17 21:10:00 +03:00
|
|
|
|
|
|
|
$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'] = <<<EOD
|
|
|
|
<a href="{$item['uri']}">
|
|
|
|
<img src="{$element['item']['image']}">
|
|
|
|
</a>
|
|
|
|
<p>{$element['item']['text']}</p>
|
|
|
|
EOD;
|
|
|
|
|
2016-09-17 20:14:05 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
2016-09-17 20:09:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-01-30 17:55:35 +04:00
|
|
|
}
|