-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrovebox-uploader.py
executable file
·221 lines (173 loc) · 6.69 KB
/
trovebox-uploader.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
#!/usr/bin/env python
# encoding: utf-8
"""
trovebox-uploader.py
version 0.4.2
Created by Magnus Wahlberg on 2013-11-27.
Copyright (c) 2012 Wahlberg Research And Development. All rights reserved.
"""
import os
import sys
import imghdr
import hashlib
import argparse
from trovebox import Trovebox
from trovebox.errors import TroveboxError, TroveboxDuplicateError
# Init
try:
client = Trovebox()
client.configure(api_version=2)
except IOError, E:
print 'Could not connect to Trovebox'
print 'Please check ~/.config/trovebox/default.'
print 'More info: https://github.com/photo/openphoto-python'
raise E
def is_folder(path):
return os.path.isdir(path)
def image_uploaded(photo, return_data=False):
res = client.photos.list(hash=hash_file(photo))
if return_data:
return res
if len(res) > 0:
return True
return False
def is_image(file_path):
valid_types = ["jpg", "jpeg", "gif", "png"]
try:
image_type = imghdr.what(file_path)
except IOError, E:
return False
if image_type in valid_types:
return True
else:
return False
def upload_folder(folder_path, tagslist=[], albums=[],
public=False, update_metadata=False):
if albums:
albums = get_album_ids(albums)
# Convert list of tags to strings
tags = list_to_string(tagslist)
albums = list_to_string(albums)
uploaded_files = 0
for file_path in scan_folder(folder_path):
if is_image(file_path):
if upload_photo(file_path, tags, albums, public, update_metadata):
uploaded_files += 1
return uploaded_files
def scan_folder(path):
files = []
if not os.path.exists(path):
print path, "does not exist"
return files
for file in os.listdir(path):
files.append(os.path.join(path, file))
return files
def get_album_ids(album_names):
albums = client.albums.list(pageSize=1000)
album_ids = []
for album_name in album_names:
album_id = [album.id for album in albums if album.name == album_name]
if not album_id:
sys.stderr.write("< No album named " + album_name + " ignoring > ")
else:
album_ids.append(album_id[0])
return album_ids
def upload_single_photo(path, tagslist=[], albums=[],
public=False, update_metadata=False):
if albums:
albums = get_album_ids(albums)
# Convert list of tags to strings
tags = list_to_string(tagslist)
albums = list_to_string(albums)
upload_photo(path, tags, albums, public, update_metadata)
def upload_photo(path, tags, albums, public, update_metadata):
sys.stderr.write('uploading ' + path + " ")
if CHECK_DUPLICATES_LOCALLY:
if image_uploaded(path):
if update_metadata:
sys.stderr.write('- already uploaded (preupload check), updating metadata')
update_photo_metadata(path, tags, albums, public)
return False
else:
sys.stderr.write('- already uploaded (preupload check) - Ok!\n')
return False
try:
client.photo.upload(path.decode(sys.getfilesystemencoding()),
tags=tags, albums=albums, permission=public)
sys.stderr.write('- Ok!\n')
except TroveboxDuplicateError:
if update_metadata:
sys.stderr.write('- already uploaded, updating metadata')
update_photo_metadata(path, tags, albums, public)
return False
else:
sys.stderr.write('- already uploaded - Ok\n')
except TroveboxError, e:
print e.message
except IOError, e:
sys.stderr.write('- Failed!\n')
print e
return True
def update_photo_metadata(path, tags, albums, public):
photo = image_uploaded(path, True)
# Add albums if needed
if albums:
for album in albums.split(","):
try:
client.album.add(album, photo, "photo")
except TroveboxError, e:
print e
sys.stderr.write('- Failed!\n')
try:
client.photo.update(photo[0], tags=tags, permission=public)
sys.stderr.write(' - Ok\n')
except TroveboxError, e:
sys.stderr.write('- Failed!\n')
print e.message
except IOError, e:
sys.stderr.write('- Failed!\n')
print e
def list_to_string(string_list):
# Convert list to string
joined_string = ','.join([str(item) for item in string_list])
return joined_string
def hash_file(filepath):
sha1 = hashlib.sha1()
f = open(filepath, 'rb')
try:
sha1.update(f.read())
finally:
f.close()
return sha1.hexdigest()
def recursive_search(rootdir):
return [f[0] for f in os.walk(rootdir)]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", required=True, dest='path', help="Path to a file or directory to upload. If directory, only uploads file in top level. To scan subfolders use -r")
parser.add_argument("-c", "--check-duplicates-locally", default=False, action="store_true", help="Check for aldready uploaded images locally, increases number of request to the server, but can increase speed if you have duplicates in the images you are uploading.")
parser.add_argument("-t", "--tags", nargs='+', default=[], help="List of tags to add to the uploaded files")
parser.add_argument("-a", "--albums", nargs='+', default=[], help="Albums to add the images to")
parser.add_argument("-p", "--public", default=False, action="store_true", help="Make the images uploaded public, default False")
parser.add_argument("-r", "--recursive", default=False, action="store_true", help="Also upload subfolders if target is a folder, default False")
parser.add_argument("-u", "--update-metadata", default=False, action="store_true", help="Also update metadata for images aldready uploaded. (Tags, albums etc)")
args = parser.parse_args()
global CHECK_DUPLICATES_LOCALLY
CHECK_DUPLICATES_LOCALLY = args.check_duplicates_locally
tags = args.tags
path = args.path
albums = args.albums
public = args.public
recursive = args.recursive
update_metadata = args.update_metadata
if is_folder(path):
if recursive:
for folder in recursive_search(path):
uploaded_files = upload_folder(folder, tags, albums, public, update_metadata)
print "\nUploaded", uploaded_files, "images from", folder, "\n"
else:
uploaded_files = upload_folder(path, tags, albums, public, update_metadata)
print "\nUploaded", uploaded_files, "images from", path, "\n"
else:
upload_single_photo(path, tags, albums, public, update_metadata)
if __name__ == '__main__':
main()