|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nelexa\GPlay\Scraper; |
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
7
|
|
|
use GuzzleHttp\Psr7\Request; |
|
8
|
|
|
use Nelexa\GPlay\Exception\GooglePlayException; |
|
9
|
|
|
use Nelexa\GPlay\GPlayApps; |
|
10
|
|
|
use Nelexa\GPlay\Model\App; |
|
11
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
|
12
|
|
|
use Psr\Http\Message\RequestInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
|
|
15
|
|
|
class SimilarAppsScraper extends PlayStoreUiPagesScraper |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param RequestInterface $request |
|
20
|
|
|
* @param ResponseInterface $response |
|
21
|
|
|
* @return App[] |
|
22
|
|
|
* @throws GooglePlayException |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response) |
|
25
|
|
|
{ |
|
26
|
|
|
$scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
|
27
|
|
|
$similarAppsUrl = null; |
|
28
|
|
|
foreach ($scriptData as $key => $scriptValue) { |
|
29
|
|
|
if (isset($scriptValue[1][1][0][0][3][4][2])) { |
|
30
|
|
|
$similarAppsUrl = GPlayApps::GOOGLE_PLAY_URL . $scriptValue[1][1][0][0][3][4][2]; |
|
31
|
|
|
break; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
unset($scriptData); |
|
36
|
|
|
|
|
37
|
|
|
if ($similarAppsUrl === null) { |
|
38
|
|
|
return []; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$similarAppsUrl .= |
|
42
|
|
|
'&' . GPlayApps::REQ_PARAM_LOCALE . '=' . urlencode($this->locale) . |
|
43
|
|
|
'&' . GPlayApps::REQ_PARAM_COUNTRY . '=' . urlencode($this->country); |
|
44
|
|
|
|
|
45
|
|
|
$requestSimilar = new Request('GET', $similarAppsUrl); |
|
46
|
|
|
try { |
|
47
|
|
|
$responseSimilar = $this->httpClient->send($requestSimilar); |
|
48
|
|
|
} catch (GuzzleException $e) { |
|
49
|
|
|
throw new GooglePlayException($e->getMessage(), $e->getCode(), $e); |
|
50
|
|
|
} |
|
51
|
|
|
return parent::__invoke($requestSimilar, $responseSimilar); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|