|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright (c) Ne-Lexa |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @see https://github.com/Ne-Lexa/google-play-scraper |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Nelexa\GPlay\Scraper; |
|
15
|
|
|
|
|
16
|
|
|
use GuzzleHttp\Psr7\Query; |
|
17
|
|
|
use Nelexa\GPlay\Exception\GooglePlayException; |
|
18
|
|
|
use Nelexa\GPlay\GPlayApps; |
|
19
|
|
|
use Nelexa\GPlay\HttpClient\ParseHandlerInterface; |
|
20
|
|
|
use Nelexa\GPlay\Model\Developer; |
|
21
|
|
|
use Nelexa\GPlay\Model\GoogleImage; |
|
22
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
|
23
|
|
|
use Psr\Http\Message\RequestInterface; |
|
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @internal |
|
28
|
|
|
*/ |
|
29
|
|
|
class DeveloperInfoScraper implements ParseHandlerInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @param RequestInterface $request |
|
33
|
|
|
* @param ResponseInterface $response |
|
34
|
|
|
* @param array $options |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \Nelexa\GPlay\Exception\GooglePlayException |
|
37
|
|
|
* |
|
38
|
|
|
* @return \Nelexa\GPlay\Model\Developer |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): Developer |
|
41
|
|
|
{ |
|
42
|
2 |
|
$query = Query::parse($request->getUri()->getQuery()); |
|
43
|
2 |
|
$developerId = $query[GPlayApps::REQ_PARAM_ID]; |
|
44
|
2 |
|
$url = (string) $request->getUri()->withQuery(http_build_query([GPlayApps::REQ_PARAM_ID => $developerId])); |
|
45
|
|
|
|
|
46
|
2 |
|
$scriptDataInfo = $this->getScriptDataInfo($request, $response, $developerId); |
|
47
|
|
|
|
|
48
|
2 |
|
$name = $scriptDataInfo[13][4][0]; |
|
49
|
|
|
|
|
50
|
2 |
|
$cover = empty($scriptDataInfo[13][1][0][3][2]) |
|
51
|
|
|
? null |
|
52
|
2 |
|
: new GoogleImage($scriptDataInfo[13][1][0][3][2]); |
|
53
|
2 |
|
$icon = empty($scriptDataInfo[13][2][0][3][2]) |
|
54
|
|
|
? null |
|
55
|
2 |
|
: new GoogleImage($scriptDataInfo[13][2][0][3][2]); |
|
56
|
2 |
|
$developerSite = $scriptDataInfo[13][0][0][5][2] ?? null; |
|
57
|
2 |
|
$description = $scriptDataInfo[13][5][1][1] ?? ''; |
|
58
|
|
|
|
|
59
|
2 |
|
return new Developer( |
|
60
|
2 |
|
Developer::newBuilder() |
|
61
|
2 |
|
->setId($developerId) |
|
62
|
2 |
|
->setUrl($url) |
|
63
|
2 |
|
->setName($name) |
|
64
|
2 |
|
->setDescription($description) |
|
65
|
2 |
|
->setWebsite($developerSite) |
|
66
|
2 |
|
->setIcon($icon) |
|
67
|
2 |
|
->setCover($cover) |
|
68
|
2 |
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param RequestInterface $request |
|
73
|
|
|
* @param ResponseInterface $response |
|
74
|
|
|
* @param string $developerId |
|
75
|
|
|
* |
|
76
|
|
|
* @throws GooglePlayException |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
2 |
|
private function getScriptDataInfo(RequestInterface $request, ResponseInterface $response, string $developerId): array |
|
81
|
|
|
{ |
|
82
|
2 |
|
$scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
|
83
|
|
|
|
|
84
|
2 |
|
$scriptDataInfo = null; |
|
85
|
|
|
|
|
86
|
2 |
|
foreach ($scriptData as $scriptValue) { |
|
87
|
2 |
|
if (isset($scriptValue[1][0][12][0][0]) && (string)$scriptValue[1][0][12][0][0] === $developerId) { |
|
88
|
2 |
|
$scriptDataInfo = $scriptValue[1][0]; // ds:5.1.0 |
|
89
|
2 |
|
break; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
if ($scriptDataInfo === null) { |
|
94
|
|
|
throw (new GooglePlayException( |
|
95
|
|
|
sprintf( |
|
96
|
|
|
'Error parse vendor page %s. Need update library.', |
|
97
|
|
|
$request->getUri() |
|
98
|
|
|
) |
|
99
|
|
|
))->setUrl($request->getUri()->__toString()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
return $scriptDataInfo; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|