Add sampledate column to weather DB table
This commit is contained in:
parent
695902ea1f
commit
aa4f4fb010
|
@ -17,10 +17,19 @@ class Weather {
|
||||||
. "&appid=" . WEATHERAPIKEY
|
. "&appid=" . WEATHERAPIKEY
|
||||||
. "&units=" . WEATHERUNITS;
|
. "&units=" . WEATHERUNITS;
|
||||||
|
|
||||||
public static function fetchWeather() {
|
public static function deleteOldWeather($days = 10) {
|
||||||
|
if ( !is_int($days) ) return false;
|
||||||
|
if ( $days < 0 ) return false;
|
||||||
|
$query = "DELETE FROM " . AppDB::TABLE_WEATHER . " WHERE DATEDIFF(NOW(), sampledate) > :days";
|
||||||
|
$sth = AppDB::getSTH($query);
|
||||||
|
if ( $sth === false ) return false;
|
||||||
|
$sth->bindValue(":days", $days, PDO::PARAM_INT);
|
||||||
|
$sth->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fetchWeather() {
|
||||||
$weather = file_get_contents(Weather::WEATHERURL);
|
$weather = file_get_contents(Weather::WEATHERURL);
|
||||||
$query = "UPDATE " . AppDB::TABLE_WEATHER . " SET weather=:weather";
|
$query = "INSERT INTO " . AppDB::TABLE_WEATHER . " (sampledate, weather) VALUES(NOW(), :weather)";
|
||||||
$sth = AppDB::getSTH($query);
|
$sth = AppDB::getSTH($query);
|
||||||
if ( $sth === false ) return false;
|
if ( $sth === false ) return false;
|
||||||
$sth->bindValue(":weather", $weather, PDO::PARAM_STR);
|
$sth->bindValue(":weather", $weather, PDO::PARAM_STR);
|
||||||
|
@ -28,7 +37,7 @@ class Weather {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getWeather() {
|
public static function getWeather() {
|
||||||
$query = "SELECT weather FROM " . AppDB::TABLE_WEATHER . " LIMIT 1";
|
$query = "SELECT weather FROM " . AppDB::TABLE_WEATHER . " ORDER BY sampledate DESC LIMIT 1";
|
||||||
$sth = AppDB::getSTH($query);
|
$sth = AppDB::getSTH($query);
|
||||||
if ( $sth === false ) return false;
|
if ( $sth === false ) return false;
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
|
|
|
@ -42,7 +42,9 @@ DROP TABLE IF EXISTS `weather`;
|
||||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
/*!40101 SET character_set_client = utf8 */;
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
CREATE TABLE `weather` (
|
CREATE TABLE `weather` (
|
||||||
`weather` text NOT NULL
|
`sampledate` datetime NOT NULL,
|
||||||
|
`weather` text NOT NULL,
|
||||||
|
PRIMARY KEY(`sampledate`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
||||||
|
|
|
@ -10,5 +10,6 @@ require_once("authfunctions.php");
|
||||||
if ( php_sapi_name() != "cli" ) exit();
|
if ( php_sapi_name() != "cli" ) exit();
|
||||||
|
|
||||||
$fetched = Weather::fetchWeather();
|
$fetched = Weather::fetchWeather();
|
||||||
|
$deleted = Weather::deleteOldWeather();
|
||||||
|
|
||||||
exit();
|
exit();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user