Use developer URL in game details page and all games list
[stouyapi.git] / bin / build-html.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Take the generated JSON files and convert them to HTML for a browser
5  *
6  * @author Christian Weiske <cweiske@cweiske.de>
7  */
8 require_once __DIR__ . '/functions.php';
9
10 //default configuration values
11 $GLOBALS['pushToMyOuyaUrl'] = '../push-to-my-ouya.php';
12 $cfgFile = __DIR__ . '/../config.php';
13 if (file_exists($cfgFile)) {
14     include $cfgFile;
15 }
16
17 $wwwDir = __DIR__ . '/../www/';
18 $discoverDir = __DIR__ . '/../www/api/v1/discover-data/';
19 $wwwDiscoverDir = $wwwDir . 'discover/';
20 $gameDetailsDir = __DIR__ . '/../www/api/v1/details-data/';
21 $wwwGameDir = $wwwDir . 'game/';
22
23 if (!is_dir($wwwDiscoverDir)) {
24     mkdir($wwwDiscoverDir, 0755);
25 }
26 if (!is_dir($wwwGameDir)) {
27     mkdir($wwwGameDir, 0755);
28 }
29
30 foreach (glob($gameDetailsDir . '*.json') as $gameDataFile) {
31     $htmlFile = basename($gameDataFile, '.json') . '.htm';
32     file_put_contents(
33         $wwwGameDir . $htmlFile,
34         renderGameFile($gameDataFile)
35     );
36 }
37
38 foreach (glob($discoverDir . '*.json') as $discoverFile) {
39     $htmlFile = basename($discoverFile, '.json') . '.htm';
40     if ($htmlFile == 'discover.htm') {
41         $htmlFile = 'index.htm';
42     }
43     file_put_contents(
44         $wwwDiscoverDir . $htmlFile,
45         renderDiscoverFile($discoverFile)
46     );
47 }
48
49 file_put_contents(
50     $wwwDiscoverDir . 'allgames.htm',
51     renderAllGamesList(glob($gameDetailsDir . '*.json'))
52 );
53
54
55 function renderAllGamesList($detailsFiles)
56 {
57     $games = [];
58     foreach ($detailsFiles as $gameDataFile) {
59         $json = json_decode(file_get_contents($gameDataFile));
60         $games[] = (object) [
61             'packageName'  => basename($gameDataFile, '.json'),
62             'title'        => $json->title,
63             'genres'       => $json->genres,
64             'developer'    => $json->developer->name,
65             'developerUrl' => $json->stouyapi->{'developer-url'} ?? null,
66             'suggestedAge' => $json->suggestedAge,
67             'apkVersion'   => $json->version->number,
68             'apkTimestamp' => $json->version->publishedAt,
69             'players'      => $json->gamerNumbers,
70             'detailUrl'    => '../game/' . str_replace(
71                         'ouya://launcher/details?app=',
72                         '',
73                         basename($gameDataFile, '.json')
74                     ) . '.htm',
75         ];
76     }
77     $navLinks = [
78         './' => 'back',
79     ];
80
81     $allGamesTemplate = __DIR__ . '/../data/templates/allgames.tpl.php';
82     ob_start();
83     include $allGamesTemplate;
84     $html = ob_get_contents();
85     ob_end_clean();
86
87     return $html;
88 }
89
90 function renderDiscoverFile($discoverFile)
91 {
92     $json = json_decode(file_get_contents($discoverFile));
93
94     $title    = $json->title . ' OUYA games';
95     $sections = [];
96     foreach ($json->rows as $row) {
97         $section = (object) [
98             'title' => $row->title,
99             'tiles' => [],
100         ];
101         foreach ($row->tiles as $tileId) {
102             $tileData = $json->tiles[$tileId];
103             if ($tileData->type == 'app') {
104                 $section->tiles[] = (object) [
105                     'type'        => $tileData->type,//app
106                     'thumb'       => $tileData->image,
107                     'title'       => $tileData->title,
108                     'rating'      => $tileData->rating->average,
109                     'ratingCount' => $tileData->rating->count,
110                     'detailUrl'   => '../game/' . str_replace(
111                         'ouya://launcher/details?app=',
112                         '',
113                         $tileData->url
114                     ) . '.htm',
115                 ];
116             } else {
117                 $section->tiles[] = (object) [
118                     'type'        => $tileData->type,//discover
119                     'thumb'       => $tileData->image,
120                     'title'       => $tileData->title,
121                     'detailUrl'   => str_replace(
122                         'ouya://launcher/discover/',
123                         '',
124                         $tileData->url
125                     ) . '.htm',
126                 ];
127             }
128         }
129         $sections[] = $section;
130     }
131
132     $navLinks = [];
133     if ($json->title == 'DISCOVER') {
134         $navLinks['../'] = 'back';
135         $navLinks['allgames.htm'] = 'all games';
136         $title = 'OUYA games list';
137     } else {
138         $navLinks['./'] = 'discover';
139     }
140
141     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
142     ob_start();
143     include $discoverTemplate;
144     $html = ob_get_contents();
145     ob_end_clean();
146
147     return $html;
148 }
149
150 function renderGameFile($gameDataFile)
151 {
152     $json = json_decode(file_get_contents($gameDataFile));
153
154     $appsDir = dirname($gameDataFile, 2) . '/apps/';
155     $appsFile = $appsDir . $json->version->uuid . '.json';
156     $appsJson = json_decode(file_get_contents($appsFile));
157
158     $downloadJson = json_decode(
159         file_get_contents(
160             $appsDir . $json->version->uuid . '-download.json'
161         )
162     );
163
164     $apkDownloadUrl = $downloadJson->app->downloadLink;
165     /*
166     if (isset($json->premium) && $json->premium) {
167         $apkDownloadUrl = null;
168     }
169     */
170
171     $internetArchiveUrl = $json->stouyapi->{'internet-archive'} ?? null;
172     $developerUrl       = $json->stouyapi->{'developer-url'} ?? null;
173
174     $pushUrl = $GLOBALS['pushToMyOuyaUrl']
175         . '?game=' . urlencode($json->apk->package);
176
177     $navLinks = [];
178     foreach ($json->genres as $genreTitle) {
179         $url = '../discover/' . categoryPath($genreTitle) . '.htm';
180         $navLinks[$url] = $genreTitle;
181     }
182
183     $gameTemplate = __DIR__ . '/../data/templates/game.tpl.php';
184     ob_start();
185     include $gameTemplate;
186     $html = ob_get_contents();
187     ob_end_clean();
188
189     return $html;
190 }
191 ?>