|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nelexa\GPlay\Scraper; |
|
5
|
|
|
|
|
6
|
|
|
use Nelexa\GPlay\Exception\GooglePlayException; |
|
7
|
|
|
use Nelexa\GPlay\GPlayApps; |
|
8
|
|
|
use Nelexa\GPlay\Http\ResponseHandlerInterface; |
|
9
|
|
|
use Nelexa\GPlay\Model\Developer; |
|
10
|
|
|
use Nelexa\GPlay\Model\GoogleImage; |
|
11
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
|
12
|
|
|
use Psr\Http\Message\RequestInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use function GuzzleHttp\Psr7\parse_query; |
|
15
|
|
|
|
|
16
|
|
|
class DeveloperInfoScraper implements ResponseHandlerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param RequestInterface $request |
|
20
|
|
|
* @param ResponseInterface $response |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
* @throws GooglePlayException |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response) |
|
25
|
|
|
{ |
|
26
|
|
|
$url = $request->getUri()->__toString(); |
|
27
|
|
|
$urlComponents = parse_url($url); |
|
28
|
|
|
$query = parse_query($urlComponents['query']); |
|
29
|
|
|
$developerId = $query[GPlayApps::REQ_PARAM_ID]; |
|
30
|
|
|
$url = $urlComponents['scheme'] . '://' |
|
31
|
|
|
. $urlComponents['host'] |
|
32
|
|
|
. $urlComponents['path'] |
|
33
|
|
|
. '?' . http_build_query([GPlayApps::REQ_PARAM_ID => $developerId]); |
|
34
|
|
|
|
|
35
|
|
|
$scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
|
36
|
|
|
|
|
37
|
|
|
$scriptDataInfo = null; |
|
38
|
|
|
foreach ($scriptData as $key => $scriptValue) { |
|
39
|
|
|
if (isset($scriptValue[0][21])) { |
|
40
|
|
|
$scriptDataInfo = $scriptValue; // ds:5 |
|
41
|
|
|
break; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
if ($scriptDataInfo === null) { |
|
45
|
|
|
throw (new GooglePlayException(sprintf( |
|
46
|
|
|
'Error parse vendor page %s. Need update library.', |
|
47
|
|
|
$request->getUri() |
|
48
|
|
|
)))->setUrl($request->getUri()->__toString()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$name = $scriptDataInfo[0][0][0]; |
|
52
|
|
|
|
|
53
|
|
|
$headerImage = empty($scriptDataInfo[0][9][0][3][2]) ? |
|
54
|
|
|
null : |
|
55
|
|
|
new GoogleImage($scriptDataInfo[0][9][0][3][2]); |
|
56
|
|
|
$icon = empty($scriptDataInfo[0][9][1][3][2]) ? |
|
57
|
|
|
null : |
|
58
|
|
|
new GoogleImage($scriptDataInfo[0][9][1][3][2]); |
|
59
|
|
|
$developerSite = $scriptDataInfo[0][9][2][0][5][2] ?? null; |
|
60
|
|
|
$description = $scriptDataInfo[0][10][1][1] ?? ''; |
|
61
|
|
|
|
|
62
|
|
|
return new Developer( |
|
63
|
|
|
Developer::newBuilder() |
|
64
|
|
|
->setId($developerId) |
|
65
|
|
|
->setUrl($url) |
|
66
|
|
|
->setName($name) |
|
67
|
|
|
->setDescription($description) |
|
68
|
|
|
->setWebsite($developerSite) |
|
69
|
|
|
->setIcon($icon) |
|
70
|
|
|
->setHeaderImage($headerImage) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|