This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
forecast.py
executable file
·72 lines (62 loc) · 2.51 KB
/
forecast.py
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
61
62
63
64
65
66
67
68
69
70
71
72
from __future__ import print_function
from Adafruit_Thermal import *
from datetime import date
import calendar
import urllib.request
import json
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
def getLink(dailyOrHourly):
latitude = "38.8894" #limit to four decimal digits
longitude = "-77.0352" #limit to four decimal digits
mainLink = "https://api.weather.gov/points/" + latitude + "," + longitude
response_main = urllib.request.urlopen(mainLink)
raw_data_main = response_main.read().decode()
data_main = json.loads(raw_data_main)
properties_main = data_main['properties']
dailyLink = properties_main["forecast"]
hourlyLink = properties_main["forecastHourly"]
if dailyOrHourly == "daily":
return dailyLink
elif dailyOrHourly == "hourly":
return hourlyLink
url_daily = getLink("daily")
response_daily = urllib.request.urlopen(url_daily)
# status & reason
# print(response_daily.status, response_daily.reason)
raw_data_daily = response_daily.read().decode()
data_daily = json.loads(raw_data_daily)
forecast_periods_daily = data_daily['properties']['periods']
current_period_isDayTime = forecast_periods_daily[0]['isDaytime']
if current_period_isDayTime:
day_index = 0
night_index = 1
else:
day_index = 1
night_index = 0
day_name = forecast_periods_daily[day_index]['name']
hi_temp = forecast_periods_daily[day_index]['temperature']
night_name = forecast_periods_daily[night_index]['name']
lo_temp = forecast_periods_daily[night_index]['temperature']
current_detailed_forecast = forecast_periods_daily[0]['detailedForecast']
url_hourly = getLink("hourly")
response_hourly = urllib.request.urlopen(url_hourly)
# status & reason
#print(response_hourly.status, response_hourly.reason)
raw_data_hourly = response_hourly.read().decode()
data_hourly = json.loads(raw_data_hourly)
forecast_periods_hourly = data_hourly['properties']['periods']
temperature = forecast_periods_hourly[0]['temperature']
d = date.today()
week_day = calendar.day_name[date(d.year,d.month,d.day).weekday()]
month_text = calendar.month_name[d.month]
printer.underlineOn()
printer.print("It's " + week_day + ", " + month_text + " " + str(d.day) + "\n")
printer.underlineOff()
printer.boldOn()
printer.print(day_name + "'s Forecast \n")
printer.boldOff()
printer.print("Current temperature: " + str(temperature) + " F \n")
printer.print("High temperature: " + str(hi_temp) + " F \n")
printer.print("Low temperature: " + str(lo_temp) + " F \n")
printer.print(current_detailed_forecast + "\n")
printer.feed(3)