I’m trying to get weather data from the open weather API.
I used my own code to generate the API key, as the standard one kept exceeding the DarkSky quota, but now it doesn’t work for a different reason. It does work however if I input it in my browser manually.
Code Sample:
@anvil.server.callable
def get_weather_data(latitude, longitude):
try:
resp = anvil.http.request(“api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&appid=%s&units=metric”
% (latitude, longitude, “20b156b0d7aa182850baffb16bfc5402”),
json=True)
# convert timestamp to datetime object
time = datetime.fromtimestamp(resp[‘current’][‘dt’])
# return time and temperature data
return {
‘time’:time,
‘temp’:resp[‘current’][‘temp’]
}
except anvil.http.HttpError:
# If we’ve exceeded our DarkSky quota, we return some random data for this example
return {‘time’:datetime.now(), ‘temp’:65 + random.uniform(-3, +3)}
Clone link:
https://anvil.works/build#clone:RVR2I3RCAHCLFMFL=BKPLXD22K5ULTHRYDVCKT2II