|
@@ -0,0 +1,124 @@
|
|
1
|
+#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+# https://apod.nasa.gov/apod/
|
|
4
|
+#
|
|
5
|
+# ----------------------------------------------------------------------------
|
|
6
|
+# "THE BEER-WARE LICENSE" (Revision 42):
|
|
7
|
+# <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
8
|
+# you can do whatever you want with this stuff. If we meet some day, and you
|
|
9
|
+# think this stuff is worth it, you can buy me a beer in return. Thomas Buck
|
|
10
|
+# ----------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+import time
|
|
13
|
+import sys
|
|
14
|
+import os
|
|
15
|
+
|
|
16
|
+import util
|
|
17
|
+from image import ImageScreen
|
|
18
|
+
|
|
19
|
+class APOD:
|
|
20
|
+ def __init__(self, g, i, to = 10.0, r = 6 * 60 * 60):
|
|
21
|
+ self.gui = g
|
|
22
|
+ self.input = i
|
|
23
|
+ self.timeout = to
|
|
24
|
+ self.refresh = r
|
|
25
|
+
|
|
26
|
+ self.get = None
|
|
27
|
+ self.path = "https://apod.nasa.gov/apod"
|
|
28
|
+ self.img_url = None
|
|
29
|
+ self.img_path = None
|
|
30
|
+ self.image = None
|
|
31
|
+ self.last = None
|
|
32
|
+ self.restart()
|
|
33
|
+
|
|
34
|
+ def restart(self):
|
|
35
|
+ if (self.last == None) or ((time.time() - self.last) >= self.refresh):
|
|
36
|
+ try:
|
|
37
|
+ print("APOD refresh")
|
|
38
|
+ self.img_url = self.get_image_path()
|
|
39
|
+ self.img_path = self.download_image(self.img_url)
|
|
40
|
+ self.image = ImageScreen(self.gui, self.img_path, 0.2, 1, 5.0, None, None, False)
|
|
41
|
+ self.last = time.time()
|
|
42
|
+ except Exception as e:
|
|
43
|
+ print()
|
|
44
|
+ if hasattr(sys, "print_exception"):
|
|
45
|
+ sys.print_exception(e)
|
|
46
|
+ else:
|
|
47
|
+ print(e)
|
|
48
|
+ print()
|
|
49
|
+
|
|
50
|
+ self.show = time.time()
|
|
51
|
+
|
|
52
|
+ def finished(self):
|
|
53
|
+ return (self.image == None) or ((time.time() - self.show) >= self.timeout)
|
|
54
|
+
|
|
55
|
+ def fetch(self, url):
|
|
56
|
+ # lazily initialize WiFi
|
|
57
|
+ if self.get == None:
|
|
58
|
+ self.get, post = util.getRequests()
|
|
59
|
+ if self.get == None:
|
|
60
|
+ return None
|
|
61
|
+
|
|
62
|
+ try:
|
|
63
|
+ #print("GET " + url)
|
|
64
|
+ r = self.get(url)
|
|
65
|
+
|
|
66
|
+ # explitic close on Response object not needed,
|
|
67
|
+ # handled internally by r.content / r.text / r.json()
|
|
68
|
+ # to avoid this automatic behaviour, first access r.content
|
|
69
|
+ # to trigger caching it in response object, then close
|
|
70
|
+ # socket.
|
|
71
|
+ tmp = r.content
|
|
72
|
+ if hasattr(r, "raw"):
|
|
73
|
+ if r.raw != None:
|
|
74
|
+ r.raw.close()
|
|
75
|
+ r.raw = None
|
|
76
|
+
|
|
77
|
+ return r
|
|
78
|
+ except Exception as e:
|
|
79
|
+ print()
|
|
80
|
+ print(url)
|
|
81
|
+ if hasattr(sys, "print_exception"):
|
|
82
|
+ sys.print_exception(e)
|
|
83
|
+ else:
|
|
84
|
+ print(e)
|
|
85
|
+ print()
|
|
86
|
+ return None
|
|
87
|
+
|
|
88
|
+ def get_image_path(self, path = ""):
|
|
89
|
+ print("Checking for new APOD")
|
|
90
|
+ r = self.fetch(self.path + "/" + path).text
|
|
91
|
+ for line in r.splitlines():
|
|
92
|
+ 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
|
|
100
|
+
|
|
101
|
+ def download_image(self, path):
|
|
102
|
+ print("Loading " + path)
|
|
103
|
+ r = self.fetch(path).content
|
|
104
|
+ scriptDir = os.path.dirname(os.path.realpath(__file__))
|
|
105
|
+ imageDir = os.path.join(scriptDir, "images")
|
|
106
|
+ imagePath = os.path.join(imageDir, "apod_" + os.path.basename(path))
|
|
107
|
+ if os.path.isfile(imagePath):
|
|
108
|
+ print("Image already loaded. Skip.")
|
|
109
|
+ return imagePath
|
|
110
|
+ print("Storing at " + imagePath)
|
|
111
|
+ with open(imagePath, 'wb') as f:
|
|
112
|
+ f.write(r)
|
|
113
|
+ return imagePath
|
|
114
|
+
|
|
115
|
+ def draw(self):
|
|
116
|
+ if self.image != None:
|
|
117
|
+ self.image.draw()
|
|
118
|
+
|
|
119
|
+if __name__ == "__main__":
|
|
120
|
+ i = util.getInput()
|
|
121
|
+ t = util.getTarget(i)
|
|
122
|
+
|
|
123
|
+ s = APOD(t, i)
|
|
124
|
+ util.loop(t, s.draw)
|