Skip to content

Commit

Permalink
Fix mobile results
Browse files Browse the repository at this point in the history
  • Loading branch information
gsouf committed Aug 27, 2018
1 parent f33a671 commit 6ef29cb
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Parser/Evaluated/MobileNaturalParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Serps\SearchEngine\Google\Page\GoogleDom;
use Serps\SearchEngine\Google\Parser\AbstractParser;
use Serps\SearchEngine\Google\Parser\Evaluated\Rule\Natural\Classical\ClassicalCardsResultO9g5cc;
use Serps\SearchEngine\Google\Parser\Evaluated\Rule\Natural\Classical\ClassicalCardsResultZ1m;
use Serps\SearchEngine\Google\Parser\Evaluated\Rule\Natural\Classical\ClassicalCardsResultZINbbc;
use Serps\SearchEngine\Google\Parser\Evaluated\Rule\Natural\Classical\ClassicalCardsVideoResult;
Expand Down Expand Up @@ -36,7 +37,8 @@ protected function generateRules()
return [
new Divider(),
new SearchResultGroup(),
new ClassicalCardsResultZINbbc(),
new ClassicalCardsResultO9g5cc(),
new ClassicalCardsResultZINbbc(), // TODO maybe outdated
new ClassicalCardsResultZ1m(), // TODO remove (outdated)
new ClassicalCardsVideoResult(),
new ClassicalCardsResult(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* @license see LICENSE
*/

namespace Serps\SearchEngine\Google\Parser\Evaluated\Rule\Natural\Classical;

use Serps\Core\Dom\DomElement;
use Serps\Core\Dom\DomNodeInterface;
use Serps\Core\Serp\BaseResult;
use Serps\Core\Serp\IndexedResultSet;
use Serps\SearchEngine\Google\NaturalResultType;
use Serps\SearchEngine\Google\Page\GoogleDom;
use Serps\SearchEngine\Google\Parser\ParsingRuleInterface;

/**
* First seen in august 2018 in mobile pages, replacing .ZINbbc
*/
class ClassicalCardsResultO9g5cc implements ParsingRuleInterface
{

public function match(GoogleDom $dom, DomElement $node)
{
$res = $dom->cssQuery('.O9g5cc.xpd a.C8nzq', $node);
// TODO consider removing .ZINbbc (replaced with .O9g5cc in august 2018)

if ($res->length == 1) {
return self::RULE_MATCH_MATCHED;
}

return self::RULE_MATCH_NOMATCH;
}

public function parse(GoogleDom $dom, \DomElement $node, IndexedResultSet $resultSet)
{
$classicalData = $this->parseNode($dom, $node);

$resultTypes = [NaturalResultType::CLASSICAL];

$item = new BaseResult($resultTypes, $classicalData);
$resultSet->addItem($item);
}

protected function parseNode(GoogleDom $dom, DomNodeInterface $node)
{
return [
'title' => function () use ($dom, $node) {
return $dom
->cssQuery('a .MUxGbd', $node)
->getNodeAt(0)
->getNodeValue();
},
'isAmp' => function () use ($dom, $node) {
return $dom
->cssQuery('.ZseVEf', $node)
->length > 0;
},
'url' => function () use ($dom, $node) {
return $dom
->cssQuery('a.C8nzq', $node)
->getNodeAt(0)
->getAttribute('href');
},
'destination' => function () use ($dom, $node) {
return $dom
->cssQuery('span.QHTnWc, span.qzEoUe', $node) // TODO ".QHTnWc" appears to be outdated
->getNodeAt(0)
->getNodeValue();
},
'description' => function () use ($dom, $node) {
return $dom
->cssQuery('.JTuIPc:not(a)>.MUxGbd', $node)
->getNodeAt(0)
->getNodeValue();
}
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/**
* First seen in 2018 in mobile pages
* TODO consider removing .ZINbbc (replaced with .O9g5cc in august 2018)
*/
class ClassicalCardsResultZINbbc implements ParsingRuleInterface
{
Expand Down
56 changes: 56 additions & 0 deletions test/resources/pages-mobile/2018/08/new+construction+ct.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# image-group recipes
url: https://www.google.ca/search?q=new+construction+ct
file: test/resources/pages-mobile/2018/08/new+construction+ct.html

test-methods:
javascriptIsEvaluated: true
getNumberOfResults: null
isMobile: true

results:

- types:
- CLASSICAL

data:
title: Connecticut New Homes & New Construction For Sale | Zillow
destination: Zillow › ct › new-homes
url: https://www.zillow.com/ct/new-homes/
description: Discover new construction homes or master planned communities in Connecticut . Check out floor plans, pictures and ...

- types:
- CLASSICAL
data:
url: https://www.realtor.com/newhomesconstruction/Connecticut
destination: Realtor.com › newhomesconstruction › C...

- types:
- CLASSICAL

- types:
- CLASSICAL

- types:
- CLASSICAL

data:
title: New Home Development and New Home Construction | William ...

- types:
- CLASSICAL

- types:
- CLASSICAL

- types:
- CLASSICAL

- types:
- CLASSICAL

data:
isAmp: true

- types:
- CLASSICAL

data:
isAmp: true
title: Connecticut Homes for Sale - 13 New Home Communities | Toll ...
url: https://www.tollbrothers.com/luxury-homes/Connecticut?amp=true
destination: Toll Brothers › luxury-homes
description: Connecticut homes for sale by Toll Brothers®. 13 new luxury home communities in CT. View photos, floor plans, pricing ...

0 comments on commit 6ef29cb

Please sign in to comment.