-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicasaweb.py
137 lines (108 loc) · 4.17 KB
/
picasaweb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import gdata
import gdata.gauth
import gdata.photos.service
import httplib2
import os.path
import PIL.Image
import sys
import time
import webbrowser
from oauth2client import client
from oauth2client.file import Storage
def _authorize(self, client):
client.auth_token = self
request_orig = client.http_client.request
def new_request(*args, **kwd):
response = request_orig(*args, **kwd)
if response.status == 401:
refresh_response = self._refresh(request_orig)
if self._invalid:
return refresh_response
else:
self.modify_request(*args, **kwd)
return request_orig(*args, **kwd)
else:
return response
client.http_client.request = new_request
return client
gdata.gauth.OAuth2Token.authorize = _authorize
def _get_credentials(http):
storage = Storage('/home/btsai/.ftp-to-picasaweb-bridge/credentials')
credentials = storage.get()
if (credentials is not None and
credentials.refresh_token is not None):
if credentials.access_token_expired:
credentials.refresh(http)
return credentials
flow = client.flow_from_clientsecrets(
'/home/btsai/.ftp-to-picasaweb-bridge/client_secrets.json',
scope='https://picasaweb.google.com/data/',
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
auth_uri = flow.step1_get_authorize_url()
sys.stdout.write("Opening window for authorization...\n")
sys.stdout.flush()
# webbrowser.open_new(auth_uri)
print auth_uri
sys.stdout.write("Authorization Code: ")
sys.stdout.flush()
auth_code = sys.stdin.readline()
auth_code = auth_code[:-1]
credentials = flow.step2_exchange(auth_code)
storage.put(credentials)
return credentials
http = httplib2.Http()
credentials = _get_credentials(http)
http_auth = credentials.authorize(http)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
gd_client = gdata.photos.service.PhotosService(
email="default",
additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token}) # For example.
gd_client = auth2token.authorize(gd_client)
albums = gd_client.GetUserFeed()
def _get_album(client, name, refresh=False):
global albums
if refresh:
albums = client.GetUserFeed()
for album in albums.entry:
if album.title.text == name and int(album.numphotos) < int(albums.maxPhotosPerAlbum):
return album
if refresh:
print "Creating album %s" % name
return client.InsertAlbum(title=name, summary='', access='private')
else:
return _get_album(client, name, True)
def _upload_photo(client, path):
image = PIL.Image.open(path)
datetime = str(image._getexif()[306]) # 306 is DateTime
del image
parsed_datetime = time.strptime(datetime, "%Y:%m:%d %H:%M:%S")
album_name = time.strftime("%m-%Y (auto)", parsed_datetime)
album = _get_album(client, album_name)
album_url = '/data/feed/api/user/default/albumid/%s' % (album.gphoto_id.text)
photo = client.InsertPhotoSimple(
album_url, os.path.basename(path), None, path, content_type='image/jpeg')
def upload_photo(path):
global auth2token
global credentials
global gd_client
global http
attempts = 0
while True:
try:
if credentials is None or credentials.access_token_expired:
credentials = _get_credentials(http)
http_auth = credentials.authorize(http)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
gd_client = gdata.photos.service.PhotosService(
email="default",
additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token}) # For example.
gd_client = auth2token.authorize(gd_client)
_upload_photo(gd_client, path)
break
except:
attempts = attempts + 1
print "Unexpected error:", sys.exc_info()
print "Sleeping for", (attempts * 10)
time.sleep(attempts * 10)
credentials = None
# filename = "/shared/iss/photos/2015-04-07/BU2A4400.JPG"