Skip to content

Commit f3b2b8e

Browse files
authored
add lang & loc params to parseApplication and parsePerms (see #8) #9
Merge pull request #9 from IzzySoft/lang
2 parents d5b702d + a7b9a5f commit f3b2b8e

File tree

2 files changed

+37
-78
lines changed

2 files changed

+37
-78
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ The `grouped` keys are the IDs given to the permission groups by Google. The emp
223223

224224
For results on failure, please see above.
225225

226+
### Obtaining language-specific content
227+
You want descriptions in your own language? The `parseApplication()` method accepts two optional parameters: your language, and your location (eg for currency). Both should be according to [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes), e.g. `en` for English, `de` for German, `fr` for French. The method `parsePerms()` also expects a single language parameter, again using ISO 639-1 specifications.
228+
229+
Note that due to technical limitation, not everything is translated.
230+
226231

227232
### TODO
228233

google-play.php

Lines changed: 32 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,73 +11,15 @@
1111
**/
1212
class GooglePlay {
1313
private $debug=false;
14-
private $categories=[
15-
"app"=>[
16-
"Art & Design",
17-
"Augmented Reality",
18-
"Auto & Vehicles",
19-
"Beauty",
20-
"Books & Reference",
21-
"Business",
22-
"Comics",
23-
"Communication",
24-
"Dating",
25-
"Daydream",
26-
"Education",
27-
"Entertainment",
28-
"Events",
29-
"Finance",
30-
"Food & Drink",
31-
"Health & Fitness",
32-
"House & Home",
33-
"Libraries & Demo",
34-
"Lifestyle",
35-
"Maps & Navigation",
36-
"Medical",
37-
"Music & Audio",
38-
"News & Magazines",
39-
"Parenting",
40-
"Personalization",
41-
"Photography",
42-
"Productivity",
43-
"Shopping",
44-
"Social",
45-
"Sports",
46-
"Tools",
47-
"Travel & Local",
48-
"Video Players & Editors",
49-
"Wear OS by Google",
50-
"Weather",
51-
],
52-
"game"=>[
53-
"Action",
54-
"Adventure",
55-
"Arcade",
56-
"Board",
57-
"Card",
58-
"Casino",
59-
"Casual",
60-
"Educational",
61-
"Music",
62-
"Puzzle",
63-
"Racing",
64-
"Role Playing",
65-
"Simulation",
66-
"Sports",
67-
"Strategy",
68-
"Trivia",
69-
"Word",
70-
],
71-
];
7214

7315
protected function getRegVal($regEx) {
7416
preg_match($regEx, $this->input, $res);
7517
if(isset($res["content"])) return trim($res["content"]);
7618
else return null;
7719
}
7820

79-
public function parseApplication($packageName) {
80-
$link="https://play.google.com/store/apps/details?id=".$packageName."&hl=en_US&gl=US";
21+
public function parseApplication($packageName,$lang='en_US',$loc='US') {
22+
$link="https://play.google.com/store/apps/details?id=".$packageName."&hl=$lang&gl=$loc";
8123
if ( ! $this->input = @file_get_contents($link) ) {
8224
return ['success'=>0,'message'=>'Google returned: '.$http_response_header[0]];
8325
}
@@ -94,14 +36,10 @@ public function parseApplication($packageName) {
9436
preg_match('/itemprop="genre" href="\/store\/apps\/category\/(?<id>[^\"]+)"([^\>]+|)>(?<content>[^\<]+)<\/a><\/span>/i', $this->input, $category);
9537
if(isset($category["id"], $category["content"])) {
9638
$values["category"]=trim(strip_tags($category["content"]));
97-
$isGame=false;
98-
foreach($this->categories["game"] as $game) {
99-
if(strtolower($values["category"]) == strtolower($game)) {
100-
$isGame=true;
101-
break;
102-
}
103-
}
104-
$values["type"]=$isGame ? "game" : "app";
39+
$catId=trim(strip_tags($category["id"]));
40+
if($catId=='GAME' || substr($catId,0,5)=='GAME_') $values["type"]="game";
41+
elseif($catId=='FAMILY' || substr($catId,0,7)=='FAMILY?') $values["type"]="family";
42+
else $values["type"]="app";
10543
} else {
10644
$values["category"]=null;
10745
$values["type"]=null;
@@ -130,15 +68,21 @@ public function parseApplication($packageName) {
13068
$values["images"]=null;
13169
}
13270

133-
$values["lastUpdated"] = strip_tags($this->getRegVal('/<div class="BgcNfc">Updated<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
134-
$values["versionName"] = strip_tags($this->getRegVal('/<div class="BgcNfc">Current Version<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
135-
$values["minimumSDKVersion"] = strip_tags($this->getRegVal('/<div class="hAyfc"><div class="BgcNfc">Requires Android<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
136-
$values["installs"] = strip_tags($this->getRegVal('/<div class="hAyfc"><div class="BgcNfc">Installs<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
137-
$values["age"] = strip_tags($this->getRegVal('/<div class="hAyfc"><div class="BgcNfc">Content Rating<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb"><div>(?<content>.*?)<\/div>/i'));
71+
if (substr(strtolower($lang),0,2)=='en') {
72+
$values["lastUpdated"] = strip_tags($this->getRegVal('/<div class="BgcNfc">Updated<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
73+
$values["versionName"] = strip_tags($this->getRegVal('/<div class="BgcNfc">Current Version<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
74+
$values["minimumSDKVersion"] = strip_tags($this->getRegVal('/<div class="hAyfc"><div class="BgcNfc">Requires Android<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
75+
$values["installs"] = strip_tags($this->getRegVal('/<div class="hAyfc"><div class="BgcNfc">Installs<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>.*?)<\/span><\/div><\/span><\/div>/i'));
76+
$values["age"] = strip_tags($this->getRegVal('/<div class="hAyfc"><div class="BgcNfc">Content Rating<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb"><div>(?<content>.*?)<\/div>/i'));
77+
$values["size"] = $this->getRegVal('/<div class="BgcNfc">Size<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>[^<]+)<\/span>/i');
78+
} else {
79+
$envals = $this->parseApplication($packageName);
80+
foreach(["lastUpdated","versionName","minimumSDKVersion","installs","age","size"] as $val) $values[$val]=$envals[$val];
81+
}
82+
13883
$values["rating"] = $this->getRegVal('/<div class="BHMmbe"[^>]*>(?<content>[^<]+)<\/div>/i');
13984
$values["votes"] = $this->getRegVal('/<span class="AYi5wd TBRnV"><span[^>]*>(?<content>[^>]+)<\/span>/i');
14085
$values["price"] = $this->getRegVal('/<meta itemprop="price" content="(?<content>[^"]+)">/i');
141-
$values["size"] = $this->getRegVal('/<div class="BgcNfc">Size<\/div><span class="htlgb"><div class="IQ1z0d"><span class="htlgb">(?<content>[^<]+)<\/span>/i');
14286

14387
if($this->debug) {
14488
print_r($values);
@@ -166,7 +110,7 @@ public function parse($link=null) {
166110
return $values;
167111
}
168112

169-
public function parsePerms($packageName) {
113+
public function parsePerms($packageName,$lang='en') {
170114
$opts = ['http' => array(
171115
'method' => 'POST',
172116
'header' => 'Content-type: application/x-www-form-urlencoded;charset=utf-8'
@@ -176,7 +120,7 @@ public function parsePerms($packageName) {
176120
)
177121
];
178122
$context = stream_context_create($opts);
179-
if ( $proto = @file_get_contents('https://play.google.com/_/PlayStoreUi/data/batchexecute?rpcids=xdSrCf&bl=boq_playuiserver_20201201.06_p0&hl=en&authuser&soc-app=121&soc-platform=1&soc-device=1&rt=c&f.sid=-8792622157958052111&_reqid=257685', false, $context) ) { // raw proto_buf data
123+
if ( $proto = @file_get_contents('https://play.google.com/_/PlayStoreUi/data/batchexecute?rpcids=xdSrCf&bl=boq_playuiserver_20201201.06_p0&hl='.$lang.'&authuser&soc-app=121&soc-platform=1&soc-device=1&rt=c&f.sid=-8792622157958052111&_reqid=257685', false, $context) ) { // raw proto_buf data
180124
preg_match("!HTTP/1\.\d\s+(\d{3})\s+(.+)$!i",$http_response_header[0],$match);
181125
$response_code = $match[1];
182126
switch ($response_code) {
@@ -194,11 +138,21 @@ public function parsePerms($packageName) {
194138

195139
$perms = $perms_unique = [];
196140
$json = preg_replace('!.*?(\[.+?\])\s*\d.*!ims','$1',$proto);
197-
$arr = json_decode($json)[0][2];
198-
foreach (json_decode($arr)[0] as $group) { // 0: group name, 1: group icon, 2: perms, 3: group_id
141+
$arr = json_decode(json_decode($json)[0][2]);
142+
if (!empty($arr[0])) foreach ($arr[0] as $group) { // 0: group name, 1: group icon, 2: perms, 3: group_id
143+
if (empty($group)) continue;
199144
$perms[$group[3][0]] = ['group_name'=>$group[0], 'perms'=>$group[2]];
200145
foreach($group[2] as $perm) $perms_unique[] = $perm[1];
201146
}
147+
if (!empty($arr[1])) {
148+
$perms['misc'] = ['group_name'=>$arr[1][0][0], 'perms'=>$arr[1][0][2]];
149+
foreach($arr[1][0][2] as $perm) $perms_unique[] = $perm[1];
150+
}
151+
if (!empty($arr[2])) {
152+
if (array_key_exists('misc',$perms)) $perms['misc']['perms'] = array_merge($perms['misc']['perms'],$arr[2]);
153+
else $perms['misc'] = ['group_name'=>$arr[1][0][0], 'perms'=>$arr[2]];
154+
foreach($arr[2] as $perm) $perms_unique[] = $perm[1];
155+
}
202156

203157
return ['success'=>1,'grouped'=>$perms,'perms'=>array_unique($perms_unique)];
204158
}

0 commit comments

Comments
 (0)