Skip to content

Commit 9e3d46a

Browse files
committed
add iteration if protobuf was missing/incomplete
1 parent 64c851b commit 9e3d46a

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

google-play.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
*
1111
**/
1212
class GooglePlay {
13-
private $debug=false;
13+
private $debug = false; // toggle debug output
14+
private $input = ''; // content retrieved from remote
15+
private $lastError = '';
1416

1517
/** Parse a given RegEx and return the match marked by '(?<content>)'
1618
* @method protected getRegVal
@@ -23,6 +25,23 @@ protected function getRegVal($regEx) {
2325
else return null;
2426
}
2527

28+
/** Fetch app page from Google Play
29+
* @method protected getApplicationPage
30+
* @param string packageName identifier for the app, e.g. 'com.example.app'
31+
* @param optional string lang language for translations. Should be ISO 639-1 two-letter code. Default: en
32+
* @param optional string loc locale, mainly for currency. Again two-letter, but uppercase
33+
* @return bool success
34+
*/
35+
protected function getApplicationPage($packageName, $lang='en_US', $loc='US') {
36+
$link = "https://play.google.com/store/apps/details?id=" . $packageName . "&hl=$lang&gl=$loc";
37+
if ( ! $this->input = @file_get_contents($link) ) {
38+
$this->lastError = $http_response_header[0];
39+
return false;
40+
} else {
41+
return true;
42+
}
43+
}
44+
2645
/** Obtain details on a given app
2746
* @method public parseApplication
2847
* @param string packageName identifier for the app, e.g. 'com.example.app'
@@ -38,9 +57,8 @@ protected function getRegVal($regEx) {
3857
* if not explicitly specified otherwise, values are strings
3958
*/
4059
public function parseApplication($packageName, $lang='en_US', $loc='US') {
41-
$link = "https://play.google.com/store/apps/details?id=" . $packageName . "&hl=$lang&gl=$loc";
42-
if ( ! $this->input = @file_get_contents($link) ) {
43-
return ['success'=>0,'message'=>'Google returned: ' . $http_response_header[0]];
60+
if ( ! $this->getApplicationPage($packageName, $lang, $loc) ) {
61+
return ['success'=>0,'message'=>$this->lastError];
4462
}
4563
$values = [];
4664
$values["packageName"] = $packageName;
@@ -64,8 +82,7 @@ public function parseApplication($packageName, $lang='en_US', $loc='US') {
6482
$values["type"] = null;
6583
}
6684

67-
$proto = json_decode($this->getRegVal('/data:(?<content>\[\[\[.+?). sideChannel: .*?\);<\/script/ims'));
68-
$values["summary"] = $proto[0][10][1][1];
85+
$values["summary"] = '';
6986
$values["description"] = $this->getRegVal('/itemprop="description"><span jsslot><div jsname="sngebd">(?<content>.*?)<\/div><\/span><div/i');
7087
$values["icon"] = $this->getRegVal('/<div class="hkhL9e"><div class="xSyT2c"><img src="(?<content>[^\"]+)"/i');
7188
$values["featureGraphic"] = preg_replace('!(.*)=w\d+.*!i', '$1', $this->getRegVal('/<meta name="twitter:image" content="(?<content>[^\"]+)"/i'));
@@ -103,6 +120,18 @@ public function parseApplication($packageName, $lang='en_US', $loc='US') {
103120
$values["votes"] = $this->getRegVal('/<span class="AYi5wd TBRnV"><span[^>]*>(?<content>[^>]+)<\/span>/i');
104121
$values["price"] = $this->getRegVal('/<meta itemprop="price" content="(?<content>[^"]+)">/i');
105122

123+
$limit = 3;
124+
while ( empty($values["summary"]) && $limit > 0 ) { // sometimes protobuf is missing, but present again on subsequent call
125+
$proto = json_decode($this->getRegVal('/data:(?<content>\[\[\[.+?). sideChannel: .*?\);<\/script/ims'));
126+
if ( empty($proto[0][10]) ) {
127+
--$limit;
128+
$this->getApplicationPage($packageName, $lang, $loc);
129+
} else {
130+
$values["summary"] = $proto[0][10][1][1];
131+
break;
132+
}
133+
}
134+
106135
if ($this->debug) {
107136
print_r($values);
108137
}

0 commit comments

Comments
 (0)