HTML frontend: Link developer URL, add favicon
[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 $wwwDir = __DIR__ . '/../www/';
11 $discoverDir = __DIR__ . '/../www/api/v1/discover-data/';
12 $wwwDiscoverDir = $wwwDir . 'discover/';
13 $gameDetailsDir = __DIR__ . '/../www/api/v1/details-data/';
14 $wwwGameDir = $wwwDir . 'game/';
15
16 if (!is_dir($wwwDiscoverDir)) {
17     mkdir($wwwDiscoverDir, 0755);
18 }
19 if (!is_dir($wwwGameDir)) {
20     mkdir($wwwGameDir, 0755);
21 }
22
23 foreach (glob($gameDetailsDir . '*.json') as $gameDataFile) {
24     $htmlFile = basename($gameDataFile, '.json') . '.htm';
25     file_put_contents(
26         $wwwGameDir . $htmlFile,
27         renderGameFile($gameDataFile)
28     );
29 }
30
31 foreach (glob($discoverDir . '*.json') as $discoverFile) {
32     $htmlFile = basename($discoverFile, '.json') . '.htm';
33     if ($htmlFile == 'discover.htm') {
34         $htmlFile = 'index.htm';
35     }
36     file_put_contents(
37         $wwwDiscoverDir . $htmlFile,
38         renderDiscoverFile($discoverFile)
39     );
40 }
41
42 function renderDiscoverFile($discoverFile)
43 {
44     $json = json_decode(file_get_contents($discoverFile));
45
46     $title    = $json->title;
47     $sections = [];
48     foreach ($json->rows as $row) {
49         $section = (object) [
50             'title' => $row->title,
51             'tiles' => [],
52         ];
53         foreach ($row->tiles as $tileId) {
54             $tileData = $json->tiles[$tileId];
55             if ($tileData->type == 'app') {
56                 $section->tiles[] = (object) [
57                     'type'        => $tileData->type,//app
58                     'thumb'       => $tileData->image,
59                     'title'       => $tileData->title,
60                     'rating'      => $tileData->rating->average,
61                     'ratingCount' => $tileData->rating->count,
62                     'detailUrl'   => '../game/' . str_replace(
63                         'ouya://launcher/details?app=',
64                         '',
65                         $tileData->url
66                     ) . '.htm',
67                 ];
68             } else {
69                 $section->tiles[] = (object) [
70                     'type'        => $tileData->type,//discover
71                     'thumb'       => $tileData->image,
72                     'title'       => $tileData->title,
73                     'detailUrl'   => str_replace(
74                         'ouya://launcher/discover/',
75                         '',
76                         $tileData->url
77                     ) . '.htm',
78                 ];
79             }
80         }
81         $sections[] = $section;
82     }
83
84     $navLinks = [];
85     if ($title == 'DISCOVER') {
86         $navLinks['../'] = 'back';
87     } else {
88         $navLinks['./'] = 'discover';
89     }
90
91     $discoverTemplate = __DIR__ . '/../data/templates/discover.tpl.php';
92     ob_start();
93     include $discoverTemplate;
94     $html = ob_get_contents();
95     ob_end_clean();
96
97     return $html;
98 }
99
100 function renderGameFile($gameDataFile)
101 {
102     $json = json_decode(file_get_contents($gameDataFile));
103
104     $appsDir = dirname($gameDataFile, 2) . '/apps/';
105     $appsFile = $appsDir . $json->version->uuid . '.json';
106     $appsJson = json_decode(file_get_contents($appsFile));
107
108     $downloadJson = json_decode(
109         file_get_contents(
110             $appsDir . $json->version->uuid . '-download.json'
111         )
112     );
113     $apkDownloadUrl = $downloadJson->app->downloadLink;
114
115     $navLinks = [];
116     foreach ($json->genres as $genreTitle) {
117         $url = '../discover/' . categoryPath($genreTitle) . '.htm';
118         $navLinks[$url] = $genreTitle;
119     }
120
121     $gameTemplate = __DIR__ . '/../data/templates/game.tpl.php';
122     ob_start();
123     include $gameTemplate;
124     $html = ob_get_contents();
125     ob_end_clean();
126
127     return $html;
128 }
129 ?>