From 42473a388e4fa083d319d707f116c3bfa56f7402 Mon Sep 17 00:00:00 2001 From: Izzy Date: Thu, 10 Dec 2020 16:01:39 +0100 Subject: [PATCH] unifying returned results: always include "success" and "message", see #15 --- google-play.php | 110 ++++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 45 deletions(-) diff --git a/google-play.php b/google-play.php index a5b077e..3ce6837 100644 --- a/google-play.php +++ b/google-play.php @@ -5,8 +5,8 @@ * @author Max & Izzy * @copyright MIT https://github.com/BaseMax/GooglePlayWebServiceAPI/blob/master/LICENSE * @log 2020-10-19 first release - * @log 2020-12-07 recent version - * @brief releases: 2020-10-19, 2020-10-25, 2020-10-29, 2020-10-30, 2020-12-05, 2020-12-06 + * @log 2020-12-10 recent version + * @brief releases: 2020-10-19, 2020-10-25, 2020-10-29, 2020-10-30, 2020-12-05, 2020-12-06, 2020-12-07, 2020-12-10 * @webpage repository https://github.com/BaseMax/GooglePlayWebServiceAPI **/ class GooglePlay { @@ -78,6 +78,7 @@ public function parseApplication($packageName, $lang='en_US', $loc='US') { return ['success'=>0,'message'=>$this->lastError]; } $values = []; + $message = ''; $values["packageName"] = $packageName; $values["name"] = strip_tags($this->getRegVal('/itemprop="name">(?.*?)<\/h1>/')); @@ -161,35 +162,7 @@ public function parseApplication($packageName, $lang='en_US', $loc='US') { print_r($values); } $values['success'] = 1; - return $values; - } - - /** Parse page specified by URL for playstore links and extract package names - * @method public parse - * @param optional string link link to parse; if empty or not specified, defaults to 'https://play.google.com/apps' - * @param optional bool is_url whether the link passed is an url to fetch-and-parse (true, default) or a string just to parse (false) - * @return array array of package names - */ - public function parse($link=null, $is_url=true) { - if ( $is_url ) { - if ($link == "" || $link == null) { - $link = "https://play.google.com/apps"; - } - $input = file_get_contents($link); - } else { - $input = $link; - } - preg_match_all('/href="\/store\/apps\/details\?id=(?[^\"]+)"/i', $input, $ids); - if ( isset($ids["ids"]) ) { - $ids = $ids["ids"]; - $ids = array_values(array_unique($ids)); - $values = $ids; - } else { - $values = []; - } - if ($this->debug) { - print_r($values); - } + $values['message'] = $message; return $values; } @@ -250,7 +223,42 @@ public function parsePerms($packageName, $lang='en') { foreach($arr[2] as $perm) $perms_unique[] = $perm[1]; } - return ['success'=>1, 'grouped'=>$perms, 'perms'=>array_unique($perms_unique)]; + return ['success'=>1, 'message'=>'', 'grouped'=>$perms, 'perms'=>array_unique($perms_unique)]; + } + + /** Parse page specified by URL for playstore links and extract package names + * @method public parse + * @param optional string link link to parse; if empty or not specified, defaults to 'https://play.google.com/apps' + * @param optional bool is_url whether the link passed is an url to fetch-and-parse (true, default) or a string just to parse (false) + * @return array array of package names + * @brief this mainly is a helper for all methods parsing for app links, like parseTopApps, parseSimilar etc. + */ + public function parse($link=null, $is_url=true) { + if ( $is_url ) { + if ($link == "" || $link == null) { + $link = "https://play.google.com/apps"; + } + if ( ! $input = @file_get_contents($link) ) { + $this->lastError = $http_response_header[0]; + return []; + } else { + $this->lastError = ''; // reset + } + } else { + $input = $link; + } + preg_match_all('/href="\/store\/apps\/details\?id=(?[^\"]+)"/i', $input, $ids); + if ( isset($ids["ids"]) ) { + $ids = $ids["ids"]; + $ids = array_values(array_unique($ids)); + $values = $ids; + } else { + $values = []; + } + if ($this->debug) { + print_r($values); + } + return $values; } /** Obtain list of top apps @@ -260,7 +268,9 @@ public function parsePerms($packageName, $lang='en') { */ public function parseTopApps() { $link = "https://play.google.com/store/apps/top"; - return $this->parse($link); + $data = $this->parse($link); + if ( empty($this->lastError) ) return ['success'=>1, 'message'=>'', 'data'=>$data]; + else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data]; } /** Obtain list of newest apps @@ -270,7 +280,9 @@ public function parseTopApps() { */ public function parseNewApps() { $link = "https://play.google.com/store/apps/new"; - return $this->parse($link); + $data = $this->parse($link); + if ( empty($this->lastError) ) return ['success'=>1, 'message'=>'', 'data'=>$data]; + else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data]; } /** Parse Play Store page for a given category and return package names @@ -281,7 +293,9 @@ public function parseNewApps() { */ public function parseCategory($category) { $link = "https://play.google.com/store/apps/category/" . $category; - return $this->parse($link); + $data = $this->parse($link); + if ( empty($this->lastError) ) return ['success'=>1, 'message'=>'', 'data'=>$data]; + else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data]; } /** Obtain list of available categories @@ -289,9 +303,10 @@ public function parseCategory($category) { * @return array array[0..n] of category names to be used with this::parseCategory */ public function parseCategories() { - $input = file_get_contents('https://play.google.com/store/apps/details?id=com.google.android.gm&hl=en&gl=US'); - preg_match_all('!href="/store/apps/category/([^"]+)"[^>]*>([^<]+)!i', $input, $cats); - return array_unique($cats[1]); + if ( ! $this->getApplicationPage('com.google.android.gm','en','US') ) + return ['success'=>0, 'message'=>$this->lastError, 'data'=>[]]; + preg_match_all('!href="/store/apps/category/([^"]+)"[^>]*>([^<]+)!i', $this->input, $cats); + return ['success'=>1, 'message'=>'', 'data'=>array_unique($cats[1])]; } /** Obtain list of similar apps @@ -301,11 +316,11 @@ public function parseCategories() { */ public function parseSimilar($packageName) { if ( ! $this->getApplicationPage($packageName) ) - return ['success'=>0,'message'=>$this->lastError]; + return ['success'=>0, 'message'=>$this->lastError, 'data'=>[]]; $input = $this->getRegVal('!

