-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.py
61 lines (49 loc) · 1.88 KB
/
local.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
import os
import re
import flickr
class Photo:
def __init__(self, path, data = None):
self.path = path
self.title = path.split('/')[-1][0:-4]
self.size = os.path.getsize(self.path)
self.data = data
def upload(self, callback = None):
photo_id = flickr.upload_photo(self.path, self.title, callback)
return photo_id
class Photoset:
def __init__(self, dir, data = None):
self.dir = dir
self.title = dir.split('/')[-1]
self.photos = []
self.data = data
self.__load_dir(dir)
def __load_dir(self, dir):
for filename in os.listdir(dir):
if os.path.isdir(dir + '/' + filename):
self.__load_dir(dir + '/' + filename)
if re.search('\.jpg', filename.lower()):
self.photos.append(Photo(dir + '/' + filename))
def size(self):
return sum([photo.size for photo in self.photos])
def upload(self, callback = None):
'''
the title should not be duplicated
'''
photoset_ids = flickr.find_photoset_ids(self.title)
photoset_id = None
uploaded_count = 0
for photo in self.photos:
def __upload_callback(progress, done):
callback(self, photo, progress, done)
if callback: callback(self, photo, 0, False) # begin upload
photo_id = photo.upload(__upload_callback if callback else None)
uploaded_count += 1
if callback: callback(self, photo, 0, True) # end upload
print u"uploaded({0}/{1}): {2}({3:.2f} MB)".format(uploaded_count, len(self.photos), photo.title, photo.size / 1024 / 1024.)
if photoset_id == None:
if len(photoset_ids) > 0:
photoset_id = photoset_ids[-1] # choose latest photoset
else:
photoset_id = flickr.create_photoset(self.title, "", photo_id)
continue
flickr.add_photo(photoset_id, photo_id) # set photoset