diff --git a/database.mwb b/database.mwb index 828c4d1..bbc2593 100644 Binary files a/database.mwb and b/database.mwb differ diff --git a/database.mwb.bak b/database.mwb.bak index 6f08e87..5d07e5f 100644 Binary files a/database.mwb.bak and b/database.mwb.bak differ diff --git a/getweather.php b/getweather.php new file mode 100644 index 0000000..29069f5 --- /dev/null +++ b/getweather.php @@ -0,0 +1,37 @@ +delete('weathercache', ["date[<]" => date('Y-m-d H:i:s', strtotime('-1 hour'))]); + +// If we don't get a cache hit, request from the API +if (!$database->has('weathercache', ["AND" => ["latitude" => $lat, "longitude" => $long]])) { + $weather = json_decode(file_get_contents("https://api.darksky.net/forecast/" . DARKSKY_APIKEY . "/$lat,$long"), TRUE); + $currentjson = json_encode($weather['currently']); + $database->insert('weathercache', ["latitude" => $lat, "longitude" => $long, "#date" => "NOW()", "currentjson" => $currentjson]); +} + +// Build output array +$output = ["status" => "OK", "currently" => []]; +// Get the cached record and put it in the output array +$output['currently'] = json_decode($database->select('weathercache', 'currentjson', ["AND" => ["latitude" => $lat, "longitude" => $long]])[0], TRUE); + +// Re-encode the data to JSON and send to client +echo json_encode($output); \ No newline at end of file