9 from oauth2client.client import AccessTokenRefreshError
11 class picasa_renderer(renderer.debuggable_abstaining_renderer):
12 """A renderer to fetch photos from picasaweb.google.com"""
14 album_whitelist = sets.ImmutableSet([
16 'Alex 6.0..8.0 years old',
17 'Alex 3.0..4.0 years old',
19 'Bangkok and Phukey, 2003',
20 'Blue Angels... Seafair',
29 'Hiking and Ohme Gardens',
40 'Olympic Sculpture Park',
49 "Trip to California '16",
50 "Trip to East Coast '16",
55 def __init__(self, name_to_timeout_dict, oauth):
56 super(picasa_renderer, self).__init__(name_to_timeout_dict, False)
63 def debug_prefix(self):
66 def periodic_render(self, key):
67 if (key == 'Fetch Photos'):
68 return self.fetch_photos()
69 elif (key == 'Shuffle Cached Photos'):
70 return self.shuffle_cached()
72 raise error('Unexpected operation')
74 # Just fetch and cache the photo URLs in memory.
75 def fetch_photos(self):
81 conn = http.client.HTTPSConnection("photoslibrary.googleapis.com")
85 { "Authorization": "%s %s" % (self.oauth.token['token_type'], self.oauth.token['access_token'])
87 response = conn.getresponse()
88 if response.status != 200:
89 print(("Failed to fetch albums, status %d\n" % response.status))
90 print(response.read())
91 albums = self.pws.GetUserFeed().entry
93 if (album.title.text not in picasa_renderer.album_whitelist):
95 photos = self.pws.GetFeed(
96 '/data/feed/api/user/%s/albumid/%s?kind=photo&imgmax=1024u' %
97 (secrets.google_username, album.gphoto_id.text))
98 for photo in photos.entry:
99 id = '%s/%s' % (photo.albumid.text, photo.gphoto_id.text)
100 temp_is_video[id] = False
102 for x in photo.media.content:
103 if "video" in x.type and int(x.height) < resolution:
105 resolution = int(x.height)
106 temp_width[id] = x.width
107 temp_height[id] = x.height
108 temp_is_video[id] = True
110 if resolution == 999999:
112 temp_width[id] = x.width
113 temp_height[id] = x.height
114 temp_is_video[id] = False
115 temp_photo_urls[id] = url
116 self.photo_urls = temp_photo_urls
117 self.width = temp_width
118 self.height = temp_height
119 self.is_video = temp_is_video
121 except (gdata.service.RequestError,
122 gdata.photos.service.GooglePhotosException,
123 AccessTokenRefreshError):
124 print("******** TRYING TO REFRESH PHOTOS CLIENT *********")
125 self.oauth.refresh_token()
126 self.client = self.oauth.photos_service()
129 # Pick one of the cached URLs and build a page.
130 def shuffle_cached(self):
131 if len(self.photo_urls) == 0:
134 pid = random.sample(self.photo_urls, 1)
137 if (self.is_video[id]): refresh = 60
139 f = file_writer.file_writer('photo_23_none.html')
142 body{background-color:#303030;}
143 div#time{color:#dddddd;}
144 div#date{color:#dddddd;}
147 if self.is_video[id]:
148 f.write('<iframe src="%s" seamless width=%s height=%s></iframe>' % (self.photo_urls[id], self.width[id], self.height[id]))
150 f.write('<img src="%s" width=%s alt="%s">' % (self.photo_urls[id], self.width[id], self.photo_urls[id]))
156 oauth = gdata_oauth.OAuth(secrets.google_client_id,
157 secrets.google_client_secret)
158 oauth.get_new_token()
159 if not oauth.has_token():
160 user_code = oauth.get_user_code()
161 print('------------------------------------------------------------')
162 print(('Go to %s and enter the code "%s" (no quotes, case-sensitive)' % (
163 oauth.verification_url, user_code)))
164 oauth.get_new_token()
165 x = picasa_renderer({"Fetch Photos": (60 * 60 * 12),
166 "Shuffle Cached Photos": (1)},