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