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