import http.client import gdata_oauth import file_writer import renderer import gdata import secrets import sets import random from oauth2client.client import AccessTokenRefreshError class picasa_renderer(renderer.debuggable_abstaining_renderer): """A renderer to fetch photos from picasaweb.google.com""" album_whitelist = sets.ImmutableSet( [ "Alex", "Alex 6.0..8.0 years old", "Alex 3.0..4.0 years old", "Barn", "Bangkok and Phukey, 2003", "Blue Angels... Seafair", "Carol Ann and Owen", "Chahuly Glass", "Dunn Gardens", "East Coast, 2011", "East Coast, 2013", "Friends", "Gasches", "Gasch Wedding", "Hiking and Ohme Gardens", "Hiking", "Karen's Wedding", "Key West 2019", "Krakow 2009", "Munich, July 2018", "NJ 2015", "NW Trek", "Oahu 2010" "Ocean Shores 2009", "Ohme Gardens", "Olympic Sculpture Park", "Paintings", "Puerto Vallarta", "Photos from posts", "Random", "SFO 2014", "Soccer", "Skiing with Alex", "Tuscany 2008", "Trip to California '16", "Trip to East Coast '16", "Yosemite 2010", "Zoo", ] ) def __init__(self, name_to_timeout_dict, oauth): super(picasa_renderer, self).__init__(name_to_timeout_dict, False) self.oauth = oauth self.photo_urls = {} self.width = {} self.height = {} self.is_video = {} def debug_prefix(self): return "picasa" def periodic_render(self, key): if key == "Fetch Photos": return self.fetch_photos() elif key == "Shuffle Cached Photos": return self.shuffle_cached() else: raise error("Unexpected operation") # Just fetch and cache the photo URLs in memory. def fetch_photos(self): try: temp_photo_urls = {} temp_width = {} temp_height = {} temp_is_video = {} conn = http.client.HTTPSConnection("photoslibrary.googleapis.com") conn.request( "GET", "/v1/albums", None, { "Authorization": "%s %s" % (self.oauth.token["token_type"], self.oauth.token["access_token"]) }, ) response = conn.getresponse() if response.status != 200: print(("Failed to fetch albums, status %d\n" % response.status)) print(response.read()) albums = self.pws.GetUserFeed().entry for album in albums: if album.title.text not in picasa_renderer.album_whitelist: continue photos = self.pws.GetFeed( "/data/feed/api/user/%s/albumid/%s?kind=photo&imgmax=1024u" % (secrets.google_username, album.gphoto_id.text) ) for photo in photos.entry: id = "%s/%s" % (photo.albumid.text, photo.gphoto_id.text) temp_is_video[id] = False resolution = 999999 for x in photo.media.content: if "video" in x.type and int(x.height) < resolution: url = x.url resolution = int(x.height) temp_width[id] = x.width temp_height[id] = x.height temp_is_video[id] = True else: if resolution == 999999: url = x.url temp_width[id] = x.width temp_height[id] = x.height temp_is_video[id] = False temp_photo_urls[id] = url self.photo_urls = temp_photo_urls self.width = temp_width self.height = temp_height self.is_video = temp_is_video return True except ( gdata.service.RequestError, gdata.photos.service.GooglePhotosException, AccessTokenRefreshError, ): print("******** TRYING TO REFRESH PHOTOS CLIENT *********") self.oauth.refresh_token() self.client = self.oauth.photos_service() return False # Pick one of the cached URLs and build a page. def shuffle_cached(self): if len(self.photo_urls) == 0: print("No photos!") return False pid = random.sample(self.photo_urls, 1) id = pid[0] refresh = 15 if self.is_video[id]: refresh = 60 f = file_writer.file_writer("photo_23_none.html") f.write( """
""" ) if self.is_video[id]: f.write( '' % (self.photo_urls[id], self.width[id], self.height[id]) ) else: f.write( '%s' % (self.photo_urls[id], self.width[id], self.photo_urls[id]) ) f.write("
") f.close() return True # Test code oauth = gdata_oauth.OAuth(secrets.google_client_id, secrets.google_client_secret) oauth.get_new_token() if not oauth.has_token(): user_code = oauth.get_user_code() print("------------------------------------------------------------") print( ( 'Go to %s and enter the code "%s" (no quotes, case-sensitive)' % (oauth.verification_url, user_code) ) ) oauth.get_new_token() x = picasa_renderer( {"Fetch Photos": (60 * 60 * 12), "Shuffle Cached Photos": (1)}, oauth ) x.fetch_photos()