|
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\Exception\GooglePlayException; |
|
15
|
|
|
use Nelexa\GPlay\GPlayApps; |
|
16
|
|
|
use Nelexa\GPlay\Model\Developer; |
|
17
|
|
|
use Nelexa\GPlay\Model\GoogleImage; |
|
18
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
|
19
|
|
|
use Nelexa\HttpClient\ResponseHandlerInterface; |
|
20
|
|
|
use Psr\Http\Message\RequestInterface; |
|
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
22
|
|
|
use function GuzzleHttp\Psr7\parse_query; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @internal |
|
26
|
|
|
*/ |
|
27
|
|
|
class DeveloperInfoScraper implements ResponseHandlerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param RequestInterface $request |
|
31
|
|
|
* @param ResponseInterface $response |
|
32
|
|
|
* |
|
33
|
|
|
* @throws GooglePlayException |
|
34
|
|
|
* |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function __invoke(RequestInterface $request, ResponseInterface $response) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$query = parse_query($request->getUri()->getQuery()); |
|
40
|
2 |
|
$developerId = $query[GPlayApps::REQ_PARAM_ID]; |
|
41
|
2 |
|
$url = (string) $request->getUri()->withQuery(http_build_query([GPlayApps::REQ_PARAM_ID => $developerId])); |
|
42
|
|
|
|
|
43
|
2 |
|
$scriptDataInfo = $this->getScriptDataInfo($request, $response); |
|
44
|
|
|
|
|
45
|
2 |
|
$name = $scriptDataInfo[0][0][0]; |
|
46
|
|
|
|
|
47
|
2 |
|
$cover = empty($scriptDataInfo[0][9][0][3][2]) ? |
|
48
|
|
|
null : |
|
49
|
2 |
|
new GoogleImage($scriptDataInfo[0][9][0][3][2]); |
|
50
|
2 |
|
$icon = empty($scriptDataInfo[0][9][1][3][2]) ? |
|
51
|
|
|
null : |
|
52
|
2 |
|
new GoogleImage($scriptDataInfo[0][9][1][3][2]); |
|
53
|
2 |
|
$developerSite = $scriptDataInfo[0][9][2][0][5][2] ?? null; |
|
54
|
2 |
|
$description = $scriptDataInfo[0][10][1][1] ?? ''; |
|
55
|
|
|
|
|
56
|
2 |
|
return new Developer( |
|
57
|
2 |
|
Developer::newBuilder() |
|
58
|
2 |
|
->setId($developerId) |
|
59
|
2 |
|
->setUrl($url) |
|
60
|
2 |
|
->setName($name) |
|
61
|
2 |
|
->setDescription($description) |
|
62
|
2 |
|
->setWebsite($developerSite) |
|
63
|
2 |
|
->setIcon($icon) |
|
64
|
2 |
|
->setCover($cover) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param RequestInterface $request |
|
70
|
|
|
* @param ResponseInterface $response |
|
71
|
|
|
* |
|
72
|
|
|
* @throws GooglePlayException |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
2 |
|
private function getScriptDataInfo(RequestInterface $request, ResponseInterface $response): array |
|
77
|
|
|
{ |
|
78
|
2 |
|
$scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
|
79
|
|
|
|
|
80
|
2 |
|
$scriptDataInfo = null; |
|
81
|
|
|
|
|
82
|
2 |
|
foreach ($scriptData as $key => $scriptValue) { |
|
83
|
2 |
|
if (isset($scriptValue[0][21])) { |
|
84
|
2 |
|
$scriptDataInfo = $scriptValue; // ds:5 |
|
85
|
2 |
|
break; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
if ($scriptDataInfo === null) { |
|
90
|
|
|
throw (new GooglePlayException( |
|
91
|
|
|
sprintf( |
|
92
|
|
|
'Error parse vendor page %s. Need update library.', |
|
93
|
|
|
$request->getUri() |
|
94
|
|
|
) |
|
95
|
|
|
))->setUrl($request->getUri()->__toString()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return $scriptDataInfo; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|