6 from typing import Dict, Set
12 class local_photos_mirror_renderer(renderer.abstaining_renderer):
13 """A renderer that uses a local mirror of Google photos"""
15 album_root_directory = "/var/www/html/kiosk/images/gphotos/albums"
17 album_whitelist = frozenset(
20 "Bangkok and Phuket, 2003",
22 "Blue Angels... Seafair",
38 "Olympic Sculpture Park",
40 "Prague and Munich 2019",
47 "Trip to California, '16",
48 "Trip to San Francisco",
49 "Trip to East Coast '16",
59 extension_whitelist = frozenset(
69 def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
70 super().__init__(name_to_timeout_dict)
71 self.candidate_photos: Set[str] = set()
73 def debug_prefix(self) -> str:
74 return "local_photos_mirror"
76 def periodic_render(self, key: str) -> bool:
77 if key == "Index Photos":
78 return self.index_photos()
79 elif key == "Choose Photo":
80 return self.choose_photo()
82 raise Exception("Unexpected operation")
84 def album_is_in_whitelist(self, name: str) -> bool:
85 for wlalbum in self.album_whitelist:
86 if re.search("\d+ %s" % wlalbum, name) is not None:
90 def index_photos(self) -> bool:
91 """Walk the filesystem looking for photos in whitelisted albums and
92 keep their paths in memory.
94 for root, subdirs, files in os.walk(self.album_root_directory):
95 last_dir = root.rsplit("/", 1)[1]
96 if self.album_is_in_whitelist(last_dir):
97 for filename in files:
98 extension = filename.rsplit(".", 1)[1]
99 if extension in self.extension_whitelist:
100 photo_path = os.path.join(root, filename)
101 photo_url = photo_path.replace(
102 "/var/www/html", "http://kiosk.house/", 1
104 self.candidate_photos.add(photo_url)
107 def choose_photo(self):
108 """Pick one of the cached URLs and build a page."""
109 if len(self.candidate_photos) == 0:
112 path = random.sample(self.candidate_photos, 1)[0]
113 with file_writer.file_writer("photo_23_3600.html") as f:
117 body{background-color:#303030;}
118 div#time{color:#dddddd;}
119 div#date{color:#dddddd;}
124 f'<img src="{path}" style="display:block;max-width=800;max-height:600;width:auto;height:auto">'
131 # x = local_photos_mirror_renderer({"Index Photos": (60 * 60 * 12),
132 # "Choose Photo": (1)})