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