You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.0 KiB
96 lines
3.0 KiB
import requests
|
|
import math
|
|
import sys
|
|
import iconSelector as iconSelect
|
|
|
|
try:
|
|
stationName = sys.argv[1]
|
|
except IndexError:
|
|
# if no argument given, default to Hartsfield Jackson International Airport (KATL)
|
|
stationName = "KATL"
|
|
|
|
|
|
# Rounds decimals up or down
|
|
def rounding(num):
|
|
test = num % 1
|
|
result = 0
|
|
|
|
if (test >= 0.5):
|
|
result = math.ceil(num)
|
|
else:
|
|
result = math.floor(num)
|
|
|
|
return result
|
|
|
|
|
|
def convertToF(num):
|
|
return (((num * 9) / 5) + 32)
|
|
|
|
|
|
def convertToMPH(num):
|
|
return num * 0.621371
|
|
|
|
|
|
def convertCompass(num):
|
|
if num in range(338, 360, 1) or num in range(0, 23, 1):
|
|
return "N"
|
|
elif num in range(23, 68, 1):
|
|
return "NE"
|
|
elif num in range(68, 113, 1):
|
|
return "E"
|
|
elif num in range(113, 158, 1):
|
|
return "SE"
|
|
elif num in range(158, 203, 1):
|
|
return "S"
|
|
elif num in range(203, 248, 1):
|
|
return "SW"
|
|
elif num in range(248, 293, 1):
|
|
return "W"
|
|
elif num in range(293, 338, 1):
|
|
return "NW"
|
|
else:
|
|
return "error"
|
|
|
|
|
|
def tooltipPrinter(r_dict):
|
|
info = r_dict['properties']
|
|
# Print station name, temp, humidity, wind speed, wind direction
|
|
stationName = f"Station Name: {info['stationName']}"
|
|
temp = f"Temp: {rounding(convertToF(info['temperature']['value']))} F"
|
|
humidity = f"Humidity: {rounding(info['relativeHumidity']['value'])}%"
|
|
windSpeed = rounding(convertToMPH(info['windSpeed']['value']))
|
|
windDirection = info['windDirection']['value']
|
|
windData = f"Wind: {windSpeed} MPH, {convertCompass(windDirection)}, {windDirection} degrees"
|
|
precipitation = f"Precipitation (3 Hrs.): {info['precipitationLast3Hours']['value']}"
|
|
result = f"<tool>{stationName} \n{temp} \n{windData} \n{humidity} \n{precipitation}</tool>"
|
|
|
|
return result
|
|
|
|
# Results at Hector Field - Fargo Int'l Airport
|
|
# request_addr = "https://api.weather.gov/stations/KFAR/observations/latest"
|
|
# Results at Dothan Regional Airport
|
|
# request_addr = "https://api.weather.gov/stations/KDHN/observations/latest"
|
|
|
|
|
|
request_addr = f"https://api.weather.gov/stations/{stationName}/observations/latest"
|
|
|
|
try:
|
|
r = requests.get(request_addr)
|
|
except Exception as err:
|
|
print("<txt>No Connection</txt>")
|
|
print(f"<tool>Error Detected: /n {err}</tool>")
|
|
else:
|
|
if r.status_code != 200:
|
|
print("<txt>No Connection to NWS</txt>")
|
|
else:
|
|
request_json = r.json()
|
|
temp = convertToF(request_json["properties"]["temperature"]["value"])
|
|
roundedTemp = rounding(temp)
|
|
condition = request_json["properties"]["textDescription"]
|
|
timeOfDay = request_json['properties']['icon'].split("/")[5]
|
|
weatherIcon = iconSelect.parseDescription(condition)
|
|
weatherIconImage = f"/home/nclanceman/tools/weather{iconSelect.imageSelector(weatherIcon, timeOfDay)}"
|
|
# print(f"<txt> \u2601 {condition}, {roundedTemp} F</txt>")
|
|
print(
|
|
f"<img>{weatherIconImage}</img><txt> {condition}, {roundedTemp}F</txt>")
|
|
print(tooltipPrinter(request_json))
|
|
|