6 from typing import Dict, Set
13 class local_photos_mirror_renderer(renderer.debuggable_abstaining_renderer):
14 """A renderer that uses a local mirror of Google photos"""
16 ALBUM_ROOT_DIR = "/var/www/kiosk/pages/images/gphotos/albums"
18 album_whitelist = frozenset(
21 "Bangkok and Phuket, 2003",
23 "Blue Angels... Seafair",
39 "Olympic Sculpture Park",
40 "Prague and Munich 2019",
46 "Trip to California, '16",
47 "Trip to San Francisco",
48 "Trip to East Coast '16",
55 extension_whitelist = frozenset(
65 def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
66 super(local_photos_mirror_renderer, self).__init__(name_to_timeout_dict, False)
67 self.candidate_photos: Set[str] = set()
69 def debug_prefix(self) -> str:
70 return "local_photos_mirror"
72 def periodic_render(self, key: str) -> bool:
73 if key == "Index Photos":
74 return self.index_photos()
75 elif key == "Choose Photo":
76 return self.choose_photo()
78 raise Exception("Unexpected operation")
80 def album_is_in_whitelist(self, name: str) -> bool:
81 for wlalbum in self.album_whitelist:
82 if re.search("\d+ %s" % wlalbum, name) is not None:
86 def index_photos(self) -> bool:
87 """Walk the filesystem looking for photos in whitelisted albums and
88 keep their paths in memory.
90 for root, subdirs, files in os.walk(self.ALBUM_ROOT_DIR):
91 last_dir = root.rsplit("/", 1)[1]
92 if self.album_is_in_whitelist(last_dir):
93 for filename in files:
94 extension = filename.rsplit(".", 1)[1]
95 if extension in self.extension_whitelist:
96 photo_path = os.path.join(root, filename)
97 photo_url = photo_path.replace(
98 "/var/www/", f"http://{constants.hostname}/", 1
100 self.candidate_photos.add(photo_url)
103 def choose_photo(self):
104 """Pick one of the cached URLs and build a page."""
105 if len(self.candidate_photos) == 0:
108 path = random.sample(self.candidate_photos, 1)[0]
109 with file_writer.file_writer("photo_23_3600.html") as f:
113 body{background-color:#303030;}
114 div#time{color:#dddddd;}
115 div#date{color:#dddddd;}
120 f'<img src="{path}" style="display:block;max-width=800;max-height:600;width:auto;height:auto">'
127 # x = local_photos_mirror_renderer({"Index Photos": (60 * 60 * 12),
128 # "Choose Photo": (1)})