From 230f1bbf00bf13308a4ff6d05961ac4b4e1fd051 Mon Sep 17 00:00:00 2001 From: seaniedan Date: Tue, 23 May 2023 10:11:05 +0100 Subject: [PATCH 1/4] removed suncalc warning --- met_weather.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/met_weather.py b/met_weather.py index 2dbbaef..b6190b7 100755 --- a/met_weather.py +++ b/met_weather.py @@ -19,6 +19,11 @@ import datetime from dateutil import tz +import warnings +warnings.filterwarnings("ignore", category=RuntimeWarning) + + + def get_now(lon, lat): now= datetime.datetime.now().astimezone(datetime.timezone.utc) local_timezone_name= get_local_timezone_name(lon, lat) @@ -291,4 +296,4 @@ def make_default_icon_dirs(): print(precipitationRate) probOfPrecipitation= [t['probOfPrecipitation']/100.0 for t in timeSeries[idx:][:24]] print ("probOfPrecipitation:") - print(probOfPrecipitation) \ No newline at end of file + print(probOfPrecipitation) From 4759df278220da2439fafb69e74bb88eb130aa58 Mon Sep 17 00:00:00 2001 From: seaniedan Date: Tue, 20 Jun 2023 17:17:10 +0100 Subject: [PATCH 2/4] allow 4 recon_attempts before fail --- met_weather.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/met_weather.py b/met_weather.py index b6190b7..095cdda 100755 --- a/met_weather.py +++ b/met_weather.py @@ -99,6 +99,10 @@ def get_forecast(lon, lat): import http.client import json + import time + + max_retries=4 + rery_delay_seconds=180 #request conn = http.client.HTTPSConnection("api-metoffice.apiconnect.ibmcloud.com") @@ -109,28 +113,26 @@ def get_forecast(lon, lat): 'accept': "application/json" } - #conn.request("GET", f"/v0/forecasts/point/daily?excludeParameterMetadata=REPLACE_THIS_VALUE&includeLocationName=REPLACE_THIS_VALUE&latitude={lat}&longitude={lon}", headers=headers) - conn.request("GET", f"/v0/forecasts/point/hourly?excludeParameterMetadata=REPLACE_THIS_VALUE&includeLocationName=true&latitude={lat}&longitude={lon}", headers=headers) - res = conn.getresponse() - data = res.read() + for retry in range(max_retries): + try: + conn.request("GET", f"/v0/forecasts/point/hourly?excludeParameterMetadata=REPLACE_THIS_VALUE&includeLocationName=true&latitude={lat}&longitude={lon}", headers=headers) + res = conn.getresponse() + data = res.read() + return json.loads(data) - #print(data.decode("utf-8")) - ################## - #decode json + except http.client.RemoteDisconnected: + time.sleep(retry_delay) + else: + # If all retries fail, raise an error or handle it accordingly + raise Exception(f"Failed to establish a connection after {max_retries} retries.") - return json.loads(data) - def get_daily_forecast(lon, lat): import http.client import json - - #parameters - #print (lon) - #print (lat) #request conn = http.client.HTTPSConnection("api-metoffice.apiconnect.ibmcloud.com") @@ -142,15 +144,9 @@ def get_daily_forecast(lon, lat): } conn.request("GET", f"/v0/forecasts/point/daily?excludeParameterMetadata=REPLACE_THIS_VALUE&includeLocationName=REPLACE_THIS_VALUE&latitude={lat}&longitude={lon}", headers=headers) - #conn.request("GET", f"/v0/forecasts/point/hourly?excludeParameterMetadata=REPLACE_THIS_VALUE&includeLocationName=true&latitude={lat}&longitude={lon}", headers=headers) res = conn.getresponse() data = res.read() - #print(data.decode("utf-8")) - ################## - #decode json - - return json.loads(data) From 3165571d0910a3ed6319438e56f938a56d00a97d Mon Sep 17 00:00:00 2001 From: seaniedan Date: Tue, 20 Jun 2023 17:23:52 +0100 Subject: [PATCH 3/4] allow 4 recon_attempts x retry_seconds before fail --- met_weather.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/met_weather.py b/met_weather.py index 095cdda..0aa04e7 100755 --- a/met_weather.py +++ b/met_weather.py @@ -121,7 +121,7 @@ def get_forecast(lon, lat): return json.loads(data) except http.client.RemoteDisconnected: - time.sleep(retry_delay) + time.sleep(retry_delay_seconds) else: # If all retries fail, raise an error or handle it accordingly raise Exception(f"Failed to establish a connection after {max_retries} retries.") From 4bd94e93c725554e1e98c560a21bad7c7b265884 Mon Sep 17 00:00:00 2001 From: seaniedan Date: Tue, 20 Jun 2023 17:26:49 +0100 Subject: [PATCH 4/4] allow 4 recon_attempts x retry_delay_seconds before fail --- met_weather.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/met_weather.py b/met_weather.py index 0aa04e7..2eb1dd3 100755 --- a/met_weather.py +++ b/met_weather.py @@ -16,12 +16,11 @@ # support: https://groups.google.com/g/metoffice-datapoint import api -import datetime +import datetime from dateutil import tz import warnings -warnings.filterwarnings("ignore", category=RuntimeWarning) - +warnings.filterwarnings("ignore", category=RuntimeWarning) def get_now(lon, lat): @@ -43,7 +42,7 @@ def get_local_timezone_name(lon, lat): def convert_from_iso(date_string): - # input time (date_string): 2022-08-01T17:00Z + # input time (date_string): 2022-08-01T17:00Z # outputs timezone-aware datetime object, in UTC import datetime return datetime.datetime.fromisoformat(date_string[:-1]).astimezone(datetime.timezone.utc) @@ -51,7 +50,7 @@ def convert_from_iso(date_string): def convert_utc_to_local(datetime_object, local_timezone_name): - # given a datetime object as UTC + # given a datetime object as UTC # and a local timezone name, e.g. 'Europe/Berlin' # return local time @@ -80,7 +79,7 @@ def get_next_sunrise_or_sunset_msg(now, lon, lat, local_timezone_name): sunset_utc= datetime.datetime.fromtimestamp(sunset.replace(tzinfo=datetime.timezone.utc).timestamp(), tz=datetime.timezone.utc) if (sunrise_utc < now < sunset_utc): - #it's day time + #it's day time next_sunrise_or_sunset_msg= "sunset\n{}".format(convert_utc_to_local(sunset_utc, local_timezone_name).strftime("%H:%M")) else: @@ -102,7 +101,7 @@ def get_forecast(lon, lat): import time max_retries=4 - rery_delay_seconds=180 + retry_delay_seconds=180 #request conn = http.client.HTTPSConnection("api-metoffice.apiconnect.ibmcloud.com")