Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Page/GoogleSerp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Serps\SearchEngine\Google\Parser\Evaluated\NaturalParser as EvaluatedNaturalParser;
use Serps\SearchEngine\Google\Parser\Raw\NaturalParser as RawNaturalParser;
use Serps\Stubs\RelatedSearch;
use Serps\SearchEngine\Google\Parser\Evaluated\WikiBoxParser;

class GoogleSerp extends GoogleDom
{
Expand Down Expand Up @@ -134,4 +135,17 @@ public function getRelatedSearches()

return $relatedSearches;
}

/**
* @return \Serps\Core\Serp\IndexedResultSet
*/
public function getWikiBox()
{
$result = null;
if ($this->javascriptIsEvaluated()) {
$parser = new WikiBoxParser();
$result = $parser->parse($this);
}
return $result;
}
}
56 changes: 56 additions & 0 deletions src/Parser/Evaluated/Rule/WikiBox/Description.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Created by PhpStorm.
* User: aurimasgladutis
* Date: 9/1/16
* Time: 4:25 PM
*/

namespace Serps\SearchEngine\Google\Parser\Evaluated\Rule\WikiBox;

use Serps\Core\Serp\BaseResult;
use Serps\Core\Serp\IndexedResultSet;
use Serps\SearchEngine\Google\Css;
use Serps\SearchEngine\Google\Page\GoogleDom;
use Serps\SearchEngine\Google\Parser\ParsingRuleInterace;

class Description implements ParsingRuleInterace
{
/**
* @param GoogleDom $dom
* @param \DOMElement $node
*
* @return int
*/
public function match(GoogleDom $dom, \DOMElement $node)
{
return $node->getAttribute('class') == '_G1d _wle _xle' ? self::RULE_MATCH_MATCHED : self::RULE_MATCH_NOMATCH;
}

/**
* @param GoogleDom $dom
* @param \DOMElement $node
* @param IndexedResultSet $resultSet
*/
public function parse(GoogleDom $dom, \DOMElement $node, IndexedResultSet $resultSet)
{
$xpath = $dom->getXpath();

/* @var $description \DOMElement */
$description = $xpath
->query(Css::toXPath('.kno-rdesc span:first-child'), $node)
->item(0);

$wikiLink = $xpath
->query(Css::toXPath('.kno-rdesc span:last-child a'), $node)
->item(0);

$data = [
'description' => $description ? $description->nodeValue : null,
'wikipedia_link' => $wikiLink ? $wikiLink->getAttribute('href') : null,
];

$item = new BaseResult('description', $data);
$resultSet->addItem($item);
}
}
77 changes: 77 additions & 0 deletions src/Parser/Evaluated/Rule/WikiBox/Header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Created by PhpStorm.
* User: aurimasgladutis
* Date: 9/1/16
* Time: 4:25 PM
*/

namespace Serps\SearchEngine\Google\Parser\Evaluated\Rule\WikiBox;

use Serps\Core\Serp\BaseResult;
use Serps\Core\Serp\IndexedResultSet;
use Serps\SearchEngine\Google\Css;
use Serps\SearchEngine\Google\Page\GoogleDom;
use Serps\SearchEngine\Google\Parser\ParsingRuleInterace;

class Header implements ParsingRuleInterace
{
/**
* @param GoogleDom $dom
* @param \DOMElement $node
*
* @return int
*/
public function match(GoogleDom $dom, \DOMElement $node)
{
return $node->getAttribute('class') == 'kp-header' ? self::RULE_MATCH_MATCHED : self::RULE_MATCH_NOMATCH;
}

/**
* @param GoogleDom $dom
* @param \DOMElement $node
* @param IndexedResultSet $resultSet
*/
public function parse(GoogleDom $dom, \DOMElement $node, IndexedResultSet $resultSet)
{
$xpath = $dom->getXpath();

/* @var $title \DOMElement */
$title = $xpath
->query(Css::toXPath('.kp-hc .kno-ecr-pt.kno-fb-ctx'), $node)
->item(0);

$description = $xpath
->query(Css::toXPath('.kp-hc ._gdf._LAf span'), $node)
->item(0);

$map = $xpath
->query(Css::toXPath('#media_result_group ._tN._eXg a'), $node) // .kno-fb-ctx .rhsg4.rhsmap5col a img
->item(0);

$mapData = null;
if (null !== $map) {
$url = $map->getAttribute('href');
$placeName = null;
$placeData = null;
if (preg_match('/maps\/place\/(.*?)\/data=(.*?)\?/', $url, $matches)) {
$placeName = $matches[1];
$placeData = $matches[2];
}
$mapData = [
'url' => $url,
'placeName' => $placeName,
'placeData' => $placeData,
];
}

$data = [
'title' => $title ? $title->nodeValue : null,
'description' => $description ? $description->nodeValue : null,
'map' => $mapData,
];

$item = new BaseResult('header', $data);
$resultSet->addItem($item);
}
}
40 changes: 40 additions & 0 deletions src/Parser/Evaluated/WikiBoxParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @license see LICENSE
*/

namespace Serps\SearchEngine\Google\Parser\Evaluated;

use Serps\SearchEngine\Google\Parser\Evaluated\Rule\WikiBox\Header;
use Serps\SearchEngine\Google\Parser\Evaluated\Rule\WikiBox\Description;
use Serps\SearchEngine\Google\Css;
use Serps\SearchEngine\Google\Page\GoogleDom;
use Serps\SearchEngine\Google\Parser\AbstractParser;

/**
* Parses natural results from a google SERP
*/
class WikiBoxParser extends AbstractParser
{
/**
* @inheritdoc
*/
protected function generateRules()
{
return [
new Header(),
new Description(),
];
}

/**
* @inheritdoc
*/
protected function getParsableItems(GoogleDom $googleDom)
{
$xpathObject = $googleDom->getXpath();
$xpath = Css::toXPath('div#rhs div._OKe > div > div');

return $xpathObject->query($xpath);
}
}