Unify domain names in README
[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, 'game/' . $htmlFile)
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, 'discover/' . $htmlFile)
46     );
47 }
48
49 file_put_contents(
50     $wwwDiscoverDir . 'allgames.htm',
51     renderAllGamesList(glob($gameDetailsDir . '*.json'), 'discover/allgames.htm')
52 );
53
54
55 function renderAllGamesList($detailsFiles, $path)
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     $canonicalUrl = $GLOBALS['baseUrl'] . $path;
81
82     $allGamesTemplate = __DIR__ . '/../data/templates/allgames.tpl.php';
83     ob_start();
84     include $allGamesTemplate;
85     $html = ob_get_contents();
86     ob_end_clean();
87
88     return $html;
89 }
90
91 function renderDiscoverFile($discoverFile, $path)
92 {
93     $json = json_decode(file_get_contents($discoverFile));
94
95     $title    = $json->title . ' OUYA games';
96     $subtitle = $json->stouyapi->subtitle ?? null;
97
98     $sections = [];
99     foreach ($json->rows as $row) {
100         $section = (object) [
101             'title' => $row->title,
102             'tiles' => [],
103         ];
104         foreach ($row->tiles as $tileId) {
105             $tileData = $json->tiles[$tileId];
106             if ($tileData->type == 'app') {
107                 $section->tiles[] = (object) [
108                     'type'        => $tileData->type,//app
109                     'thumb'       => $tileData->image,
110                     'title'       => $tileData->title,
111                     'rating'      => $tileData->rating->average,
112                     'ratingCount' => $tileData->rating->count,
113                     'detailUrl'   => '../game/' . str_replace(
114                         'ouya://launcher/details?app=',
115                         '',
116                         $tileData->url
117                     ) . '.htm',
118                 ];
119             } else {
120                 $section->tiles[] = (object) [
121                     'type'        => $tileData->type,//discover
122                     'thumb'       => $tileData->image,
123                     'title'       => $tileData->title,
124                     'detailUrl'   => str_replace(
125                         'ouya://launcher/discover/',
126                         '',
127                         $tileData->url
128                     ) . '.htm',
129                 ];
130             }
131         }
132         $sections[] = $section;
133     }
134
135     $navLinks = [];
136     if ($json->title == 'DISCOVER') {
137         $navLinks['../'] = 'back';
138         $navLinks['allgames.htm'] = 'all games';
139         $title = 'OUYA games list';
140     } else {
141         $navLinks['./'] = 'discover';
142     }
143
144     if ($path === 'discover/index.htm') {
145         $path = 'discover/';
146     }
147     $canonicalUrl = $GLOBALS['baseUrl'] . $path;
148
149     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
150     ob_start();
151     include $discoverTemplate;
152     $html = ob_get_contents();
153     ob_end_clean();
154
155     return $html;
156 }
157
158 function renderGameFile($gameDataFile, $path)
159 {
160     $json = json_decode(file_get_contents($gameDataFile));
161
162     $appsDir = dirname($gameDataFile, 2) . '/apps/';
163     $appsFile = $appsDir . $json->version->uuid . '.json';
164     $appsJson = json_decode(file_get_contents($appsFile));
165
166     $downloadJson = json_decode(
167         file_get_contents(
168             $appsDir . $json->version->uuid . '-download.json'
169         )
170     );
171
172     $apkDownloadUrl = $downloadJson->app->downloadLink;
173     /*
174     if (isset($json->premium) && $json->premium) {
175         $apkDownloadUrl = null;
176     }
177     */
178     $developerDetailsUrl = null;
179     if (isset($json->developer->url) && $json->developer->url) {
180         $developerDetailsUrl = '../discover/' . str_replace(
181             'ouya://launcher/discover/',
182             '',
183             $json->developer->url
184         ) . '.htm';
185     }
186
187     $internetArchiveUrl = $json->stouyapi->{'internet-archive'} ?? null;
188     $developerUrl       = $json->stouyapi->{'developer-url'} ?? null;
189     $canonicalUrl       = $GLOBALS['baseUrl'] . $path;
190
191     $pushUrl = $GLOBALS['pushToMyOuyaUrl']
192         . '?game=' . urlencode($json->apk->package);
193
194     $navLinks = [];
195     foreach ($json->genres as $genreTitle) {
196         $url = '../discover/' . categoryPath($genreTitle) . '.htm';
197         $navLinks[$url] = $genreTitle;
198     }
199
200     $gameTemplate = __DIR__ . '/../data/templates/game.tpl.php';
201     ob_start();
202     include $gameTemplate;
203     $html = ob_get_contents();
204     ob_end_clean();
205
206     return $html;
207 }
208 ?>