Browse Source

add NASA astronomy picture of the day loader. and some more pics.

Thomas Buck 1 year ago
parent
commit
83f4f5aa7f
7 changed files with 144 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 124
    0
      apod.py
  3. BIN
      images/ccc.png
  4. BIN
      images/ccc_stern.jpg
  5. BIN
      images/gta.png
  6. BIN
      images/pest.jpg
  7. 19
    0
      livingroom.py

+ 1
- 0
.gitignore View File

1
 __pycache__
1
 __pycache__
2
 tmp
2
 tmp
3
 venv
3
 venv
4
+images/apod_*

+ 124
- 0
apod.py View File

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)

BIN
images/ccc.png View File


BIN
images/ccc_stern.jpg View File


BIN
images/gta.png View File


BIN
images/pest.jpg View File


+ 19
- 0
livingroom.py View File

24
 from tetris import Tetris
24
 from tetris import Tetris
25
 from breakout import Breakout
25
 from breakout import Breakout
26
 from config import Config
26
 from config import Config
27
+from apod import APOD
27
 import util
28
 import util
28
 
29
 
29
 # Need to import InputWrapper before initializing RGB Matrix on Pi
30
 # Need to import InputWrapper before initializing RGB Matrix on Pi
48
 m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
49
 m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
49
 m.add(Solid(t, pause))
50
 m.add(Solid(t, pause))
50
 
51
 
52
+m.add(APOD(t, i))
53
+m.add(Solid(t, pause))
54
+
51
 m.add(ImageScreen(t, "32_earth.gif", 0.2, 2))
55
 m.add(ImageScreen(t, "32_earth.gif", 0.2, 2))
52
 m.add(Solid(t, pause))
56
 m.add(Solid(t, pause))
53
 
57
 
54
 m.add(ImageScreen(t, "aphex-twin-logo.png", 0.2, 1, 10.0, None, None, False))
58
 m.add(ImageScreen(t, "aphex-twin-logo.png", 0.2, 1, 10.0, None, None, False))
55
 m.add(Solid(t, pause))
59
 m.add(Solid(t, pause))
56
 
60
 
61
+m.add(ImageScreen(t, "gta.png", 0.2, 1, 10.0, None, None, False))
62
+m.add(Solid(t, pause))
63
+
64
+m.add(ImageScreen(t, "camp23.png", 0.2, 1, 10.0, None, None, False))
65
+m.add(Solid(t, pause))
66
+
67
+m.add(ImageScreen(t, "pest.jpg", 0.2, 1, 10.0, None, None, False))
68
+m.add(Solid(t, pause))
69
+
70
+m.add(ImageScreen(t, "ccc_stern.jpg", 0.2, 1, 10.0, None, None, False))
71
+m.add(Solid(t, pause))
72
+
73
+m.add(ImageScreen(t, "ccc.png", 0.2, 1, 10.0))
74
+m.add(Solid(t, pause))
75
+
57
 m.add(ImageScreen(t, "cann.png", 0.2, 1, 10.0))
76
 m.add(ImageScreen(t, "cann.png", 0.2, 1, 10.0))
58
 m.add(Solid(t, pause))
77
 m.add(Solid(t, pause))
59
 
78
 

Loading…
Cancel
Save