f7f915438d8bcff9b1fdc7b9a2c550277d2718f6
[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 function renderDiscoverFile($discoverFile)
50 {
51     $json = json_decode(file_get_contents($discoverFile));
52
53     $title    = $json->title;
54     $sections = [];
55     foreach ($json->rows as $row) {
56         $section = (object) [
57             'title' => $row->title,
58             'tiles' => [],
59         ];
60         foreach ($row->tiles as $tileId) {
61             $tileData = $json->tiles[$tileId];
62             if ($tileData->type == 'app') {
63                 $section->tiles[] = (object) [
64                     'type'        => $tileData->type,//app
65                     'thumb'       => $tileData->image,
66                     'title'       => $tileData->title,
67                     'rating'      => $tileData->rating->average,
68                     'ratingCount' => $tileData->rating->count,
69                     'detailUrl'   => '../game/' . str_replace(
70                         'ouya://launcher/details?app=',
71                         '',
72                         $tileData->url
73                     ) . '.htm',
74                 ];
75             } else {
76                 $section->tiles[] = (object) [
77                     'type'        => $tileData->type,//discover
78                     'thumb'       => $tileData->image,
79                     'title'       => $tileData->title,
80                     'detailUrl'   => str_replace(
81                         'ouya://launcher/discover/',
82                         '',
83                         $tileData->url
84                     ) . '.htm',
85                 ];
86             }
87         }
88         $sections[] = $section;
89     }
90
91     $navLinks = [];
92     if ($title == 'DISCOVER') {
93         $navLinks['../'] = 'back';
94     } else {
95         $navLinks['./'] = 'discover';
96     }
97
98     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
99     ob_start();
100     include $discoverTemplate;
101     $html = ob_get_contents();
102     ob_end_clean();
103
104     return $html;
105 }
106
107 function renderGameFile($gameDataFile)
108 {
109     $json = json_decode(file_get_contents($gameDataFile));
110
111     $appsDir = dirname($gameDataFile, 2) . '/apps/';
112     $appsFile = $appsDir . $json->version->uuid . '.json';
113     $appsJson = json_decode(file_get_contents($appsFile));
114
115     $downloadJson = json_decode(
116         file_get_contents(
117             $appsDir . $json->version->uuid . '-download.json'
118         )
119     );
120     $apkDownloadUrl = $downloadJson->app->downloadLink;
121     $pushUrl = $GLOBALS['pushToMyOuyaUrl']
122         . '?game=' . urlencode($json->apk->package);
123
124     $navLinks = [];
125     foreach ($json->genres as $genreTitle) {
126         $url = '../discover/' . categoryPath($genreTitle) . '.htm';
127         $navLinks[$url] = $genreTitle;
128     }
129
130     $gameTemplate = __DIR__ . '/../data/templates/game.tpl.php';
131     ob_start();
132     include $gameTemplate;
133     $html = ob_get_contents();
134     ob_end_clean();
135
136     return $html;
137 }
138 ?>