Better title
[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 . ' OUYA games';
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 ($json->title == 'DISCOVER') {
93         $navLinks['../'] = 'back';
94         $title = 'OUYA games list';
95     } else {
96         $navLinks['./'] = 'discover';
97     }
98
99     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
100     ob_start();
101     include $discoverTemplate;
102     $html = ob_get_contents();
103     ob_end_clean();
104
105     return $html;
106 }
107
108 function renderGameFile($gameDataFile)
109 {
110     $json = json_decode(file_get_contents($gameDataFile));
111
112     $appsDir = dirname($gameDataFile, 2) . '/apps/';
113     $appsFile = $appsDir . $json->version->uuid . '.json';
114     $appsJson = json_decode(file_get_contents($appsFile));
115
116     $downloadJson = json_decode(
117         file_get_contents(
118             $appsDir . $json->version->uuid . '-download.json'
119         )
120     );
121     $apkDownloadUrl = $downloadJson->app->downloadLink;
122     $pushUrl = $GLOBALS['pushToMyOuyaUrl']
123         . '?game=' . urlencode($json->apk->package);
124
125     $navLinks = [];
126     foreach ($json->genres as $genreTitle) {
127         $url = '../discover/' . categoryPath($genreTitle) . '.htm';
128         $navLinks[$url] = $genreTitle;
129     }
130
131     $gameTemplate = __DIR__ . '/../data/templates/game.tpl.php';
132     ob_start();
133     include $gameTemplate;
134     $html = ob_get_contents();
135     ob_end_clean();
136
137     return $html;
138 }
139 ?>