Fixup weather renderer.
[kiosk.git] / local_photos_mirror_renderer.py
1 #!/usr/bin/env python3
2
3 import os
4 import random
5 import re
6 from typing import Dict, Set
7
8 import file_writer
9 import renderer
10
11
12 class local_photos_mirror_renderer(renderer.abstaining_renderer):
13     """A renderer that uses a local mirror of Google photos"""
14
15     album_root_directory = "/var/www/html/kiosk/images/gphotos/albums"
16
17     album_whitelist = frozenset(
18         [
19             "Autumn at Kubota",
20             "Bangkok and Phuket, 2003",
21             "Barn",
22             "Blue Angels... Seafair",
23             "Chihuly Glass",
24             "Dunn Gardens",
25             "East Coast 2018",
26             "Fall '17",
27             "Friends",
28             "Hiking",
29             "Key West 2019",
30             "Krakow 2009",
31             "Kubota Gardens",
32             "Las Vegas, 2017",
33             "London, 2018",
34             "Munich, July 2018",
35             "NJ 2015",
36             "Newer Alex Photos",
37             "Ohme Gardens",
38             "Olympic Sculpture Park",
39             "Portland, ME 2021",
40             "Prague and Munich 2019",
41             "Random",
42             "SFO 2014",
43             "Scott and Lynn",
44             "Sculpture Place",
45             "Skiing with Alex",
46             "Sonoma",
47             "Trip to California, '16",
48             "Trip to San Francisco",
49             "Trip to East Coast '16",
50             "Turkey 2022",
51             "Tuscany 2008",
52             "WA Roadtrip, 2021",
53             "Yosemite 2010",
54             "Zoo",
55         ]
56     )
57
58     extension_whitelist = frozenset(
59         [
60             "jpg",
61             "gif",
62             "JPG",
63             "jpeg",
64             "GIF",
65         ]
66     )
67
68     def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
69         super().__init__(name_to_timeout_dict)
70         self.candidate_photos: Set[str] = set()
71
72     def debug_prefix(self) -> str:
73         return "local_photos_mirror"
74
75     def periodic_render(self, key: str) -> bool:
76         if key == "Index Photos":
77             return self.index_photos()
78         elif key == "Choose Photo":
79             return self.choose_photo()
80         else:
81             raise Exception("Unexpected operation")
82
83     def album_is_in_whitelist(self, name: str) -> bool:
84         for wlalbum in self.album_whitelist:
85             if re.search("\d+ %s" % wlalbum, name) is not None:
86                 return True
87         return False
88
89     def index_photos(self) -> bool:
90         """Walk the filesystem looking for photos in whitelisted albums and
91         keep their paths in memory.
92         """
93         for root, subdirs, files in os.walk(self.album_root_directory):
94             last_dir = root.rsplit("/", 1)[1]
95             if self.album_is_in_whitelist(last_dir):
96                 for filename in files:
97                     extension = filename.rsplit(".", 1)[1]
98                     if extension in self.extension_whitelist:
99                         photo_path = os.path.join(root, filename)
100                         photo_url = photo_path.replace(
101                             "/var/www/html", "http://kiosk.house/", 1
102                         )
103                         self.candidate_photos.add(photo_url)
104         return True
105
106     def choose_photo(self):
107         """Pick one of the cached URLs and build a page."""
108         if len(self.candidate_photos) == 0:
109             print("No photos!")
110             return False
111         path = random.sample(self.candidate_photos, 1)[0]
112         with file_writer.file_writer("photo_23_3600.html") as f:
113             f.write(
114                 """
115 <style>
116 body{background-color:#303030;}
117 div#time{color:#dddddd;}
118 div#date{color:#dddddd;}
119 </style>
120 <center>"""
121             )
122             f.write(
123                 f'<img src="{path}" style="display:block;max-width=800;max-height:600;width:auto;height:auto">'
124             )
125             f.write("</center>")
126         return True
127
128
129 # Test code
130 # x = local_photos_mirror_renderer({"Index Photos": (60 * 60 * 12),
131 #                                  "Choose Photo": (1)})
132 # x.index_photos()
133 # x.choose_photo()