Similar

(?.+?)()!ims'); if ( empty($input) ) - return ['success'=>0,'message'=>'no data found']; - return $this->parse($input, false); + return ['success'=>1, 'message'=>'no data found', 'data'=>[]]; + return ['success'=>1, 'message'=>'', 'data'=>$this->parse($input, false)]; } /** Obtain list of other apps by same author @@ -315,11 +330,11 @@ public function parseSimilar($packageName) { */ public function parseOthers($packageName) { if ( ! $this->getApplicationPage($packageName) ) - return ['success'=>0,'message'=>$this->lastError]; + return ['success'=>0, 'message'=>$this->lastError, 'data'=>[]]; $input = $this->getRegVal('!

More by [^<]*

(?.+?)
0,'message'=>'no data found']; - return $this->parse($input, false); + return ['success'=>1, 'message'=>'no data found', 'data'=>[]]; + return ['success'=>1, 'message'=>'', 'data'=>$this->parse($input, false)]; } /** Search for apps by a given string @@ -329,6 +344,11 @@ public function parseOthers($packageName) { */ public function parseSearch($query) { $link = "https://play.google.com/store/search?q=". urlencode($query) ."&c=apps"; - return $this->parse($link); + $data = $this->parse($link); + if ( empty($this->lastError) ) { + if ( empty($data) ) return ['success'=>1, 'message'=>'no data found', 'data'=>$data]; + else return ['success'=>1, 'message'=>'', 'data'=>$data]; + } + else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data]; } }