Trying to avoid error propagation from Google Weather overuse
This commit is contained in:
@@ -23,7 +23,7 @@ def get_current_weather(api_key, lat, lng, units="IMPERIAL"):
|
|||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"An error occurred: {e}")
|
#print(f"An error occurred: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_daily_forecast(api_key, lat, lng, days=2):
|
def get_daily_forecast(api_key, lat, lng, days=2):
|
||||||
@@ -51,7 +51,7 @@ def get_daily_forecast(api_key, lat, lng, days=2):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"An error occurred: {e}")
|
#print(f"An error occurred: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_google_weather():
|
def get_google_weather():
|
||||||
@@ -71,6 +71,8 @@ def get_google_weather():
|
|||||||
|
|
||||||
theweather["current"] = (f"{condition}") + (f", {temp.get('degrees')}°F")\
|
theweather["current"] = (f"{condition}") + (f", {temp.get('degrees')}°F")\
|
||||||
+ (f", {humidity}%") + (f" hum., Feels like: {feels_like.get('degrees')}°F")
|
+ (f", {humidity}%") + (f" hum., Feels like: {feels_like.get('degrees')}°F")
|
||||||
|
else: # current_data not returned
|
||||||
|
theweather["current"] = "Problem retrieving current weather"
|
||||||
|
|
||||||
forecast_data = get_daily_forecast(API_KEY, LATITUDE, LONGITUDE)
|
forecast_data = get_daily_forecast(API_KEY, LATITUDE, LONGITUDE)
|
||||||
|
|
||||||
@@ -85,6 +87,8 @@ def get_google_weather():
|
|||||||
min_temp = day.get("minTemperature", {}).get("degrees")
|
min_temp = day.get("minTemperature", {}).get("degrees")
|
||||||
condition = day.get("daytimeForecast", {}).get("weatherCondition", {}).get("description", {}).get("text")
|
condition = day.get("daytimeForecast", {}).get("weatherCondition", {}).get("description", {}).get("text")
|
||||||
theweather["forecast_tomorrow"] = (f"{condition}") + (f", {min_temp}°F-{max_temp}°F")
|
theweather["forecast_tomorrow"] = (f"{condition}") + (f", {min_temp}°F-{max_temp}°F")
|
||||||
|
else:
|
||||||
|
theweather["forecast_tomorrow"] = "Problem retrieving weather forecast"
|
||||||
|
|
||||||
return theweather
|
return theweather
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,13 @@ d.multiline_text((1, 1), txt1, font=fntBold, fill=0)
|
|||||||
#txt2 = "This is the day that the LORD has made\nWe will rejoice and be glad in it."
|
#txt2 = "This is the day that the LORD has made\nWe will rejoice and be glad in it."
|
||||||
#txt2 = asyncio.run(getweather())
|
#txt2 = asyncio.run(getweather())
|
||||||
d.multiline_text((1, 35), "Weather:", font=fnt, fill=0)
|
d.multiline_text((1, 35), "Weather:", font=fnt, fill=0)
|
||||||
weather = google_weather.get_google_weather()
|
try:
|
||||||
txt2 = " Current: "+weather["current"] \
|
weather = google_weather.get_google_weather()
|
||||||
|
txt2 = " Current: "+weather["current"] \
|
||||||
+"\n Today's forecast: "+weather["forecast_today"] \
|
+"\n Today's forecast: "+weather["forecast_today"] \
|
||||||
+"\n Tomorrow: "+weather["forecast_tomorrow"]
|
+"\n Tomorrow: "+weather["forecast_tomorrow"]
|
||||||
|
except: # Assume the weather API blew up
|
||||||
|
txt2 = "Problem getting weather"
|
||||||
d.multiline_text((1, 60), txt2, font=fnt, fill=0)
|
d.multiline_text((1, 60), txt2, font=fnt, fill=0)
|
||||||
d.multiline_text((1, 135), "Pirates:", font=fnt, fill=0)
|
d.multiline_text((1, 135), "Pirates:", font=fnt, fill=0)
|
||||||
d.multiline_text((3,160), mlb.get_pirates(), font=fnt, fill=0)
|
d.multiline_text((3,160), mlb.get_pirates(), font=fnt, fill=0)
|
||||||
|
|||||||
22
mlb.py
22
mlb.py
@@ -2,9 +2,8 @@ import statsapi
|
|||||||
import datetime
|
import datetime
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
def process_game(game):
|
def process_game(game,mytz):
|
||||||
results = ""
|
results = ""
|
||||||
useastern_timezone = pytz.timezone("US/Eastern")
|
|
||||||
if game["home_name"] == "Pittsburgh Pirates": # home game
|
if game["home_name"] == "Pittsburgh Pirates": # home game
|
||||||
score_pirates = game["home_score"]
|
score_pirates = game["home_score"]
|
||||||
score_other = game["away_score"]
|
score_other = game["away_score"]
|
||||||
@@ -16,19 +15,19 @@ def process_game(game):
|
|||||||
if game["status"] != "Scheduled":
|
if game["status"] != "Scheduled":
|
||||||
if game["status"] == "Final":
|
if game["status"] == "Final":
|
||||||
if score_pirates > score_other:
|
if score_pirates > score_other:
|
||||||
results += " Pirates won"
|
results += ", Pirates won"
|
||||||
else:
|
else:
|
||||||
results += " Pirates lost"
|
results += ", Pirates lost"
|
||||||
else: # game in progress
|
else: # game in progress
|
||||||
if score_pirates > score_other:
|
if score_pirates > score_other:
|
||||||
results += " Pirates winning"
|
results += ", Pirates winning"
|
||||||
else:
|
else:
|
||||||
results += " Pirates losing"
|
results += ", Pirates losing"
|
||||||
results += " "+str(game["away_score"])+" - "+str(game["home_score"])
|
results += " "+str(game["away_score"])+" - "+str(game["home_score"])
|
||||||
if game["status"] != "Final":
|
if game["status"] != "Final":
|
||||||
gamedate = datetime.datetime.strptime(game["game_datetime"],"%Y-%m-%dT%H:%M:%SZ")
|
gamedate = datetime.datetime.strptime(game["game_datetime"],"%Y-%m-%dT%H:%M:%SZ")
|
||||||
gamedateutc = gamedate.replace(tzinfo=datetime.timezone.utc)
|
gamedateutc = gamedate.replace(tzinfo=datetime.timezone.utc)
|
||||||
results+= " "+gamedateutc.astimezone(tz=useastern_timezone).strftime("%I:%M %p")
|
results+= " "+gamedateutc.astimezone(tz=mytz).strftime("%I:%M %p")
|
||||||
if game["national_broadcasts"]:
|
if game["national_broadcasts"]:
|
||||||
nb = ""
|
nb = ""
|
||||||
for b in game["national_broadcasts"]:
|
for b in game["national_broadcasts"]:
|
||||||
@@ -41,19 +40,20 @@ def process_game(game):
|
|||||||
|
|
||||||
|
|
||||||
def get_pirates():
|
def get_pirates():
|
||||||
|
useastern_timezone = pytz.timezone("US/Eastern")
|
||||||
results = ""
|
results = ""
|
||||||
todaystr = datetime.datetime.now().strftime("%Y-%m-%d")
|
todaystr = datetime.datetime.now(tz=useastern_timezone).strftime("%Y-%m-%d")
|
||||||
tomorrow = datetime.datetime.now()+datetime.timedelta(days=1)
|
tomorrow = datetime.datetime.now(tz=useastern_timezone)+datetime.timedelta(days=1)
|
||||||
tomorrowstr = tomorrow.strftime("%Y-%m-%d")
|
tomorrowstr = tomorrow.strftime("%Y-%m-%d")
|
||||||
piratesid = statsapi.lookup_team('pittsburgh')[0]['id']
|
piratesid = statsapi.lookup_team('pittsburgh')[0]['id']
|
||||||
todaysgame = statsapi.schedule(team=piratesid,date=todaystr)
|
todaysgame = statsapi.schedule(team=piratesid,date=todaystr)
|
||||||
if todaysgame:
|
if todaysgame:
|
||||||
results+= "Today: "+process_game(todaysgame[0])+"\n"
|
results+= "Today: "+process_game(todaysgame[0],useastern_timezone)+"\n"
|
||||||
#results += todaysgame[0]["summary"]+" "+todaysgame[0]["game_datetime"]+"\n"
|
#results += todaysgame[0]["summary"]+" "+todaysgame[0]["game_datetime"]+"\n"
|
||||||
#print("todaysgame",todaysgame)
|
#print("todaysgame",todaysgame)
|
||||||
tomorrowsgame = statsapi.schedule(team=piratesid,date=tomorrowstr)
|
tomorrowsgame = statsapi.schedule(team=piratesid,date=tomorrowstr)
|
||||||
if tomorrowsgame:
|
if tomorrowsgame:
|
||||||
results+= "Tomorrow: "+process_game(tomorrowsgame[0])+"\n"
|
results+= "Tomorrow: "+process_game(tomorrowsgame[0],useastern_timezone)+"\n"
|
||||||
#dtt = tomorrowsgame[0]["game_datetime"][11:16]
|
#dtt = tomorrowsgame[0]["game_datetime"][11:16]
|
||||||
#results+= tomorrowsgame[0]["summary"]+" "+tomorrowsgame[0]["game_datetime"]+"\n"
|
#results+= tomorrowsgame[0]["summary"]+" "+tomorrowsgame[0]["game_datetime"]+"\n"
|
||||||
return results
|
return results
|
||||||
|
|||||||
Reference in New Issue
Block a user