Changes ;)
[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.debuggable_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             "8-Mile Lake Hike",
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             "Scott and Lynn",
43             "Sculpture Place",
44             "SFO 2014",
45             "Skiing with Alex",
46             "Sonoma",
47             "Trip to California, '16",
48             "Trip to San Francisco",
49             "Trip to East Coast '16",
50             "Tuscany 2008",
51             "Yosemite 2010",
52             "WA Roadtrip, 2021",
53             "Zoo",
54         ]
55     )
56
57     extension_whitelist = frozenset(
58         [
59             "jpg",
60             "gif",
61             "JPG",
62             "jpeg",
63             "GIF",
64         ]
65     )
66
67     def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
68         super(local_photos_mirror_renderer, self).__init__(name_to_timeout_dict, False)
69         self.candidate_photos: Set[str] = set()
70
71     def debug_prefix(self) -> str:
72         return "local_photos_mirror"
73
74     def periodic_render(self, key: str) -> bool:
75         if key == "Index Photos":
76             return self.index_photos()
77         elif key == "Choose Photo":
78             return self.choose_photo()
79         else:
80             raise Exception("Unexpected operation")
81
82     def album_is_in_whitelist(self, name: str) -> bool:
83         for wlalbum in self.album_whitelist:
84             if re.search("\d+ %s" % wlalbum, name) is not None:
85                 return True
86         return False
87
88     def index_photos(self) -> bool:
89         """Walk the filesystem looking for photos in whitelisted albums and
90         keep their paths in memory.
91         """
92         for root, subdirs, files in os.walk(self.album_root_directory):
93             last_dir = root.rsplit("/", 1)[1]
94             if self.album_is_in_whitelist(last_dir):
95                 for filename in files:
96                     extension = filename.rsplit(".", 1)[1]
97                     if extension in self.extension_whitelist:
98                         photo_path = os.path.join(root, filename)
99                         photo_url = photo_path.replace(
100                             "/var/www/html", "http://kiosk.house/", 1
101                         )
102                         self.candidate_photos.add(photo_url)
103         return True
104
105     def choose_photo(self):
106         """Pick one of the cached URLs and build a page."""
107         if len(self.candidate_photos) == 0:
108             print("No photos!")
109             return False
110         path = random.sample(self.candidate_photos, 1)[0]
111         with file_writer.file_writer("photo_23_3600.html") as f:
112             f.write(
113                 """
114 <style>
115 body{background-color:#303030;}
116 div#time{color:#dddddd;}
117 div#date{color:#dddddd;}
118 </style>
119 <center>"""
120             )
121             f.write(
122                 f'<img src="{path}" style="display:block;max-width=800;max-height:600;width:auto;height:auto">'
123             )
124             f.write("</center>")
125         return True
126
127
128 # Test code
129 # x = local_photos_mirror_renderer({"Index Photos": (60 * 60 * 12),
130 #                                  "Choose Photo": (1)})
131 # x.index_photos()
132 # x.choose_photo()