Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
coinish committed Dec 30, 2023
2 parents 1d726ae + 4bd94e9 commit 325574c
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions met_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
# 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)


def get_now(lon, lat):
now= datetime.datetime.now().astimezone(datetime.timezone.utc)
local_timezone_name= get_local_timezone_name(lon, lat)
Expand All @@ -38,15 +42,15 @@ 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)



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

Expand Down Expand Up @@ -75,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:
Expand All @@ -94,6 +98,10 @@ def get_forecast(lon, lat):

import http.client
import json
import time

max_retries=4
retry_delay_seconds=180

#request
conn = http.client.HTTPSConnection("api-metoffice.apiconnect.ibmcloud.com")
Expand All @@ -104,28 +112,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()

#print(data.decode("utf-8"))
##################
#decode json
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)

except http.client.RemoteDisconnected:
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.")

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")
Expand All @@ -137,15 +143,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)


Expand Down Expand Up @@ -291,4 +291,4 @@ def make_default_icon_dirs():
print(precipitationRate)
probOfPrecipitation= [t['probOfPrecipitation']/100.0 for t in timeSeries[idx:][:24]]
print ("probOfPrecipitation:")
print(probOfPrecipitation)
print(probOfPrecipitation)

0 comments on commit 325574c

Please sign in to comment.