Link game pages to internet archive
[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             'suggestedAge' => $json->suggestedAge,
66             'apkVersion'   => $json->version->number,
67             'apkTimestamp' => $json->version->publishedAt,
68             'players'      => $json->gamerNumbers,
69             'detailUrl'    => '../game/' . str_replace(
70                         'ouya://launcher/details?app=',
71                         '',
72                         basename($gameDataFile, '.json')
73                     ) . '.htm',
74         ];
75     }
76     $navLinks = [
77         './' => 'back',
78     ];
79
80     $allGamesTemplate = __DIR__ . '/../data/templates/allgames.tpl.php';
81     ob_start();
82     include $allGamesTemplate;
83     $html = ob_get_contents();
84     ob_end_clean();
85
86     return $html;
87 }
88
89 function renderDiscoverFile($discoverFile)
90 {
91     $json = json_decode(file_get_contents($discoverFile));
92
93     $title    = $json->title . ' OUYA games';
94     $sections = [];
95     foreach ($json->rows as $row) {
96         $section = (object) [
97             'title' => $row->title,
98             'tiles' => [],
99         ];
100         foreach ($row->tiles as $tileId) {
101             $tileData = $json->tiles[$tileId];
102             if ($tileData->type == 'app') {
103                 $section->tiles[] = (object) [
104                     'type'        => $tileData->type,//app
105                     'thumb'       => $tileData->image,
106                     'title'       => $tileData->title,
107                     'rating'      => $tileData->rating->average,
108                     'ratingCount' => $tileData->rating->count,
109                     'detailUrl'   => '../game/' . str_replace(
110                         'ouya://launcher/details?app=',
111                         '',
112                         $tileData->url
113                     ) . '.htm',
114                 ];
115             } else {
116                 $section->tiles[] = (object) [
117                     'type'        => $tileData->type,//discover
118                     'thumb'       => $tileData->image,
119                     'title'       => $tileData->title,
120                     'detailUrl'   => str_replace(
121                         'ouya://launcher/discover/',
122                         '',
123                         $tileData->url
124                     ) . '.htm',
125                 ];
126             }
127         }
128         $sections[] = $section;
129     }
130
131     $navLinks = [];
132     if ($json->title == 'DISCOVER') {
133         $navLinks['../'] = 'back';
134         $navLinks['allgames.htm'] = 'all games';
135         $title = 'OUYA games list';
136     } else {
137         $navLinks['./'] = 'discover';
138     }
139
140     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
141     ob_start();
142     include $discoverTemplate;
143     $html = ob_get_contents();
144     ob_end_clean();
145
146     return $html;
147 }
148
149 function renderGameFile($gameDataFile)
150 {
151     $json = json_decode(file_get_contents($gameDataFile));
152
153     $appsDir = dirname($gameDataFile, 2) . '/apps/';
154     $appsFile = $appsDir . $json->version->uuid . '.json';
155     $appsJson = json_decode(file_get_contents($appsFile));
156
157     $downloadJson = json_decode(
158         file_get_contents(
159             $appsDir . $json->version->uuid . '-download.json'
160         )
161     );
162
163     $apkDownloadUrl = $downloadJson->app->downloadLink;
164     /*
165     if (isset($json->premium) && $json->premium) {
166         $apkDownloadUrl = null;
167     }
168     */
169
170     $internetArchiveUrl = $json->stouyapi->{'internet-archive'} ?? null;
171
172     $pushUrl = $GLOBALS['pushToMyOuyaUrl']
173         . '?game=' . urlencode($json->apk->package);
174
175     $navLinks = [];
176     foreach ($json->genres as $genreTitle) {
177         $url = '../discover/' . categoryPath($genreTitle) . '.htm';
178         $navLinks[$url] = $genreTitle;
179     }
180
181     $gameTemplate = __DIR__ . '/../data/templates/game.tpl.php';
182     ob_start();
183     include $gameTemplate;
184     $html = ob_get_contents();
185     ob_end_clean();
186
187     return $html;
188 }
189 ?>