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