1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Ne-Lexa |
7
|
|
|
* @license MIT |
8
|
|
|
* |
9
|
|
|
* @see https://github.com/Ne-Lexa/google-play-scraper |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Nelexa\GPlay\Scraper; |
13
|
|
|
|
14
|
|
|
use Nelexa\GPlay\GPlayApps; |
15
|
|
|
use Nelexa\GPlay\Scraper\Extractor\AppsExtractor; |
16
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
17
|
|
|
use Nelexa\HttpClient\ResponseHandlerInterface; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use function GuzzleHttp\Psr7\parse_query; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
*/ |
25
|
|
|
class ClusterAppsScraper implements ResponseHandlerInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param RequestInterface $request |
29
|
|
|
* @param ResponseInterface $response |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
31 |
|
public function __invoke(RequestInterface $request, ResponseInterface $response): array |
34
|
|
|
{ |
35
|
31 |
|
$scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
36
|
31 |
|
$scriptDataInfo = null; |
37
|
|
|
|
38
|
31 |
|
foreach ($scriptData as $scriptValue) { |
39
|
31 |
|
if (isset($scriptValue[0][1][0][0][0]) && \is_array($scriptValue[0][1][0][0][0])) { |
40
|
31 |
|
$scriptDataInfo = $scriptValue; // ds:3 |
41
|
31 |
|
break; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
31 |
|
if ($scriptDataInfo === null) { |
46
|
|
|
return [[], null]; |
47
|
|
|
} |
48
|
|
|
|
49
|
31 |
|
$query = parse_query($request->getUri()->getQuery()); |
50
|
31 |
|
$locale = $query[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE; |
51
|
31 |
|
$country = $query[GPlayApps::REQ_PARAM_COUNTRY] ?? GPlayApps::DEFAULT_COUNTRY; |
52
|
|
|
|
53
|
31 |
|
$apps = []; |
54
|
|
|
|
55
|
31 |
|
foreach ($scriptDataInfo[0][1][0][0][0] as $data) { |
56
|
31 |
|
$apps[] = AppsExtractor::extractApp($data, $locale, $country); |
57
|
|
|
} |
58
|
|
|
|
59
|
31 |
|
$nextToken = $scriptDataInfo[0][1][0][0][7][1] ?? null; |
60
|
|
|
|
61
|
31 |
|
return [$apps, $nextToken]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|