|
@@ -17,11 +17,12 @@ import util
|
17
|
17
|
from image import ImageScreen
|
18
|
18
|
|
19
|
19
|
class APOD:
|
20
|
|
- def __init__(self, g, i, to = 10.0, r = 6 * 60 * 60):
|
|
20
|
+ def __init__(self, g, i, to = 10.0, r = 6 * 60 * 60, ito = 30.0):
|
21
|
21
|
self.gui = g
|
22
|
22
|
self.input = i
|
23
|
23
|
self.timeout = to
|
24
|
24
|
self.refresh = r
|
|
25
|
+ self.input_timeout = ito
|
25
|
26
|
|
26
|
27
|
self.get = None
|
27
|
28
|
self.path = "https://apod.nasa.gov/apod"
|
|
@@ -31,11 +32,11 @@ class APOD:
|
31
|
32
|
self.last = None
|
32
|
33
|
self.restart()
|
33
|
34
|
|
34
|
|
- def restart(self):
|
|
35
|
+ def reloadImage(self):
|
35
|
36
|
if (self.last == None) or ((time.time() - self.last) >= self.refresh):
|
36
|
37
|
try:
|
37
|
38
|
print("APOD refresh")
|
38
|
|
- self.img_url = self.get_image_path()
|
|
39
|
+ self.img_url, self.img_desc = self.get_image_metadata()
|
39
|
40
|
self.img_path = self.download_image(self.img_url)
|
40
|
41
|
self.image = ImageScreen(self.gui, self.img_path, 0.2, 1, 5.0, None, None, False)
|
41
|
42
|
self.last = time.time()
|
|
@@ -47,10 +48,28 @@ class APOD:
|
47
|
48
|
print(e)
|
48
|
49
|
print()
|
49
|
50
|
|
|
51
|
+ def restart(self):
|
|
52
|
+ self.reloadImage()
|
|
53
|
+ self.old_keys = self.input.empty() # TODO support missing input
|
|
54
|
+ self.lastInput = None
|
|
55
|
+ self.drawingText = False
|
50
|
56
|
self.show = time.time()
|
51
|
57
|
|
52
|
58
|
def finished(self):
|
53
|
|
- return (self.image == None) or ((time.time() - self.show) >= self.timeout)
|
|
59
|
+ if self.lastInput == None:
|
|
60
|
+ # skip when no image could be loaded
|
|
61
|
+ if self.image == None:
|
|
62
|
+ return True
|
|
63
|
+
|
|
64
|
+ # 10s show image timeout when no buttons have been pressed
|
|
65
|
+ if (time.time() - self.show) >= self.timeout:
|
|
66
|
+ return True
|
|
67
|
+ else:
|
|
68
|
+ # 30s button input timeout after last button press
|
|
69
|
+ if (time.time() - self.lastInput) >= self.input_timeout:
|
|
70
|
+ return True
|
|
71
|
+
|
|
72
|
+ return False
|
54
|
73
|
|
55
|
74
|
def fetch(self, url):
|
56
|
75
|
# lazily initialize WiFi
|
|
@@ -85,40 +104,78 @@ class APOD:
|
85
|
104
|
print()
|
86
|
105
|
return None
|
87
|
106
|
|
88
|
|
- def get_image_path(self, path = ""):
|
89
|
|
- print("Checking for new APOD")
|
|
107
|
+ def get_image_metadata(self, path = ""):
|
|
108
|
+ if len(path) == 0:
|
|
109
|
+ print("Checking for new APOD")
|
|
110
|
+ else:
|
|
111
|
+ print("Checking out APOD '{}'".format(path))
|
|
112
|
+
|
|
113
|
+ imgPath = None
|
|
114
|
+ imgDesc = None
|
|
115
|
+
|
90
|
116
|
r = self.fetch(self.path + "/" + path).text
|
91
|
117
|
for line in r.splitlines():
|
92
|
118
|
start = line.find('IMG SRC="')
|
93
|
|
- if start < 0:
|
94
|
|
- continue
|
95
|
|
- start += 9
|
96
|
|
- end = line.find('"', start)
|
97
|
|
- img = line[start : end]
|
98
|
|
- return self.path + "/" + img
|
99
|
|
- return None
|
|
119
|
+ if start >= 0:
|
|
120
|
+ start += 9
|
|
121
|
+ end = line.find('"', start)
|
|
122
|
+ img = line[start : end]
|
|
123
|
+ imgPath = self.path + "/" + img
|
|
124
|
+ break # TODO also check for description
|
|
125
|
+
|
|
126
|
+ return imgPath, imgDesc
|
100
|
127
|
|
101
|
128
|
def download_image(self, path):
|
102
|
129
|
print("Loading " + path)
|
103
|
130
|
r = self.fetch(path).content
|
|
131
|
+
|
104
|
132
|
scriptDir = os.path.dirname(os.path.realpath(__file__))
|
105
|
133
|
imageDir = os.path.join(scriptDir, "images")
|
106
|
134
|
imagePath = os.path.join(imageDir, "apod_" + os.path.basename(path))
|
|
135
|
+
|
107
|
136
|
if os.path.isfile(imagePath):
|
108
|
137
|
print("Image already loaded. Skip.")
|
109
|
138
|
return imagePath
|
|
139
|
+
|
110
|
140
|
print("Storing at " + imagePath)
|
111
|
141
|
with open(imagePath, 'wb') as f:
|
112
|
142
|
f.write(r)
|
|
143
|
+
|
113
|
144
|
return imagePath
|
114
|
145
|
|
|
146
|
+ def buttons(self):
|
|
147
|
+ keys = self.input.get()
|
|
148
|
+
|
|
149
|
+ if keys["up"] and (not self.old_keys["up"]) and (not self.old_keys["select"]):
|
|
150
|
+ pass # TODO next picture
|
|
151
|
+ elif keys["down"] and (not self.old_keys["down"]) and (not self.old_keys["select"]):
|
|
152
|
+ pass # TODO previous picture
|
|
153
|
+ elif keys["a"] and (not self.old_keys["a"]):
|
|
154
|
+ self.drawingText = True
|
|
155
|
+ elif (keys["select"] and keys["start"] and (not self.old_keys["start"])) or (keys["start"] and keys["select"] and (not self.old_keys["select"])):
|
|
156
|
+ self.restart()
|
|
157
|
+
|
|
158
|
+ self.old_keys = keys.copy()
|
|
159
|
+
|
115
|
160
|
def draw(self):
|
116
|
|
- if self.image != None:
|
117
|
|
- self.image.draw()
|
|
161
|
+ if self.input != None:
|
|
162
|
+ self.buttons()
|
|
163
|
+
|
|
164
|
+ if self.drawingText:
|
|
165
|
+ pass # TODO
|
|
166
|
+ else:
|
|
167
|
+ if self.image != None:
|
|
168
|
+ self.image.draw()
|
118
|
169
|
|
119
|
170
|
if __name__ == "__main__":
|
120
|
171
|
i = util.getInput()
|
121
|
172
|
t = util.getTarget(i)
|
122
|
173
|
|
123
|
174
|
s = APOD(t, i)
|
124
|
|
- util.loop(t, s.draw)
|
|
175
|
+
|
|
176
|
+ def helper():
|
|
177
|
+ s.draw()
|
|
178
|
+ if s.finished():
|
|
179
|
+ s.restart()
|
|
180
|
+
|
|
181
|
+ util.loop(t, helper)
|