forked from splitbrain/sleepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleepy.py
executable file
·203 lines (163 loc) · 6.72 KB
/
sleepy.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import Tkconstants as TkC
from Tkinter import Tk, Frame, Label
from PIL import ImageTk as itk
import sensor
import glob
import json
import os
import sys
import time
import tkFont
import urllib
import urllib2
import thread
import datetime
class Palette:
def __init__(self):
pass
background = "#192D2B"
primary = "#694330"
secondary = "#948358"
class Weather(Frame):
widgets = {}
blank = None
images = {}
def __init__(self, parent, w, h):
"""
Create the GUI elements that display the weather data
"""
Frame.__init__(self, parent)
self.parent = parent
self.config(
height=h,
width=w,
bg="gray",
)
self.load_images()
# to be able to size labels in pixels, they need an image assigned
self.blank = itk.PhotoImage(file="pix/Blank.gif")
font10 = tkFont.Font(family='Droid Sans', size=9)
font8 = tkFont.Font(family='Droid Sans', size=8)
for i in range(0, 5):
self.widgets[i] = {}
self.widgets[i]['frame'] = Frame(self, width=(w / 5), height=h, bg=Palette.background)
self.widgets[i]['frame'].place(x=((w / 5) * i), y=0)
self.widgets[i]['iconholder'] = Label(self.widgets[i]['frame'], image=self.images['none'], bg=Palette.background,
borderwidth=0)
self.widgets[i]['iconholder'].place(x=8, y=0)
self.widgets[i]['line1'] = Label(self.widgets[i]['frame'], text="??h ??°", bg=Palette.background, fg=Palette.secondary,
width=(w / 5), compound="center", image=self.blank, font=font10)
self.widgets[i]['line1'].place(x=0, y=48)
self.widgets[i]['line2'] = Label(self.widgets[i]['frame'], text="???", bg=Palette.background, fg=Palette.secondary,
width=(w / 5), compound="center", image=self.blank, font=font8)
self.widgets[i]['line2'].place(x=0, y=64)
def load_images(self):
"""
Preloads the weather images
"""
for f in glob.glob('pix/weather/*.png'):
base = os.path.splitext(os.path.basename(f))[0]
self.images[base] = itk.PhotoImage(file=f)
def update_data(self, weather_data):
"""
Update the weather display with the given data
"""
for i in range(0, 5):
self.widgets[i]['line1'].config(
text="%d° %sh" % (
weather_data['list'][i]['main']['temp'] - 273.15,
datetime.datetime.fromtimestamp(weather_data['list'][i]['dt']).strftime('%H').lstrip('0')
)
)
self.widgets[i]['line2'].config(
text=weather_data['list'][i]['weather'][0]['main']
)
self.widgets[i]['iconholder'].config(
image=self.images[weather_data['list'][i]['weather'][0]['icon']]
)
class Sleepy(Frame):
clock = None
calendar = None
weather = None
temperature = None
def __init__(self, parent):
Frame.__init__(self, parent, background=Palette.background)
self.parent = parent
self.pack(fill=TkC.BOTH, expand=1)
# init the clock
clock_font = tkFont.Font(family='Droid Sans', size=52, weight='bold')
self.clock = Label(self, text="??:??", fg=Palette.primary, bg=Palette.background, font=clock_font)
self.clock.place(x=0, y=0)
# init the calendar
calendar_font = tkFont.Font(family='Droid Sans', size=12)
self.calendar = Label(self, text="?? ?????, ???", fg=Palette.secondary, bg=Palette.background, font=calendar_font)
self.calendar.place(x=4, y=70)
# init the weather
self.weather = Weather(self, 320, 82)
self.weather.place(x=0, y=(240 - 82))
# init the temperature
temperature_font = tkFont.Font(family='Droid Sans', size=12)
self.temperature = Label(self, text="?? °C", fg=Palette.secondary, bg=Palette.background, font=temperature_font)
self.temperature.place(x=240, y=50)
# print tkFont.families()
# ('Century Schoolbook L', 'Droid Sans Mono', 'Droid Sans Ethiopic', 'Droid Sans Thai', 'DejaVu Sans Mono', 'URW Palladio L', 'Droid Arabic Naskh', 'URW Gothic L', 'Dingbats', 'URW Chancery L', 'FreeSerif', 'DejaVu Sans', 'Droid Sans Japanese', 'Droid Sans Georgian', 'Nimbus Sans L', 'Droid Serif', 'Droid Sans Hebrew', 'Droid Sans Fallback', 'Standard Symbols L', 'Nimbus Mono L', 'Nimbus Roman No9 L', 'FreeSans', 'DejaVu Serif', 'Droid Sans Armenian', 'FreeMono', 'URW Bookman L', 'Droid Sans')
# start working
self.update_clock()
self.update_temperature()
self.fetch_weather_thread()
def update_clock(self):
"""
Updates the clock every ten seconds (less precision = less stuff to do)
"""
now = time.strftime("%H:%M")
self.clock.configure(text=now)
cal = time.strftime("%A, %B %d")
self.calendar.configure(text=cal)
self.parent.after(10000, self.update_clock)
def update_temperature(self):
"""
Update the temperature every minute
"""
t = sensor.DS18B20(simulate=True) #FIXME should be set from CLI parameter
temp = "%0.2f °C" % (t.read_temp())
self.temperature.configure(text=temp)
self.parent.after(60*1000, self.update_temperature)
def fetch_weather_thread(self):
"""
start a thread to fetch new weather data
"""
thread.start_new_thread(self.fetch_weather, ())
def fetch_weather(self):
"""
fetch new weather data
"""
apikey = "f4b0ead5b6a13da37d586a327730ae0b" # FIXME move to config
location = "Berlin,de"
values = {
'q': location,
'APPID': apikey,
'mode': json
}
try:
response = urllib2.urlopen('http://api.openweathermap.org/data/2.5/forecast?' + urllib.urlencode(values))
data = response.read()
weather_data = json.loads(data)
self.weather.update_data(weather_data)
# call again in 1 hour
self.parent.after(1000 * 60 * 60, self.fetch_weather_thread)
except urllib2.URLError as e:
print e.reason
# call again in 1 minute
self.parent.after(1000 * 60 * 1, self.fetch_weather_thread)
def main():
root = Tk()
root.geometry("320x240")
root.wm_title('sleepy')
if len(sys.argv) > 1 and sys.argv[1] == 'fs':
root.wm_attributes('-fullscreen', True)
app = Sleepy(root)
root.mainloop()
if __name__ == '__main__':
main()