X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=local_photos_mirror_renderer.py;h=b68df68096e3fa0b7ce0dd9b94bb6bd9ccc5be3e;hb=bfde32ea8f021da27fb2cdf535efb0e9c465d6a2;hp=020683d00e8b8c793aa500006708e463aff87744;hpb=4b1f3d8a8b278ca6d62f461ea80c8ea21080c301;p=kiosk.git diff --git a/local_photos_mirror_renderer.py b/local_photos_mirror_renderer.py index 020683d..b68df68 100644 --- a/local_photos_mirror_renderer.py +++ b/local_photos_mirror_renderer.py @@ -1,100 +1,133 @@ +#!/usr/bin/env python3 + import os +import random +import re +from typing import Dict, Set + import file_writer import renderer -import sets -import random -class local_photos_mirror_renderer(renderer.debuggable_abstaining_renderer): + +class local_photos_mirror_renderer(renderer.abstaining_renderer): """A renderer that uses a local mirror of Google photos""" - album_root_directory = "/usr/local/export/www/gphotos/albums" + album_root_directory = "/var/www/html/kiosk/images/gphotos/albums" - album_whitelist = sets.ImmutableSet([ - '1208 Newer Alex Photos', - '1013 Scott and Lynn', - '0106 Key West 2019', - '1017 Olympic Sculpture Park', - '0212 Chihuly Glass', - '0730 Trip to East Coast \'16', - '0715 Barn', - '1009 East Coast 2018', - '0819 Skiing with Alex', - '0819 Friends', - '0227 Trip to California, \'16', - '0407 London, 2018', - '0528 Ohme Gardens', - '0809 Bangkok and Phuket, 2003', - '0803 Blue Angels... Seafair', - '0719 Dunn Gardens', - '0514 Krakow 2009', - '0515 Tuscany 2008', - '0508 Yosemite 2010', - '0611 Sonoma', - '1025 NJ 2015', - '0407 Las Vegas, 2017', - ]) + album_whitelist = frozenset( + [ + "Autumn at Kubota", + "Bangkok and Phuket, 2003", + "Barn", + "Blue Angels... Seafair", + "Chihuly Glass", + "Dunn Gardens", + "East Coast 2018", + "Fall '17", + "Friends", + "Hiking", + "Key West 2019", + "Krakow 2009", + "Kubota Gardens", + "Las Vegas, 2017", + "London, 2018", + "Munich, July 2018", + "NJ 2015", + "Newer Alex Photos", + "Ohme Gardens", + "Olympic Sculpture Park", + "Portland, ME 2021", + "Prague and Munich 2019", + "Random", + "SFO 2014", + "Scott and Lynn", + "Sculpture Place", + "Skiing with Alex", + "Sonoma", + "Trip to California, '16", + "Trip to San Francisco", + "Trip to East Coast '16", + "Turkey 2022", + "Tuscany 2008", + "WA Roadtrip, 2021", + "Yosemite 2010", + "Zoo", + ] + ) - extension_whitelist = sets.ImmutableSet([ - 'jpg', - 'gif', - 'JPG', - 'jpeg', - 'GIF', - ]) + extension_whitelist = frozenset( + [ + "jpg", + "gif", + "JPG", + "jpeg", + "GIF", + ] + ) - def __init__(self, name_to_timeout_dict): - super(local_photos_mirror_renderer, self).__init__(name_to_timeout_dict, False) - self.candidate_photos = set() + def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None: + super().__init__(name_to_timeout_dict) + self.candidate_photos: Set[str] = set() - def debug_prefix(self): + def debug_prefix(self) -> str: return "local_photos_mirror" - def periodic_render(self, key): - if (key == 'Index Photos'): + def periodic_render(self, key: str) -> bool: + if key == "Index Photos": return self.index_photos() - elif (key == 'Choose Photo'): + elif key == "Choose Photo": return self.choose_photo() else: - raise error('Unexpected operation') + raise Exception("Unexpected operation") + + def album_is_in_whitelist(self, name: str) -> bool: + for wlalbum in self.album_whitelist: + if re.search("\d+ %s" % wlalbum, name) is not None: + return True + return False - # Walk the filesystem looking for photos in whitelisted albums and - # keep their paths in memory. - def index_photos(self): + def index_photos(self) -> bool: + """Walk the filesystem looking for photos in whitelisted albums and + keep their paths in memory. + """ for root, subdirs, files in os.walk(self.album_root_directory): - last_dir = root.rsplit('/', 1)[1] - if last_dir in self.album_whitelist: - for x in files: - extension = x.rsplit('.', 1)[1] + last_dir = root.rsplit("/", 1)[1] + if self.album_is_in_whitelist(last_dir): + for filename in files: + extension = filename.rsplit(".", 1)[1] if extension in self.extension_whitelist: - photo_path = os.path.join(root, x) + photo_path = os.path.join(root, filename) photo_url = photo_path.replace( - "/usr/local/export/www/", - "http://10.0.0.18/", - 1) + "/var/www/html", "http://kiosk.house/", 1 + ) self.candidate_photos.add(photo_url) return True - # Pick one of the cached URLs and build a page. def choose_photo(self): + """Pick one of the cached URLs and build a page.""" if len(self.candidate_photos) == 0: print("No photos!") return False path = random.sample(self.candidate_photos, 1)[0] - f = file_writer.file_writer('photo_23_none.html') - f.write(""" + with file_writer.file_writer("photo_23_3600.html") as f: + f.write( + """ -
""") - f.write('' % path) - f.write("
") - f.close() +
""" + ) + f.write( + f'' + ) + f.write("
") return True + # Test code -#x = local_photos_mirror_renderer({"Index Photos": (60 * 60 * 12), +# x = local_photos_mirror_renderer({"Index Photos": (60 * 60 * 12), # "Choose Photo": (1)}) -#x.index_photos() -#x.choose_photo() +# x.index_photos() +# x.choose_photo()