Skip to content

Commit 889bbe8

Browse files
committed
Update Rating.php
Instance Scrapper
1 parent 8686b66 commit 889bbe8

File tree

1 file changed

+3
-130
lines changed

1 file changed

+3
-130
lines changed

src/includes/Rating.php

+3-130
Original file line numberDiff line numberDiff line change
@@ -14,146 +14,19 @@
1414

1515
namespace Apaapi\includes;
1616

17-
use DOMDocument, DOMNodeList, DOMXPath;
18-
1917
/**
2018
* @deprecated Use Scrapper Class instead
2119
*/
22-
final class Rating
20+
final class Rating extends Scrapper
2321
{
2422
/**
25-
* @access private
26-
* @var string $keyword
27-
* @var string $locale
28-
* @var string $tag
29-
*/
30-
private $keyword;
31-
private $locale;
32-
private $tag;
33-
34-
private const ACTION = '/dp/';
35-
private const VALUExPATH = "//div[contains(@class,'AverageCustomerReviews')]";
36-
private const COUNTxPATH = "//div[contains(@class,'averageStarRatingNumerical')]";
37-
38-
/**
39-
* Init rating.
40-
*
41-
* @param string $keyword
42-
* @param string $locale
43-
* @param string $tag
44-
*/
45-
public function __construct(string $keyword, string $locale = 'com', ?string $tag = null)
46-
{
47-
$this->keyword = Normalizer::formatId($keyword);
48-
$this->locale = Normalizer::formatTLD($locale);
49-
$this->tag = $tag;
50-
}
51-
52-
/**
53-
* Get rating data.
23+
* Get item rating data.
5424
*
5525
* @access public
5626
* @return array
5727
*/
5828
public function get() : array
5929
{
60-
$default = $this->getDefault();
61-
62-
if ( !Keyword::isASIN($this->keyword) && !Keyword::isISBN($this->keyword) ) {
63-
return $default;
64-
}
65-
66-
$key = "rating-{$this->locale}-{$this->keyword}";
67-
$key = Cache::generateKey($key);
68-
69-
if ( !($rating = Cache::get($key)) ) {
70-
71-
$host = Provider::HOST;
72-
$url = str_replace('{locale}', $this->locale, $host);
73-
$url .= self::ACTION;
74-
$url .= $this->keyword;
75-
76-
$header = Scrapper::generateHeader($this->locale);
77-
$client = new Client($url, [
78-
'header' => $header,
79-
'timeout' => 30
80-
]);
81-
82-
$client->setEncoding()->get();
83-
$response = $client->getBody();
84-
85-
if ( $client->getStatusCode() == 200 ) {
86-
$rating = $this->extract($response);
87-
$rating['url'] = "{$url}?tag={$this->tag}&linkCode=ll2";
88-
$rating = array_merge($default, $rating);
89-
Cache::set($key, $rating);
90-
}
91-
92-
}
93-
94-
return $rating ?: $default;
95-
}
96-
97-
/**
98-
* Extract rating data from HTML.
99-
*
100-
* @access private
101-
* @param string $html
102-
* @return array
103-
*/
104-
private function extract(string $html) : array
105-
{
106-
$rating = [];
107-
108-
if ( class_exists('DomDocument') ) {
109-
110-
// Ignore XML errors
111-
libxml_use_internal_errors(true);
112-
113-
// Init DOM document
114-
$dom = new DOMDocument();
115-
$dom->loadHTML($html);
116-
$xPath = new DOMXPath($dom);
117-
118-
// Extract rating value
119-
$nodes = $xPath->query(self::VALUExPATH);
120-
if ( $nodes instanceof DOMNodeList && ($nodes->length > 0) ) {
121-
$node = $nodes->item(0);
122-
$data = explode(' ', (string)$node->nodeValue);
123-
$value = $data[0];
124-
$value = str_replace(',', '.', $value);
125-
$rating['value'] = (float)$value;
126-
}
127-
128-
// Extract rating count
129-
$nodes = $xPath->query(self::COUNTxPATH);
130-
if ( $nodes instanceof DOMNodeList && ($nodes->length > 0) ) {
131-
$node = $nodes->item(0);
132-
$count = (string)$node->nodeValue;
133-
$count = preg_replace('/\D/', '', $count);
134-
$rating['count'] = (int)$count;
135-
}
136-
137-
// Clear XML errors
138-
libxml_clear_errors();
139-
140-
}
141-
142-
return $rating;
143-
}
144-
145-
/**
146-
* Get default rating.
147-
*
148-
* @access private
149-
* @return array
150-
*/
151-
private function getDefault() : array
152-
{
153-
return [
154-
'value' => null,
155-
'count' => null,
156-
'url' => null
157-
];
30+
return $this->getRating();
15831
}
15932
}

0 commit comments

Comments
 (0)