020683d00e8b8c793aa500006708e463aff87744
[kiosk.git] / local_photos_mirror_renderer.py
1 import os
2 import file_writer
3 import renderer
4 import sets
5 import random
6
7 class local_photos_mirror_renderer(renderer.debuggable_abstaining_renderer):
8     """A renderer that uses a local mirror of Google photos"""
9
10     album_root_directory = "/usr/local/export/www/gphotos/albums"
11
12     album_whitelist = sets.ImmutableSet([
13         '1208 Newer Alex Photos',
14         '1013 Scott and Lynn',
15         '0106 Key West 2019',
16         '1017 Olympic Sculpture Park',
17         '0212 Chihuly Glass',
18         '0730 Trip to East Coast \'16',
19         '0715 Barn',
20         '1009 East Coast 2018',
21         '0819 Skiing with Alex',
22         '0819 Friends',
23         '0227 Trip to California, \'16',
24         '0407 London, 2018',
25         '0528 Ohme Gardens',
26         '0809 Bangkok and Phuket, 2003',
27         '0803 Blue Angels... Seafair',
28         '0719 Dunn Gardens',
29         '0514 Krakow 2009',
30         '0515 Tuscany 2008',
31         '0508 Yosemite 2010',
32         '0611 Sonoma',
33         '1025 NJ 2015',
34         '0407 Las Vegas, 2017',
35     ])
36
37     extension_whitelist = sets.ImmutableSet([
38         'jpg',
39         'gif',
40         'JPG',
41         'jpeg',
42         'GIF',
43     ])
44
45     def __init__(self, name_to_timeout_dict):
46         super(local_photos_mirror_renderer, self).__init__(name_to_timeout_dict, False)
47         self.candidate_photos = set()
48
49     def debug_prefix(self):
50         return "local_photos_mirror"
51
52     def periodic_render(self, key):
53         if (key == 'Index Photos'):
54             return self.index_photos()
55         elif (key == 'Choose Photo'):
56             return self.choose_photo()
57         else:
58             raise error('Unexpected operation')
59
60     # Walk the filesystem looking for photos in whitelisted albums and
61     # keep their paths in memory.
62     def index_photos(self):
63         for root, subdirs, files in os.walk(self.album_root_directory):
64             last_dir = root.rsplit('/', 1)[1]
65             if last_dir in self.album_whitelist:
66                 for x in files:
67                     extension = x.rsplit('.', 1)[1]
68                     if extension in self.extension_whitelist:
69                         photo_path = os.path.join(root, x)
70                         photo_url = photo_path.replace(
71                             "/usr/local/export/www/",
72                             "http://10.0.0.18/",
73                             1)
74                         self.candidate_photos.add(photo_url)
75         return True
76
77     # Pick one of the cached URLs and build a page.
78     def choose_photo(self):
79         if len(self.candidate_photos) == 0:
80             print("No photos!")
81             return False
82         path = random.sample(self.candidate_photos, 1)[0]
83         f = file_writer.file_writer('photo_23_none.html')
84         f.write("""
85 <style>
86 body{background-color:#303030;}
87 div#time{color:#dddddd;}
88 div#date{color:#dddddd;}
89 </style>
90 <center>""")
91         f.write('<img src="%s" style="display:block;max-width=800;max-height:600;width:auto;height:auto">' % path)
92         f.write("</center>")
93         f.close()
94         return True
95
96 # Test code
97 #x = local_photos_mirror_renderer({"Index Photos": (60 * 60 * 12),
98 #                                  "Choose Photo": (1)})
99 #x.index_photos()
100 #x.choose_photo()