|
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\Extractor; |
|
15
|
|
|
|
|
16
|
|
|
use Nelexa\GPlay\Model\App; |
|
17
|
|
|
use Nelexa\GPlay\Model\GoogleImage; |
|
18
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
class AppsExtractor |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param array $data |
|
27
|
|
|
* @param string $locale |
|
28
|
|
|
* @param string $country |
|
29
|
|
|
* |
|
30
|
|
|
* @return App |
|
31
|
|
|
*/ |
|
32
|
21 |
|
public static function extractApp(array $data, string $locale, string $country): App |
|
33
|
|
|
{ |
|
34
|
21 |
|
$name = $data[3]; |
|
35
|
21 |
|
$appId = $data[0][0]; |
|
36
|
21 |
|
$icon = new GoogleImage($data[1][3][2]); |
|
37
|
21 |
|
$developerName = $data[14]; |
|
38
|
21 |
|
$installsText = $data[15]; |
|
39
|
21 |
|
$priceText = null; |
|
40
|
21 |
|
if (isset($data[8][1][0][0], $data[8][1][0][2]) && $data[8][1][0][0] > 0) { |
|
41
|
9 |
|
$priceText = $data[8][1][0][2]; |
|
42
|
|
|
} |
|
43
|
21 |
|
$score = $data[4][1] ?? 0.0; |
|
44
|
21 |
|
$screenshots = array_values(array_map(static function (array $item): GoogleImage { |
|
45
|
21 |
|
return new GoogleImage($item[3][2]); |
|
46
|
21 |
|
}, array_filter($data[2], static function ($item): bool { |
|
47
|
21 |
|
return isset($item[3][2]); |
|
48
|
21 |
|
}))); |
|
49
|
21 |
|
$description = ScraperUtil::html2text($data[13][1] ?? ''); |
|
50
|
21 |
|
$cover = null; |
|
51
|
21 |
|
if (isset($data[22][3][2])) { |
|
52
|
21 |
|
$cover = new GoogleImage($data[22][3][2]); |
|
53
|
|
|
} elseif (isset($data[100][1][0][3][2])) { |
|
54
|
|
|
$cover = new GoogleImage($data[100][1][0][3][2]); |
|
55
|
|
|
} |
|
56
|
|
|
// $categoryName = $data[0][5]; |
|
57
|
|
|
|
|
58
|
21 |
|
return App::newBuilder() |
|
59
|
21 |
|
->setId($appId) |
|
60
|
21 |
|
->setLocale($locale) |
|
61
|
21 |
|
->setCountry($country) |
|
62
|
21 |
|
->setName($name) |
|
63
|
21 |
|
->setDeveloperName($developerName) |
|
64
|
21 |
|
->setCover($cover) |
|
65
|
21 |
|
->setIcon($icon) |
|
66
|
21 |
|
->setInstallsText($installsText) |
|
67
|
21 |
|
->setScore($score) |
|
68
|
21 |
|
->setPriceText($priceText) |
|
69
|
21 |
|
->setScreenshots($screenshots) |
|
70
|
21 |
|
->setDescription($description) |
|
71
|
21 |
|
->build() |
|
72
|
21 |
|
; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
9 |
|
public static function isAppStructure(array $data): bool |
|
76
|
|
|
{ |
|
77
|
9 |
|
return |
|
78
|
9 |
|
isset( |
|
79
|
9 |
|
$data[0][0], // package id |
|
80
|
9 |
|
$data[1][3][2], // icon |
|
81
|
9 |
|
$data[3], // app name |
|
82
|
9 |
|
$data[14] // developer name |
|
83
|
9 |
|
) && \is_string($data[0][0]) |
|
84
|
9 |
|
&& \is_string($data[1][3][2]) |
|
85
|
9 |
|
&& \is_string($data[3]) |
|
86
|
9 |
|
&& \is_string($data[14]) |
|
87
|
9 |
|
; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param mixed $items |
|
92
|
|
|
* @param string $locale |
|
93
|
|
|
* @param string $country |
|
94
|
|
|
* |
|
95
|
|
|
* @return App[] |
|
96
|
|
|
*/ |
|
97
|
9 |
|
public static function extractApps($items, string $locale, string $country): array |
|
98
|
|
|
{ |
|
99
|
9 |
|
$apps = []; |
|
100
|
|
|
|
|
101
|
9 |
|
if (\is_array($items)) { |
|
102
|
9 |
|
foreach ($items as $item) { |
|
103
|
9 |
|
if (self::isAppStructure($item)) { |
|
104
|
8 |
|
$apps[] = self::extractApp($item, $locale, $country); |
|
105
|
2 |
|
} elseif (isset($item[0]) && self::isAppStructure($item[0])) { |
|
106
|
2 |
|
$apps[] = self::extractApp($item[0], $locale, $country); |
|
107
|
|
|
} elseif (isset($item[0][0]) && self::isAppStructure($item[0][0])) { |
|
108
|
|
|
$apps[] = self::extractApp($item[0][0], $locale, $country); |
|
109
|
|
|
} elseif (isset($item[0][0][0]) && self::isAppStructure($item[0][0][0])) { |
|
110
|
|
|
$apps[] = self::extractApp($item[0][0][0], $locale, $country); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
9 |
|
return $apps; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|