-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathonce.py
executable file
·164 lines (143 loc) · 4.76 KB
/
once.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import copy, json,requests, pytz,time
from inky.inky_uc8159 import Inky, DESATURATED_PALETTE
from datetime import datetime
from PIL import Image, ImageFont, ImageDraw
import io, apikey, os,signal, iconmap
import RPi.GPIO as GPIO
path = os.path.dirname(os.path.realpath(__file__))
ICON_SIZE = 100
TILE_WIDTH = 150
TILE_HEIGHT = 200
FONT_SIZE = 25
SPACE = 2
ROTATE = 0 # 180 = flip display
USE_INKY = True
SHOW_CLOCK = False
SLEEP_TIME = 3600
colours = ['Black', 'White', 'Green', 'Blue', 'Red', 'Yellow', 'Orange']
percipitation_colour = colours[0]
temp_colour = colours[4]
day_colour = colours[2]
presure_colour = colours[3]
LABELS = ['A','B','C','D']
time_colour = colours[4]
class Day:
def __init__(self, min, max, pop, id, sunrise, sunset, pressure, dt):
self.min = int(min + 0.5)
self.max = int(max + 0.5)
self.pop = pop
self.id = id
self.sunrise = sunrise
self.sunset = sunset
self.pressure = pressure
self.dt = dt
def get_icon(name):
return Image.open(name).convert("RGBA")
def day_lists_not_identical(days, other_days):
if (len(days) != len(other_days)):
return True
for i in range(len(days)):
if (days[i].min != other_days[i].min):
return True
if (days[i].max != other_days[i].max):
return True
if (days[i].pop != other_days[i].pop):
return True
if (days[i].id != other_days[i].id):
return True
return True
api_key = apikey.api_key
night_map = iconmap.night_map
day_map = iconmap.day_map
general_map = iconmap.general_map
if (api_key == "<your API key>"):
print("You forgot to enter your API key")
exit()
lat = apikey.lat
lon = apikey.lon
url = "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&exclude=hourly&appid=%s&units=metric" % (
lat, lon, api_key)
palette_colors = [(c[0] / 255.0, c[1] / 255.0, c[2] / 255.0) for c in DESATURATED_PALETTE[2:6] + [(0, 0, 0)]]
tile_positions = []
for i in range(2):
for j in range(4):
tile_positions.append((j * TILE_WIDTH, i * TILE_HEIGHT))
inky_display = Inky()
satuation = 0
y_top = int(inky_display.height)
y_bottom = y_top + int(inky_display.height * (4.0 / 10.0))
font = ImageFont.truetype(path+
"/fonts/BungeeColor-Regular_colr_Windows.ttf", FONT_SIZE)
old_days = []
try:
response = requests.get(url)
data = json.loads(response.text)
except:
None
days = []
daily = data["daily"]
for day in daily:
min = day["temp"]["min"]
max = day["temp"]["max"]
pop = day["pop"]
id = day["weather"][0]["id"]
sunrise = int(day["sunrise"])
sunset = int(day["sunset"])
dt = int(day["dt"])
pressure = int(day["pressure"])
days.append(Day(min, max, pop, id, sunrise, sunset, pressure, dt))
if (day_lists_not_identical(days, old_days)):
old_days = copy.deepcopy(days)
img = Image.new("RGBA", inky_display.resolution, colours[1])
draw = ImageDraw.Draw(img)
for i in range(8):
name = path+"/icons/wi-"
if (i == 0):
t = int(time.time())
if (t < days[i].sunset):
name += day_map[days[i].id]
else:
name += night_map[days[i].id]
else:
name += general_map[days[i].id]
icon = get_icon(name)
x = tile_positions[i][0] + (TILE_WIDTH - ICON_SIZE) // 2
y = tile_positions[i][1]
img.paste(icon, (x, y))
text = str(int(100 * days[i].pop)) + "%"
w, h = font.getsize(text)
x = tile_positions[i][0] + (TILE_WIDTH - w) // 2
y = tile_positions[i][1] + ICON_SIZE + SPACE
draw.text((x, y), text, percipitation_colour, font)
text = str(days[i].min) + "°"
measuretext = str(days[i].min) + "°|" + str(days[i].max) + "°"
w, h = font.getsize(measuretext)
x = tile_positions[i][0] + (TILE_WIDTH - w) // 2
y += FONT_SIZE
draw.text((x, y), text, colours[3], font)
text = str(days[i].max) + "°"
#w, h = font.getsize(text)
#y += FONT_SIZE
#x += x+ font.getsize(maxtem)
print(x)
x+=45
draw.text((x, y), " " + text, colours[4], font)
press = str(days[i].pressure)
text = str(press)+"hPa"
w, h = font.getsize(text)
x = tile_positions[i][0] + (TILE_WIDTH - w) // 2
y += FONT_SIZE
draw.text((x, y), text, presure_colour, font)
ts = time.gmtime(days[i].dt)
day_name = time.strftime("%a", ts)
text = day_name
w, h = font.getsize(text)
x = tile_positions[i][0] + (TILE_WIDTH - w) // 2
y += FONT_SIZE
draw.text((x, y), text, day_colour, font)
img.rotate(180)
inky_display.set_border(colours[4])
inky_display.set_image(img.rotate(ROTATE), saturation=0)
inky_display.show()