Skip to content

Commit 0d6dafb

Browse files
committed
fix parseCategories(), now using local definitions
1 parent ca0e47f commit 0d6dafb

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

categories.jsonl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{"id": "ANDROID_WEAR", "name": "Wear OS by Google", "type": "app"}
2+
{"id": "ART_AND_DESIGN", "name": "Art & Design", "type": "app"}
3+
{"id": "AUTO_AND_VEHICLES", "name": "Auto & Vehicles", "type": "app"}
4+
{"id": "BEAUTY", "name": "Beauty", "type": "app"}
5+
{"id": "BOOKS_AND_REFERENCE", "name": "Books & Reference", "type": "app"}
6+
{"id": "BUSINESS", "name": "Business", "type": "app"}
7+
{"id": "COMICS", "name": "Comics", "type": "app"}
8+
{"id": "COMMUNICATION", "name": "Communication", "type": "app"}
9+
{"id": "DATING", "name": "Dating", "type": "app"}
10+
{"id": "EDUCATION", "name": "Education", "type": "app"}
11+
{"id": "ENTERTAINMENT", "name": "Entertainment", "type": "app"}
12+
{"id": "EVENTS", "name": "Events", "type": "app"}
13+
{"id": "FINANCE", "name": "Finance", "type": "app"}
14+
{"id": "FOOD_AND_DRINK", "name": "Food & Drink", "type": "app"}
15+
{"id": "HEALTH_AND_FITNESS", "name": "Health & Fitness", "type": "app"}
16+
{"id": "HOUSE_AND_HOME", "name": "House & Home", "type": "app"}
17+
{"id": "LIBRARIES_AND_DEMO", "name": "Libraries & Demo", "type": "app"}
18+
{"id": "LIFESTYLE", "name": "Lifestyle", "type": "app"}
19+
{"id": "MAPS_AND_NAVIGATION", "name": "Maps & Navigation", "type": "app"}
20+
{"id": "MEDIA_AND_VIDEO", "name": "Media & Video", "type": "app"}
21+
{"id": "MEDICAL", "name": "Medical", "type": "app"}
22+
{"id": "MUSIC_AND_AUDIO", "name": "Music & Audio", "type": "app"}
23+
{"id": "NEWS_AND_MAGAZINES", "name": "News & Magazines", "type": "app"}
24+
{"id": "PARENTING", "name": "Parenting", "type": "app"}
25+
{"id": "PERSONALIZATION", "name": "Personalisation", "type": "app"}
26+
{"id": "PHOTOGRAPHY", "name": "Photography", "type": "app"}
27+
{"id": "PRODUCTIVITY", "name": "Productivity", "type": "app"}
28+
{"id": "SHOPPING", "name": "Shopping", "type": "app"}
29+
{"id": "SOCIAL", "name": "Social", "type": "app"}
30+
{"id": "SPORTS", "name": "Sports", "type": "app"}
31+
{"id": "TOOLS", "name": "Tools", "type": "app"}
32+
{"id": "TRANSPORTATION", "name": "Transportation", "type": "app"}
33+
{"id": "TRAVEL_AND_LOCAL", "name": "Travel & Local", "type": "app"}
34+
{"id": "VIDEO_PLAYERS", "name": "Players & Editors", "type": "app"}
35+
{"id": "WEATHER", "name": "Weather", "type": "app"}
36+
{"id": "APP_WIDGETS", "name": "App Widgets", "type": "app"}
37+
{"id": "GAME", "name": "Game", "type": "game"}
38+
{"id": "GAME_ACTION", "name": "Action", "type": "game"}
39+
{"id": "GAME_ADVENTURE", "name": "Adventure", "type": "game"}
40+
{"id": "GAME_ARCADE", "name": "Arcade", "type": "game"}
41+
{"id": "GAME_BOARD", "name": "Board", "type": "game"}
42+
{"id": "GAME_CARD", "name": "Card", "type": "game"}
43+
{"id": "GAME_CASINO", "name": "Casino", "type": "game"}
44+
{"id": "GAME_CASUAL", "name": "Casual", "type": "game"}
45+
{"id": "GAME_EDUCATIONAL", "name": "Educational", "type": "game"}
46+
{"id": "GAME_MUSIC", "name": "Music", "type": "game"}
47+
{"id": "GAME_PUZZLE", "name": "Puzzle", "type": "game"}
48+
{"id": "GAME_RACING", "name": "Racing", "type": "game"}
49+
{"id": "GAME_ROLE_PLAYING", "name": "Role Playing", "type": "game"}
50+
{"id": "GAME_SIMULATION", "name": "Simulation", "type": "game"}
51+
{"id": "GAME_SPORTS", "name": "Sports", "type": "game"}
52+
{"id": "GAME_STRATEGY", "name": "Strategy", "type": "game"}
53+
{"id": "GAME_TRIVIA", "name": "Trivia", "type": "game"}
54+
{"id": "GAME_WORD", "name": "Word", "type": "game"}
55+
{"id": "FAMILY", "name": "Family", "type": "family"}
56+
{"id": "FAMILY_ACTION", "name": "Family Action & Adventure", "type": "family"}
57+
{"id": "FAMILY_BRAINGAMES", "name": "Family Brain Games", "type": "family"}
58+
{"id": "FAMILY_CREATE", "name": "Family Creativity", "type": "family"}
59+
{"id": "FAMILY_EDUCATION", "name": "Family Education", "type": "family"}
60+
{"id": "FAMILY_MUSICVIDEO", "name": "Family Music & Video", "type": "family"}
61+
{"id": "FAMILY_PRETEND", "name": "Family Pretend Play", "type": "family"}

