-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgooglephotos.py
177 lines (127 loc) · 6.17 KB
/
googlephotos.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import AuthorizedSession
from google.oauth2.credentials import Credentials
import json
import os.path
import argparse
import logging
def auth(scopes):
flow = InstalledAppFlow.from_client_secrets_file(
'',
scopes=scopes)
credentials = flow.run_local_server(host='localhost',
port=8080,
authorization_prompt_message="",
success_message='The auth flow is complete; you may close this window.',
open_browser=True)
return credentials
def get_authorized_session(auth_token_file):
scopes=['https://www.googleapis.com/auth/photoslibrary',
'https://www.googleapis.com/auth/photoslibrary.sharing']
cred = None
if auth_token_file:
try:
cred = Credentials.from_authorized_user_file(auth_token_file, scopes)
except OSError as err:
logging.debug("Error opening auth token file - {0}".format(err))
except ValueError:
logging.debug("Error loading auth tokens - Incorrect format")
if not cred:
cred = auth(scopes)
session = AuthorizedSession(cred)
if auth_token_file:
try:
save_cred(cred, auth_token_file)
except OSError as err:
logging.debug("Could not save auth tokens - {0}".format(err))
return session
def save_cred(cred, auth_file):
cred_dict = {
'token': cred.token,
'refresh_token': cred.refresh_token,
'id_token': cred.id_token,
'scopes': cred.scopes,
'token_uri': cred.token_uri,
'client_id': cred.client_id,
'client_secret': cred.client_secret
}
with open(auth_file, 'w') as f:
print(json.dumps(cred_dict), file=f)
# Generator to loop through all albums
def getAlbums(session, appCreatedOnly=False):
params = {
'excludeNonAppCreatedData': appCreatedOnly
}
while True:
albums = session.get('https://photoslibrary.googleapis.com/v1/albums', params=params).json()
logging.debug("Server response: {}".format(albums))
if 'albums' in albums:
for a in albums["albums"]:
yield a
if 'nextPageToken' in albums:
params["pageToken"] = albums["nextPageToken"]
else:
return
else:
return
def create_or_retrieve_album(session, album_title):
# Find albums created by this app to see if one matches album_title
for a in getAlbums(session, True):
if a["title"].lower() == album_title.lower():
album_id = a["id"]
logging.info("Uploading into EXISTING photo album -- \'{0}\'".format(album_title))
return album_id
# No matches, create new album
create_album_body = json.dumps({"album":{"title": album_title}})
#print(create_album_body)
resp = session.post('https://photoslibrary.googleapis.com/v1/albums', create_album_body).json()
logging.debug("Server response: {}".format(resp))
if "id" in resp:
logging.info("Uploading into NEW photo album -- \'{0}\'".format(album_title))
return resp['id']
else:
logging.error("Could not find or create photo album '\{0}\'. Server Response: {1}".format(album_title, resp))
return None
def upload_photos(session, photo_file_list, album_name):
print("Uploading photos...")
album_id = create_or_retrieve_album(session, album_name) if album_name else None
# interrupt upload if an upload was requested but could not be created
if album_name and not album_id:
return
session.headers["Content-type"] = "application/octet-stream"
session.headers["X-Goog-Upload-Protocol"] = "raw"
for photo_file_name in photo_file_list:
try:
photo_file = open(photo_file_name, mode='rb')
photo_bytes = photo_file.read()
except OSError as err:
logging.error("Could not read file \'{0}\' -- {1}".format(photo_file_name, err))
continue
session.headers["X-Goog-Upload-File-Name"] = os.path.basename(photo_file_name)
logging.info("Uploading photo -- \'{}\'".format(photo_file_name))
print("Uploading photo -- \'{}\'".format(photo_file_name)) # TODO: fix logging and remove this
upload_token = session.post('https://photoslibrary.googleapis.com/v1/uploads', photo_bytes)
if (upload_token.status_code == 200) and (upload_token.content):
create_body = json.dumps({"albumId":album_id, "newMediaItems":[{"description":"","simpleMediaItem":{"uploadToken":upload_token.content.decode()}}]}, indent=4)
resp = session.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', create_body).json()
logging.debug("Server response: {}".format(resp))
if "newMediaItemResults" in resp:
status = resp["newMediaItemResults"][0]["status"]
if status.get("code") and (status.get("code") > 0):
logging.error("Could not add \'{0}\' to library -- {1}".format(os.path.basename(photo_file_name), status["message"]))
else:
logging.info("Added \'{}\' to library and album \'{}\' ".format(os.path.basename(photo_file_name), album_name))
else:
logging.error("Could not add \'{0}\' to library. Server Response -- {1}".format(os.path.basename(photo_file_name), resp))
else:
logging.error("Could not upload \'{0}\'. Server Response - {1}".format(os.path.basename(photo_file_name), upload_token))
try:
del(session.headers["Content-type"])
del(session.headers["X-Goog-Upload-Protocol"])
del(session.headers["X-Goog-Upload-File-Name"])
except KeyError:
pass
def upload_folder(folder, album='media', auth_file=''):
photos = [ os.path.join(folder, x) for x in os.listdir(folder)]
session = get_authorized_session(auth_file)
upload_photos(session, photos, album)