Move weather fetching and cache handling to weather_inc.php
This commit is contained in:
parent
ed72cb87b1
commit
31881d5e69
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,4 +2,5 @@ error/
|
|||||||
stats/
|
stats/
|
||||||
vendor/
|
vendor/
|
||||||
settings.php
|
settings.php
|
||||||
*.log
|
*.log
|
||||||
|
database.mwb.bak
|
22
Dockerfile
22
Dockerfile
@ -1,12 +1,12 @@
|
|||||||
FROM php:5-apache
|
FROM php:5-apache
|
||||||
MAINTAINER Skylar Ittner <admin@netsyms.com>
|
MAINTAINER Skylar Ittner <admin@netsyms.com>
|
||||||
RUN apt-get update && apt-get upgrade -y
|
RUN apt-get update && apt-get upgrade -y
|
||||||
RUN apt-get install git openssh-server
|
RUN apt-get install git openssh-server
|
||||||
# nuke the webroot
|
# nuke the webroot
|
||||||
WORKDIR /var/www
|
WORKDIR /var/www
|
||||||
RUN rm -rf html && mkdir html
|
RUN rm -rf html && mkdir html
|
||||||
WORKDIR /var/www/html
|
WORKDIR /var/www/html
|
||||||
# install the server crap (private repo for now, thx GitHub Student)
|
# install the server crap (private repo for now, thx GitHub Student)
|
||||||
RUN git clone https://skylarmt-script-account:scriptb0t@github.com/skylarmt/TerranQuestServer.git .
|
RUN git clone https://skylarmt-script-account:scriptb0t@github.com/skylarmt/TerranQuestServer.git .
|
||||||
# 0wn3d
|
# 0wn3d
|
||||||
RUN cd .. && chown -R www-data:www-data html
|
RUN cd .. && chown -R www-data:www-data html
|
BIN
database.mwb.bak
BIN
database.mwb.bak
Binary file not shown.
@ -2,36 +2,9 @@
|
|||||||
|
|
||||||
require 'required.php';
|
require 'required.php';
|
||||||
require 'onlyloggedin.php';
|
require 'onlyloggedin.php';
|
||||||
|
require 'weather_inc.php';
|
||||||
|
|
||||||
// Validate input
|
$output['currently'] = $currently;
|
||||||
if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
|
|
||||||
sendError("Missing information.", true);
|
|
||||||
}
|
|
||||||
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) {
|
|
||||||
sendError("Latitude (lat) is in the wrong format.", true);
|
|
||||||
}
|
|
||||||
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) {
|
|
||||||
sendError("Longitude (long) is in the wrong format.", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Round to 2 digits (approx. 1.1km)
|
|
||||||
$lat = number_format((float) $VARS['lat'], 2, '.', '');
|
|
||||||
$long = number_format((float) $VARS['long'], 2, '.', '');
|
|
||||||
|
|
||||||
// Delete old records
|
|
||||||
$database->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
|
// Re-encode the data to JSON and send to client
|
||||||
echo json_encode($output);
|
echo json_encode($output);
|
@ -1,7 +1,7 @@
|
|||||||
include.path=${php.global.include.path}
|
include.path=${php.global.include.path}
|
||||||
php.version=PHP_54
|
php.version=PHP_54
|
||||||
source.encoding=UTF-8
|
source.encoding=UTF-8
|
||||||
src.dir=.
|
src.dir=.
|
||||||
tags.asp=false
|
tags.asp=false
|
||||||
tags.short=false
|
tags.short=false
|
||||||
web.root=.
|
web.root=.
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://www.netbeans.org/ns/project/1">
|
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||||
<type>org.netbeans.modules.php.project</type>
|
<type>org.netbeans.modules.php.project</type>
|
||||||
<configuration>
|
<configuration>
|
||||||
<data xmlns="http://www.netbeans.org/ns/php-project/1">
|
<data xmlns="http://www.netbeans.org/ns/php-project/1">
|
||||||
<name>TerranQuest Server</name>
|
<name>TerranQuest Server</name>
|
||||||
</data>
|
</data>
|
||||||
</configuration>
|
</configuration>
|
||||||
</project>
|
</project>
|
||||||
|
35
weather_inc.php
Normal file
35
weather_inc.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Handles all the hard weather db stuff, just provide $VARS lat and long.
|
||||||
|
*
|
||||||
|
* Get the contents of the current weather with $currently
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// Validate input
|
||||||
|
if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
|
||||||
|
sendError("Missing information.", true);
|
||||||
|
}
|
||||||
|
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) {
|
||||||
|
sendError("Latitude (lat) is in the wrong format.", true);
|
||||||
|
}
|
||||||
|
if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) {
|
||||||
|
sendError("Longitude (long) is in the wrong format.", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Round to 2 digits (approx. 1.1km)
|
||||||
|
$lat = number_format((float) $VARS['lat'], 2, '.', '');
|
||||||
|
$long = number_format((float) $VARS['long'], 2, '.', '');
|
||||||
|
|
||||||
|
// Delete old records
|
||||||
|
$database->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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the cached record and put it in a variable
|
||||||
|
$currently = json_decode($database->select('weathercache', 'currentjson', ["AND" => ["latitude" => $lat, "longitude" => $long]])[0], TRUE);
|
Reference in New Issue
Block a user