diff --git a/app.py b/app.py index 8dbcdd2..2c3ae4e 100644 --- a/app.py +++ b/app.py @@ -28,6 +28,9 @@ from psycopg2 import pool import database_management as dm +# Utility +from datetime import datetime, timedelta + import user_settings as us # JSON header verification, API key, etc. @@ -48,6 +51,7 @@ # Get read and write DB connection for managing database. Initialize DB object. writeConn = psycopg2.connect(us.databaseUrl) db = dm.AirDatabase(writeConn) +last_weather_fetch_time = datetime.now() # Add incoming data to DB. @@ -65,15 +69,23 @@ def insert_data(): # Add all historical data to DB. db.load_historal_data() - if us.openWeatherApiKey: - print('querying weather API') - # Make get request to OpenWeather API. - weatherResponse = get("https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&appid={}&units=imperial&lang={}".format( - us.latitude, us.longitude, us.openWeatherApiKey, us.lang)) - print('got weather API response') - - weatherData = json.loads(weatherResponse.content.decode('utf-8')) - db.insert_weather_row_and_forecasts(weatherData) + current_time = datetime.now() + # Only fetch new weather data if it's been more than an hour since the + # last fetch. + if current_time < last_weather_fetch_time + timedelta(hours = 1): + if us.openWeatherApiKey: + print('querying weather API') + # Make get request to OpenWeather API. + weatherResponse = get("https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&appid={}&units=imperial&lang={}".format( + us.latitude, us.longitude, us.openWeatherApiKey, us.lang)) + print('got weather API response') + + weatherData = json.loads(weatherResponse.content.decode('utf-8')) + result = db.insert_weather_row_and_forecasts(weatherData) + + if result != 'failed' and db.table_rowcount("weather_data") > 0: + global last_weather_fetch_time + last_weather_fetch_time = current_time return 'done' diff --git a/database_management.py b/database_management.py index fbc05c1..3f285b9 100644 --- a/database_management.py +++ b/database_management.py @@ -245,6 +245,33 @@ def table_exists(self, table_name): print('checked if {} exists'.format(table_name)) return self.cur.rowcount != 0 + def table_rowcount(self, table_name): + """ + Return the number of rows in the table. + + Args: + table_name: str + + Returns: + integer + """ + records = None + + try: + self.cur.execute( + "SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {} ".format(table_name)) + except psycopg2.ProgrammingError as e: + print(e) + self.conn.rollback() + else: + # Format data. + try: + records = cur.fetchall() + return records.TABLE_ROWS[0] + except psycopg2.ProgrammingError: + print('no data in selected timeframe') + return None + def del_row(self, data): """ Remove an observation from the air database. @@ -368,6 +395,7 @@ def insert_weather_row_and_forecasts(self, data): except (psycopg2.ProgrammingError, psycopg2.errors.UniqueViolation, KeyError) as e: print('failed: ', e) self.conn.rollback() + return 'failed' else: self.conn.commit()