google-play.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class GooglePlay {
1313
private $debug = false; // toggle debug output
1414
private $input = ''; // content retrieved from remote
1515
private $lastError = '';
16+
private $categories = []; // list of Google Play app categories
1617

1718
/** Turn debug mode on or off
1819
* @method public setDebug
@@ -349,7 +350,7 @@ public function parseNewApps() {
349350
/** Parse Play Store page for a given category and return package names
350351
* use this::parseCategories to obtain a list of available categories
351352
* @method public parseCategory
352-
* @param string category name of the category to parse
353+
* @param string category id of the category to parse
353354
* @return array array of package names
354355
*/
355356
public function parseCategory($category) {
@@ -359,15 +360,24 @@ public function parseCategory($category) {
359360
else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data];
360361
}
361362

362-
/** Obtain list of available categories
363+
/** Obtain list of available categories.
364+
* Definitions of all available categories are stored in categories.jsonl using
365+
* [JSONL](https://en.wikipedia.org/wiki/JSONL) format. This method returns them
366+
* as array of objects with the ID as key and the properties id, name, type.
363367
* @method public parseCategories
364-
* @return array array[0..n] of category names to be used with this::parseCategory
368+
* @return array array of categories to be used with e.g. this::parseCategory
369+
* @see https://developers.apptweak.com/reference/google-play-store-categories
365370
*/
366-
public function parseCategories() {
367-
if ( ! $this->getApplicationPage('com.google.android.gm','en','US') )
368-
return ['success'=>0, 'message'=>$this->lastError, 'data'=>[]];
369-
preg_match_all('!href="/store/apps/category/([^"]+)"[^>]*>([^<]+)!i', $this->input, $cats);
370-
return ['success'=>1, 'message'=>'', 'data'=>array_unique($cats[1])];
371+
public function parseCategories($force=false) {
372+
if ( ! empty($this->categories) && ! $force ) ['success'=>1, 'message'=>'', 'data'=>$this->categories];
373+
$catfile = __DIR__ . '/categories.jsonl';
374+
if ( ! file_exists($catfile) ) return ['success'=>0, 'message'=>"Category definition file '$catfile' does not exist.", 'data'=>[]];
375+
$this->categories = [];
376+
foreach ( file($catfile) as $line ) {
377+
$cat = json_decode($line);
378+
$this->categories[$cat->id] = $cat;
379+
}
380+
return ['success'=>1, 'message'=>'', 'data'=>$this->categories];
371381
}
372382

373383
/** Obtain list of similar apps

0 commit comments

Comments
 (0)