summaryrefslogtreecommitdiff
path: root/weather.py
blob: 5996f7707bb7836a8571b35601b17c735ebc01ba (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
import requests
import json
from time import sleep
from datetime import datetime
from settings import weather

gridX = weather["gridX"]
gridY = weather["gridY"]
wfo = weather["WFO"]
units = weather["Units"]  # either si or us
hourly = weather["Hourly"]
detailed = weather["Detailed"]
hours = weather["Hours"]


def getForecast():
    output = []

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

    dayResp = requests.get(dayForecastUrl)
    hourResp = requests.get(hourlyForecastUrl)

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

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

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

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

    if hourly:
        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