Sujet : Re: new here
De : bowman (at) *nospam* montana.com (rbowman)
Groupes : comp.lang.pythonDate : 28. Aug 2024, 19:06:33
Autres entêtes
Message-ID : <lj9799F3k5jU1@mid.individual.net>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : Pan/0.149 (Bellevue; 4c157ba)
On Wed, 28 Aug 2024 08:41:28 +0100, Daniel wrote:
That is so cool. I've had the same idea to use the API with AWS for my
bbs. I also want to do the same thing for other government sites like
ecfr for pulling aviation regulations.
Is your code somewhere I can look at it?
The NOAA? I didn't get dncy so I'm calling get_station_info with a
hardcoded latitude and longitude. The commented out print(json.dumps())
pretty print the JSON so you can decide what to extract. I'm not 100% sure
about tacking K onto the grid ID. The famous 'works for me' A URL like
https://forecast.weather.gov/MapClick.php?lat=40.8551337&lon=-114.0140115will show the same info, in this case for Wendover Airport (KENV).
https://www.weather.gov/documentation/services-web-apiis the documentation for the API.
import json
import requests
def get_station_info(latitude, longitude):
url = f"
https://api.weather.gov/points/{latitude},{longitude}"
response = requests.get(url).json()
# print(json.dumps(response, indent=4, sort_keys=True))
property = response['properties']
grid_id = property["gridId"]
grid_x = property["gridX"]
grid_y = property["gridY"]
forecast_url = property["forecast"]
observation_url = f"
https://api.weather.gov/stations/K{grid_id}/
observations/latest"
response = requests.get(observation_url).json()
# print(json.dumps(response, indent=4, sort_keys=True))
properties = response['properties']
pressure = properties['barometricPressure']
temperature = properties['temperature']
humidity = properties['relativeHumidity']
dewpoint = properties['dewpoint']
temp = 9 * temperature['value'] / 5 + 32
dew = 9 * dewpoint['value'] / 5 + 32
hum = humidity['value']
bp = pressure['value'] * 0.000295
print(f"temperature {temp} dewpoint {dew:.2f} humidity {hum:.2f}
pressure {bp:.2f}\n")
response = requests.get(forecast_url).json()
properties = response['properties']
periods = properties['periods']
# print(json.dumps(period, indent=4, sort_keys=True))
for i in range(0, len(periods)):
period = periods[i]
print(period['name'])
print(period['detailedForecast'])
print(f"temperature: {period['temperature']}")
print(f"wind {period['windSpeed']} {period['windDirection']}")
print()