Skip to content

Instantly share code, notes, and snippets.

@nsanden
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsanden/b728ad0b447351511754 to your computer and use it in GitHub Desktop.
Save nsanden/b728ad0b447351511754 to your computer and use it in GitHub Desktop.
<?php
/* Searches pinterest (screen scrape) for photos */
public function search($search_params)
{
try {
$url = 'http://www.pinterest.com/search/pins/?q=' . urlencode($search_params['search_keywords']);
$html = file_get_contents($url);
} catch (\Exception $e) {
$this->message->set_message("Could not load the Pinterest search page. Please try again later." . "\n\n". $e->getMessage(), Message::MSG_TYPE_ERROR);
exit;
}
$dom = new \DOMDocument();
@$dom->loadHTML($html);
$x = new \DOMXPath($dom);
$i=0;
foreach($x->query("//div[contains(@class,'pinWrapper')]") as $node)
{
//id
$idNode = $x->query(".//a[contains(@class,'pinImageWrapper')]", $node);
foreach($idNode as $myIdNode)
{
$url = $myIdNode->getAttribute('href');
preg_match('~/pin/([0-9]+)/~', $url, $matches);
$id = $matches[1];
}
if($search_params['hide_used_content'] == 1)
{
if($this->is_used($id, Yii::$app->user->id))
{
continue;
}
}
$posts[$i]['id'] = $id;
//image
$imgNode = $x->query(".//img[contains(@src,'236x/')]", $node);
foreach($imgNode as $myImgNode)
{
$posts[$i]['img'] = str_replace('236x', '736x', $this->rel2abs($myImgNode->getAttribute("src"), $url));
}
//text
$descriptionNode = $x->query(".//p[contains(@class,'pinDescription')]", $node);
foreach($descriptionNode as $myDescriptionNode)
{
$posts[$i]['message'] = $myDescriptionNode->nodeValue;
}
//repins
$repinNode = $x->query(".//a[contains(@class,'socialItem') and contains(@href, '/repins/')]", $node);
foreach($repinNode as $myRepinNode)
{
$posts[$i]['repins'] = (trim(str_replace('repin', '', str_replace('repins', '', $myRepinNode->nodeValue))));
}
//likes
$likesNode = $x->query(".//a[contains(@class,'socialItem likes')]", $node);
foreach($likesNode as $myLikesNode)
{
$posts[$i]['likes'] = (trim(str_replace('like', '', str_replace('likes', '', $myLikesNode->nodeValue))));
}
//likes
$commentsNode = $x->query(".//a[contains(@class,'socialItem comments')]", $node);
foreach($commentsNode as $myCommentsNode)
{
$posts[$i]['comments'] = (trim(str_replace('comment', '', str_replace('comments', '', $myCommentsNode->nodeValue))));
}
//author name
$authorNameNode = $x->query(".//div[@class='creditName'][1]", $node);
foreach($authorNameNode as $myAuthorNameNode)
{
$posts[$i]['author_name'] = $myAuthorNameNode->nodeValue;
}
//author link
$authorLinkNode = $x->query(".//div[contains(@class,'pinCredits')]//a[contains(@class,'creditItem')][1]", $node);
//\fbpostbot\Common::print_p($authorLinkNode);
foreach($authorLinkNode as $myAuthorLinkNode)
{
$posts[$i]['author_link'] = $myAuthorLinkNode->getAttribute("href");
}
//author img
$authorImgNode = $x->query(".//div[contains(@class,'pinCredits')]//img[contains(@class,'creditImg')][1]", $node);
//\fbpostbot\Common::print_p($authorLinkNode);
foreach($authorImgNode as $myAuthorImgNode)
{
$posts[$i]['author_img_src'] = $myAuthorImgNode->getAttribute("src");
}
$i++;
}
return $posts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment