Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only call OpenWeather API once an hour to avoid rate limits #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand All @@ -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.
Expand All @@ -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'

Expand Down
28 changes: 28 additions & 0 deletions database_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down