summaryrefslogtreecommitdiff
path: root/weather.py
blob: 76af11a36adfbb6f6490e75537085c0480bd4c82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
import json
from time import sleep
from datetime import datetime
from settings import getSettings

weather = getSettings("weather")
gridX = weather["gridX"]
gridY = weather["gridY"]
wfo = weather["WFO"]
units = weather["Units"]  # either si or us
hours = weather["Hours"]


def getHourlyForecast():
	output = []

	hourlyForecastUrl = f"https://api.weather.gov/gridpoints/{wfo}/{gridX},{gridY}/forecast/hourly?units={units}"

	hourResp = requests.get(hourlyForecastUrl)

	while hourResp.status_code != 200:
		hourResp = requests.get(hourlyForecastUrl)
		sleep(10)

	hourData = json.loads(hourResp.text)
	hourPeriods = hourData["properties"]["periods"]

	output.append(f"{hours} Hour Forecast")
	for i in range(hours):
		forecast = hourPeriods[i]
		sTime = datetime.strptime(forecast["startTime"], "%Y-%m-%dT%H:%M:%S%z")
		formatTime = sTime.strftime("%m-%d %H:%M")
		temp = str(forecast["temperature"]) + "°" + forecast["temperatureUnit"] + " " + forecast["shortForecast"]
		hourlyForecast = f"{formatTime}: {temp}"
		output.append(hourlyForecast)

	return output


def getDetailedForecast():
	output = []

	dayForecastUrl = f"https://api.weather.gov/gridpoints/{wfo}/{gridX},{gridY}/forecast?units={units}"
	dayResp = requests.get(dayForecastUrl)

	while dayResp.status_code != 200:
		dayResp = requests.get(dayForecastUrl)
		sleep(10)

	dayData = json.loads(dayResp.text)
	dayPeriods = dayData["properties"]["periods"]

	todayFor = dayPeriods[0]
	tonightFor = dayPeriods[1]

	output.append("Today's Forecast: " + todayFor["detailedForecast"])
	output.append("\nTonight's Forecast: " + tonightFor["detailedForecast"] + "\n")

	return output