9 from oauth2client.client import AccessTokenRefreshError
12 class picasa_renderer(renderer.debuggable_abstaining_renderer):
13 """A renderer to fetch photos from picasaweb.google.com"""
15 album_whitelist = sets.ImmutableSet(
18 "Alex 6.0..8.0 years old",
19 "Alex 3.0..4.0 years old",
21 "Bangkok and Phukey, 2003",
22 "Blue Angels... Seafair",
31 "Hiking and Ohme Gardens",
39 "Oahu 2010" "Ocean Shores 2009",
41 "Olympic Sculpture Park",
50 "Trip to California '16",
51 "Trip to East Coast '16",
57 def __init__(self, name_to_timeout_dict, oauth):
58 super(picasa_renderer, self).__init__(name_to_timeout_dict, False)
65 def debug_prefix(self):
68 def periodic_render(self, key):
69 if key == "Fetch Photos":
70 return self.fetch_photos()
71 elif key == "Shuffle Cached Photos":
72 return self.shuffle_cached()
74 raise error("Unexpected operation")
76 # Just fetch and cache the photo URLs in memory.
77 def fetch_photos(self):
83 conn = http.client.HTTPSConnection("photoslibrary.googleapis.com")
89 "Authorization": "%s %s"
90 % (self.oauth.token["token_type"], self.oauth.token["access_token"])
93 response = conn.getresponse()
94 if response.status != 200:
95 print(("Failed to fetch albums, status %d\n" % response.status))
96 print(response.read())
97 albums = self.pws.GetUserFeed().entry
99 if album.title.text not in picasa_renderer.album_whitelist:
101 photos = self.pws.GetFeed(
102 "/data/feed/api/user/%s/albumid/%s?kind=photo&imgmax=1024u"
103 % (secrets.google_username, album.gphoto_id.text)
105 for photo in photos.entry:
106 id = "%s/%s" % (photo.albumid.text, photo.gphoto_id.text)
107 temp_is_video[id] = False
109 for x in photo.media.content:
110 if "video" in x.type and int(x.height) < resolution:
112 resolution = int(x.height)
113 temp_width[id] = x.width
114 temp_height[id] = x.height
115 temp_is_video[id] = True
117 if resolution == 999999:
119 temp_width[id] = x.width
120 temp_height[id] = x.height
121 temp_is_video[id] = False
122 temp_photo_urls[id] = url
123 self.photo_urls = temp_photo_urls
124 self.width = temp_width
125 self.height = temp_height
126 self.is_video = temp_is_video
129 gdata.service.RequestError,
130 gdata.photos.service.GooglePhotosException,
131 AccessTokenRefreshError,
133 print("******** TRYING TO REFRESH PHOTOS CLIENT *********")
134 self.oauth.refresh_token()
135 self.client = self.oauth.photos_service()
138 # Pick one of the cached URLs and build a page.
139 def shuffle_cached(self):
140 if len(self.photo_urls) == 0:
143 pid = random.sample(self.photo_urls, 1)
146 if self.is_video[id]:
149 f = file_writer.file_writer("photo_23_none.html")
153 body{background-color:#303030;}
154 div#time{color:#dddddd;}
155 div#date{color:#dddddd;}
159 if self.is_video[id]:
161 '<iframe src="%s" seamless width=%s height=%s></iframe>'
162 % (self.photo_urls[id], self.width[id], self.height[id])
166 '<img src="%s" width=%s alt="%s">'
167 % (self.photo_urls[id], self.width[id], self.photo_urls[id])
175 oauth = gdata_oauth.OAuth(secrets.google_client_id, secrets.google_client_secret)
176 oauth.get_new_token()
177 if not oauth.has_token():
178 user_code = oauth.get_user_code()
179 print("------------------------------------------------------------")
182 'Go to %s and enter the code "%s" (no quotes, case-sensitive)'
183 % (oauth.verification_url, user_code)
186 oauth.get_new_token()
188 {"Fetch Photos": (60 * 60 * 12), "Shuffle Cached Photos": (1)}, oauth