-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
59 lines (51 loc) · 1.9 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
// variabbles
$tmdb_key = getenv('tmdb_key'); // the tmdb api key, stored as a Netlify secret
$base = getenv('URL'); // set by netlify
if(!empty($base)){
$base .= '/';
}
$output_dir = __DIR__.'/dist'; // also change in /netlify.toml
// function to recursively copy an entire directory (with all files and sub-directories) from on place to another
function copy_dir(string $src, string $dst){
$dir = opendir($src);
if(!is_dir($dst)){ // destination folder does not exists, let's create it
mkdir($dst);
}
while($file = readdir($dir)){ // go through all objects in the source folder
if(($file != '.' ) && ($file != '..')){
$src_path = $src.'/'.$file;
$dst_path = $dst.'/'.$file;
if(is_dir($src_path)){ // its's a folder
copy_dir($src_path, $dst_path);
}else{ // it's a file
copy($src_path, $dst_path);
}
}
}
closedir($dir);
}
// really, really basic templating engine. input some html and some variables and it will replace them and
function generateHTML(string $html, array $vars=[]){
foreach($vars as $var->$value){
$var = strtolower($var);
$var = preg_quote($var, '/');
$html = preg_replace('/{{\s*'.$var.'\s*}}/', $value, $html);
}
$html .= '<!-- generated: '.date("l jS \of F Y h:i:s A").' -->';
return $html;
}
// expects a destination path and some content and it will create a file
function saveFile(string $path, string $content){
return file_put_contents($path, $content);
}
function getPopularMovies(int $page=1){
global $tmdb_key;
$data = file_get_contents('https://api.themoviedb.org/3/movie/popular?api_key='.urlencode($tmdb_key).'&page='.urlencode($page));
return json_decode($data, true);
}
function getUpcomingMovies(int $page=1){
global $tmdb_key;
$data = file_get_contents('https://api.themoviedb.org/3/movie/upcoming?api_key='.urlencode($tmdb_key).'&page='.urlencode($page));
return json_decode($data, true);
}