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()
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"An error occurred: {e}")
|
||||
#print(f"An error occurred: {e}")
|
||||
return None
|
||||
|
||||
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
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"An error occurred: {e}")
|
||||
#print(f"An error occurred: {e}")
|
||||
return None
|
||||
|
||||
def get_google_weather():
|
||||
@@ -71,6 +71,8 @@ def get_google_weather():
|
||||
|
||||
theweather["current"] = (f"{condition}") + (f", {temp.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)
|
||||
|
||||
@@ -85,6 +87,8 @@ def get_google_weather():
|
||||
min_temp = day.get("minTemperature", {}).get("degrees")
|
||||
condition = day.get("daytimeForecast", {}).get("weatherCondition", {}).get("description", {}).get("text")
|
||||
theweather["forecast_tomorrow"] = (f"{condition}") + (f", {min_temp}°F-{max_temp}°F")
|
||||
else:
|
||||
theweather["forecast_tomorrow"] = "Problem retrieving weather forecast"
|
||||
|
||||
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 = asyncio.run(getweather())
|
||||
d.multiline_text((1, 35), "Weather:", font=fnt, fill=0)
|
||||
weather = google_weather.get_google_weather()
|
||||
txt2 = " Current: "+weather["current"] \
|
||||
try:
|
||||
weather = google_weather.get_google_weather()
|
||||
txt2 = " Current: "+weather["current"] \
|
||||
+"\n Today's forecast: "+weather["forecast_today"] \
|
||||
+"\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, 135), "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 pytz
|
||||
|
||||
def process_game(game):
|
||||
def process_game(game,mytz):
|
||||
results = ""
|
||||
useastern_timezone = pytz.timezone("US/Eastern")
|
||||
if game["home_name"] == "Pittsburgh Pirates": # home game
|
||||
score_pirates = game["home_score"]
|
||||
score_other = game["away_score"]
|
||||
@@ -16,19 +15,19 @@ def process_game(game):
|
||||
if game["status"] != "Scheduled":
|
||||
if game["status"] == "Final":
|
||||
if score_pirates > score_other:
|
||||
results += " Pirates won"
|
||||
results += ", Pirates won"
|
||||
else:
|
||||
results += " Pirates lost"
|
||||
results += ", Pirates lost"
|
||||
else: # game in progress
|
||||
if score_pirates > score_other:
|
||||
results += " Pirates winning"
|
||||
results += ", Pirates winning"
|
||||
else:
|
||||
results += " Pirates losing"
|
||||
results += ", Pirates losing"
|
||||
results += " "+str(game["away_score"])+" - "+str(game["home_score"])
|
||||
if game["status"] != "Final":
|
||||
gamedate = datetime.datetime.strptime(game["game_datetime"],"%Y-%m-%dT%H:%M:%SZ")
|
||||
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"]:
|
||||
nb = ""
|
||||
for b in game["national_broadcasts"]:
|
||||
@@ -41,19 +40,20 @@ def process_game(game):
|
||||
|
||||
|
||||
def get_pirates():
|
||||
useastern_timezone = pytz.timezone("US/Eastern")
|
||||
results = ""
|
||||
todaystr = datetime.datetime.now().strftime("%Y-%m-%d")
|
||||
tomorrow = datetime.datetime.now()+datetime.timedelta(days=1)
|
||||
todaystr = datetime.datetime.now(tz=useastern_timezone).strftime("%Y-%m-%d")
|
||||
tomorrow = datetime.datetime.now(tz=useastern_timezone)+datetime.timedelta(days=1)
|
||||
tomorrowstr = tomorrow.strftime("%Y-%m-%d")
|
||||
piratesid = statsapi.lookup_team('pittsburgh')[0]['id']
|
||||
todaysgame = statsapi.schedule(team=piratesid,date=todaystr)
|
||||
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"
|
||||
#print("todaysgame",todaysgame)
|
||||
tomorrowsgame = statsapi.schedule(team=piratesid,date=tomorrowstr)
|
||||
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]
|
||||
#results+= tomorrowsgame[0]["summary"]+" "+tomorrowsgame[0]["game_datetime"]+"\n"
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user