This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpicasa.batch.py
executable file
·410 lines (349 loc) · 14.9 KB
/
picasa.batch.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/python
import sys
import string, re, time
from datetime import datetime
import configparser
import os, signal
import hashlib
from PIL import Image
from PIL.ExifTags import TAGS
from tempfile import mkstemp
import argparse
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run_flow
import atom
import gdata.photos.service, gdata.media, gdata.geo, gdata.gauth
import httplib2
class PicasaClient():
api_key = None
api_secret = None
token = None
gdClient = None
rootpath=None
verbose=False
forceCreateAlbum=False
forceResizePhoto=False
forceNotUploadFiles=False
perm=None
albuns=None
albunsToBeCreated = []
PICASA_MAX_FREE_DIMENSION = 2048
#pattern = re.compile('\.(jpg|avi)$', re.IGNORECASE)
pattern = re.compile('\.jpg$', re.IGNORECASE)
patternWrong = re.compile('^\.', re.IGNORECASE)
def load_stored_credentials(self):
storage = Storage("credentials")
value = storage.get()
if not value:
raise Exception("Invalid token")
self.credentials = value
def save_credentials(self):
storage = Storage("credentials.dat")
storage.put(self.credentials)
def refresh_token(self):
self.credentials.refresh(httplib2.Http())
self.save_credentials()
def connect(self):
flow = OAuth2WebServerFlow(client_id=self.api_key,
client_secret=self.api_secret,
scope='https://picasaweb.google.com/data/',
redirect_uri='http://localhost/return',
access_type='offline', approval_prompt='force')
if self.token:
self.credentials = flow.step2_exchange(self.token)
self.save_credentials()
else:
storage = Storage("credentials.dat")
credentials = storage.get()
if not credentials:
class MyOpts:
pass
flags=MyOpts()
flags.logging_level = 'DEBUG'
flags.noauth_local_webserver = True
flags.auth_host_port = [8100]
flags.auth_host_name = 'localhost'
credentials = run_flow(flow, storage, flags=flags)
self.credentials = credentials
if credentials.access_token_expired:
self.refresh_token()
# auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
self.gdClient = gdata.photos.service.PhotosService(additional_headers={'Authorization' : 'Bearer %s' % self.credentials.access_token})
# self.gdClient = auth2token.authorize(gdClient)
self.load_user_profile()
def load_user_profile(self):
print("Loading user profile")
result = self.gdClient.GetUserFeed()
self.userid = result.user.text
self.gdClient = gdata.photos.service.PhotosService(email=self.userid, additional_headers={'Authorization' : 'Bearer %s' % self.credentials.access_token})
def batchUpload(self, paths):
self.connect()
if (self.verbose): print("Batch upload: ")
for path in paths:
self.batchUploadPath(path)
if len(self.albunsToBeCreated) > 0:
print("Albuns para serem criados:")
for album in self.albunsToBeCreated:
print(album)
def batchUploadPath(self, path):
# Get a list of files
files = []
dirs = []
for filename in os.listdir(path):
if self.patternWrong.search(filename):
continue
file = os.path.join(path, filename)
if os.path.isdir(file):
dirs.append(file)
elif not self.forceNotUploadFiles \
and os.path.isfile(file) \
and self.pattern.search(file):
files.append(filename)
files.sort()
dirs.sort()
albumName=self.transformPath2Album(path)
if (self.verbose): print("Album: %s " % (albumName))
album=self.getAlbum(albumName)
if album is None:
self.albunsToBeCreated.append(albumName)
# if (self.verbose): print("Creating Album: " + albumName)
# album=self.createAlbum(albumName, albumDate)
elif len(files) > 0:
file = os.path.join(path, files[0])
albumDate = self.getAlbumDateFromPhotos(file)
album.timestamp = gdata.photos.Timestamp(text=albumDate)
# self.gdClient.Put(album, album.GetEditLink().href, converter=gdata.photos.AlbumEntryFromString)
photoList = self.getPhotosFromAlbum(album)
noUploads=False
for filename in files:
file = os.path.join(path, filename)
# was the file already uploaded
md5=md5sum(file)
if (self.isPhotoInAlbum(md5, photoList)):
sys.stdout.write(".")
sys.stdout.flush()
noUploads=True
continue
if noUploads: print(""); noUploads=False
if self.forceResizePhoto:
self.resizeAndUploadPhoto(file, filename, md5, album)
else:
self.uploadPhoto(file, filename, md5, album)
if noUploads: print("")
for dir in dirs:
self.batchUploadPath(dir)
def transformPath2Album(self, fullpath):
path = fullpath.strip()
path = re.sub('/$', '', path)
path = re.sub(self.rootpath, '', fullpath)
path = re.sub('/[0-9]+_', '/', path)
path = re.sub('_', ' ', path)
path = re.sub('^/ *', '', path)
path = re.sub('/ *$', '', path)
path = re.sub('/', ' / ', path)
return path
def transformFilename2PhotoTitle(self, filename):
title = self.pattern.sub('', filename)
title = re.sub('_', ' ', title)
return title
def transformPhotoTitle2Tags(self, photoTitle):
tags = re.sub(' +[0-9]+$', '', photoTitle)
tags = re.sub(' ', ',', tags)
return tags
def getAlbum(self, albumName):
if self.albuns is None:
self.albuns = self.getAlbums()
for album in self.albuns:
if album.title.text.strip() == albumName.strip():
return album
return None
def getPhotosFromAlbum(self, album):
photos = self.gdClient.GetFeed('/data/feed/api/user/%s/albumid/%s?kind=photo' % (self.userid, album.gphoto_id.text))
return photos.entry
def createAlbum(self, albumName, albumDate):
while True:
try:
album=self.getAlbum(albumName)
if album is None:
album = self.gdClient.InsertAlbum(albumName, albumName, access=self.perm, timestamp=albumDate)
return album
except Exception as e:
print("Create failed.")
print("Album name %s" % albumName)
# import traceback; traceback.print_exc()
time.sleep(5)
def getAlbums(self):
uri = '/data/feed/api/user/%s?kind=album' % (self.userid)
albums=[]
limit = 500
offset = 1
while True:
albumsBlock=self.gdClient.GetFeed(uri, limit=limit, start_index=offset)
total = int(albumsBlock.total_results.text)
offset = len(albums) + limit
albums.extend(albumsBlock.entry)
# print 'Getting albums: %s' % (len(albums))
if len(albumsBlock.entry)<=1 or offset >= total:
break
print("%s albuns loaded." % len(albums))
return albums
def getAlbumDateFromPhotos(self, file):
try:
img = Image.open(file)
exif = img._getexif()
except:
statinfo = os.stat(file)
dt = '%i' % int(statinfo.st_mtime * 1000)
return dt
if exif != None:
for tag, value in exif.items():
decoded = TAGS.get(tag, tag)
if decoded == 'DateTime':
try:
dt = '%i' % int(time.mktime(time.strptime(value, "%Y:%m:%d %H:%M:%S")) * 1000)
return dt
except ValueError:
return None
statinfo = os.stat(file)
dt = '%i' % int(statinfo.st_mtime * 1000)
return dt
def isPhotoInAlbum(self, md5, photoList):
for photo in photoList:
if photo.checksum.text==md5:
return True
return False
def uploadPhoto(self, file, filename, md5, album):
photoTitle = self.transformFilename2PhotoTitle(filename)
tags = self.transformPhotoTitle2Tags(photoTitle)
entry = gdata.photos.PhotoEntry()
entry.title = atom.Title(text=photoTitle)
entry.summary = atom.Summary(text=photoTitle, summary_type='text')
entry.checksum = gdata.photos.Checksum(text=md5)
entry.media.keywords = gdata.media.Keywords()
entry.media.keywords.text = tags
if (self.verbose): print('%s [%s]' % (filename, photoTitle))
uploaded=False
while uploaded == False:
try:
self.gdClient.InsertPhoto('/data/feed/api/user/default/albumid/%s' % (album.gphoto_id.text), entry, file, content_type='image/jpeg')
uploaded=True
except gdata.photos.service.GooglePhotosException as e:
print("Upload failed. ", e)
self.connect()
time.sleep(2)
def resizeAndUploadPhoto(self, file, filename, md5, album):
try:
img = Image.open(file)
(width, height) = img.size
newDimension = None
ratio = float(width) / float(height)
if width > height and width > self.PICASA_MAX_FREE_DIMENSION:
newHeight = int(self.PICASA_MAX_FREE_DIMENSION / ratio)
newDimension = (self.PICASA_MAX_FREE_DIMENSION, newHeight)
elif height > width and height > self.PICASA_MAX_FREE_DIMENSION:
newWidth = int(self.PICASA_MAX_FREE_DIMENSION * ratio)
newDimension = (newWidth, self.PICASA_MAX_FREE_DIMENSION)
# Create a temporary resized file
if newDimension is not None:
print("Resizing %s (%s, %s) to (%s, %s)" % (filename, width, height, newDimension[0], newDimension[1]))
resizedImage = img.resize(newDimension)
tempFile, tempPath = mkstemp()
resizedImage.save(tempPath, "JPEG", exif=img.info.get('exif', ""))
self.uploadPhoto(tempPath, filename, md5, album)
os.close(tempFile)
os.remove(tempPath)
else:
self.uploadPhoto(file, filename, md5, album)
except Exception as e:
print("Unable to open file %s" % filename)
import traceback; traceback.print_exc()
def normalizeAlbums(self):
self.connect()
self.albuns = self.getAlbums()
for album in self.albuns:
newTitle = album.title.text.strip()
newTitle = re.sub('^/ *', '', newTitle)
newTitle = re.sub('/ *$', '', newTitle)
print("'%s' -> '%s'" % (album.title.text, newTitle))
album.title.text=newTitle
self.gdClient.Put(album, album.GetEditLink().href, converter=gdata.photos.AlbumEntryFromString)
print("normalize")
def deleteAll(self):
self.connect()
self.albuns = self.getAlbums()
delete_worker(self.gdClient, self.albuns)
def delete_worker(gdClient, albuns):
print("Deleting %s albuns" % len(albuns))
for i, album in enumerate(albuns):
if album.GetEditLink():
print("Deleting %s - %s" % (i, album.title.text))
try:
gdClient.Delete(album)
except Exception as e:
print(e)
# import traceback
# traceback.print_stack()
def md5sum(fileName):
m = hashlib.md5()
try:
fd = open(fileName,"rb")
except IOError:
print("Unable to open the file in readmode: " + fileName)
return
content = fd.readlines()
fd.close()
for eachLine in content:
m.update(eachLine)
return m.hexdigest()
def readFromConfigFile(client, args):
configParser = configparser.ConfigParser()
configParser.readfp(args.config)
client.api_key = getParam(args.api_key, configParser, 'api_key')
client.api_secret = getParam(args.api_secret, configParser, 'api_secret')
client.rootpath = getParam(args.rootpath, configParser, 'rootpath')
client.perm = args.perm
client.verbose = args.verbose
client.forceCreateAlbum = args.forceCreateAlbum
client.forceResizePhoto = args.forceResizePhoto
client.token = args.token
def getParam(arg, parser, item):
if arg is not None:
return arg
return parser.get('config', item)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-v', dest='verbose', help='Verbose', action='store_true')
parser.add_argument('--config', dest='config', help='Configuration file', type=argparse.FileType('r'))
parser.add_argument('--apikey', dest='api_key', help='api key')
parser.add_argument('--apisecret', dest='api_secret', help='api secret')
parser.add_argument('--token', dest='token', help='Token returned')
parser.add_argument('--delete-all', dest='delete_all', help='Delete all albuns (be careful)', action='store_true')
parser.add_argument('--root', dest='rootpath', help='Root Path')
parser.add_argument('--folder', dest="folder", help='Upload folder(s)', nargs='+', action='store')
parser.add_argument('-a', dest='normalizeAlbum', help='Normalize album name', action='store_true')
parser.add_argument('-u', dest='upload', help='Upload album', action='store_true')
parser.add_argument('-c', dest="forceCreateAlbum", help='Enforce create album', action='store_true')
parser.add_argument('-r', dest="forceResizePhoto", help='Resize picture bigger than 4900px before upload (don\'t modify the original file)', action='store_true')
parser.add_argument('--perms', dest='perm', action='store', help='Album perms', choices=['public', 'private', 'link'], default='private')
parser.add_argument('--not-upload-files', dest='forceNotUploadFiles', help='Should not upload files', action='store_true')
args = parser.parse_args()
client = PicasaClient()
client.forceNotUploadFiles = args.forceNotUploadFiles
if args.config:
readFromConfigFile(client, args)
if args.perm==True:
pass
elif args.upload==True:
client.batchUpload(args.folder)
elif args.delete_all:
client.deleteAll()
elif args.normalizeAlbum==True:
client.normalizeAlbums()
def trapCtrlC():
def terminateSignalHandler(signal, frame):
sys.exit()
signal.signal(signal.SIGINT, terminateSignalHandler)
trapCtrlC()
if __name__ == "__main__":
main()