Weather

"""
Requests is a HTTP library for the Python programming language. 
The goal of the project is to make HTTP requests simpler and more human-friendly. 
RapidAPI is the world's largest API Marketplace. 
Developers use Rapid API to discover and connect to thousands of APIs. 
The api used below are from
https://rapidapi.com/collection/list-of-free-apis
https://rapidapi.com/weatherbit/api/weather/
"""
import requests

url = "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/3hourly"

querystring = {"lat":"35.5","lon":"-78.5","postal_code":"92127"} # Added zip code to the query string

headers = {
	"X-RapidAPI-Key": "e518d32506mshebee640d7001529p1f8128jsnaa52ade28413",
	"X-RapidAPI-Host": "weatherbit-v1-mashape.p.rapidapi.com"
}

response = requests.request("GET", url, headers=headers, params=querystring)

# print(response.text)   # <- raw data is too long !!!
print("\n------ Summary weather ---- \n")
res = response.json()  # convert response to python json object
print(res["city_name"])

# only print the first item in the data
data0 = res["data"][0]

print(data0["timestamp_local"]+" "+data0["weather"]["description"] +" " + str(data0["temp"]))
print("\n------ Detailed weather API response ---- \n")
# here are all the items in data0
for key, value in data0.items():  # print all items in data0
	print(key, value)
------ Summary weather ---- 

San Diego
2022-10-10T20:00:00 Scattered clouds 17.6

------ Detailed weather API response ---- 

clouds_hi 90
clouds_low 9
clouds_mid 0
snow 0
dni 0
temp 17.6
app_temp 17.6
wind_cdir W
rh 91
pod n
pres 989.5
timestamp_utc 2022-10-11T03:00:00
timestamp_local 2022-10-10T20:00:00
clouds 22
snow_depth 0
wind_spd 1.32
wind_cdir_full west
slp 1013
datetime 2022-10-11:03
ts 1665457200
dewpt 16.1
uv 0
wind_gust_spd 1.84
wind_dir 267
ghi 0
dhi 0
precip 0
weather {'description': 'Scattered clouds', 'code': 802, 'icon': 'c02n'}
pop 0
ozone 297
vis 13.096
solar_rad 0