-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
289 lines (216 loc) · 7.2 KB
/
utils.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
import base64
import getpass
import hashlib
import json
import os
import time
import pyexiv2
import re
import datetime
primary_backup_marker = "destination_folder"
secondary_backup_marker = "secondary_backup"
def format_time(_time):
days = int(_time // 86400)
hours = int(_time // 3600 % 24)
minutes = int(_time // 60 % 60)
seconds = int(_time % 60)
result = ""
if days > 0:
result += str(days) + " days, "
if hours > 0:
result += str(hours) + " hours, "
if minutes > 0:
result += str(minutes) + " minutes, "
if seconds > 0:
result += str(seconds) + " seconds"
if result:
return result
return "0 seconds"
def format_eta(bits_per_second, elapsed_bits, total_bits):
remaining_bits = total_bits - elapsed_bits
remaining_time = remaining_bits / bits_per_second
return format_time(remaining_time)
def sizeof_fmt(num, suffix='B'):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def get_file_size(file_name):
try:
return os.path.getsize(file_name)
except:
return 0
def is_picture(file_name):
fname, fext = os.path.splitext(file_name)
return fext.lower() in [".jpg", ".jpeg", ".png"]
def get_md5sum_from_file(file_name):
m = hashlib.md5()
with open(file_name, "rb") as f:
while True:
buf = f.read(128)
if not buf:
break
m.update(buf)
return m.hexdigest()
def get_exif_value(exif_data, key):
try:
return exif_data[key].value
except Exception:
return None
def read_picture_date(exif_data):
obj_date = get_exif_value(exif_data, 'Exif.Image.DateTime')
if obj_date is None:
obj_date = get_exif_value(exif_data, 'Exif.Photo.DateTimeOriginal')
if obj_date is None:
obj_date = get_exif_value(exif_data, 'Exif.Photo.DateTimeDigitized')
return obj_date
def date_from_exif_data(filename):
try:
exif_data = pyexiv2.ImageMetadata(filename)
exif_data.read()
obj_date = read_picture_date(exif_data)
except Exception as ex:
obj_date = None
return obj_date
def get_date_from_foldername(filename):
dirname = os.path.basename(os.path.dirname(filename))
if dirname is None or len(dirname) < 4:
return None
return get_date_from_filename(dirname)
def get_date_from_filename(filename):
try:
date_str = re.sub("[^\\d]", "", os.path.basename(filename))
if len(date_str) < 8:
return get_date_from_foldername(filename)
year = int(date_str[0:4])
if year < 1971 or year > datetime.datetime.now().year:
return get_date_from_foldername(filename)
month = int(date_str[4:6])
day = int(date_str[6:8])
hour = 0
min = 0
sec = 0
if len(date_str) >= 12:
try:
hour = int(date_str[8:10])
if hour > 23:
hour = 23
min = int(date_str[10:12])
if min > 59:
min = 59
if len(date_str) >= 14:
sec = int(date_str[12:14])
if sec > 59:
sec = 59
except:
print("Error parsing time")
date = datetime.datetime(year, month, day, hour, min, sec)
return date
except Exception as e:
return get_date_from_foldername(filename)
def get_date_from_file_date(filename):
file_date = time.localtime(os.path.getctime(filename))
obj_date = datetime.datetime(file_date.tm_year, file_date.tm_mon, file_date.tm_mday,
file_date.tm_hour, file_date.tm_min, file_date.tm_sec)
return obj_date
def get_picture_date(picture_path):
obj_date = None
if is_picture(picture_path):
obj_date = date_from_exif_data(picture_path)
if obj_date is None:
obj_date = get_date_from_filename(picture_path)
if obj_date is None:
obj_date = get_date_from_file_date(picture_path)
return obj_date
def get_api_keys_from_config(config_file_name):
config_file_name = os.path.expanduser(config_file_name)
try:
config = json.load(open(config_file_name))
api_key = unicode(config["api_key"])
api_secret = unicode(config["api_secret"])
return api_key, api_secret
except Exception:
return None, None
def get_drive_list():
if os.name == "posix":
import commands
mount = commands.getoutput('mount -v')
lines = mount.split('\n')
return map(lambda line: line.split()[2], filter(lambda l: l.startswith("/dev"), lines))
elif os.name == "nt":
import win32api
dv = win32api.GetLogicalDriveStrings()
return dv.split('\000')[:-1]
else:
return None
def find_camera_folders(folder="DCIM"):
drives = get_drive_list()
result = []
for drive in drives:
if drive == "/" or drive == "/home":
continue
camera_folder = os.path.join(drive, folder)
if os.path.isdir(camera_folder):
result.append(camera_folder)
return result
def find_backup_folder(folder):
drives = get_drive_list()
import glob
for drive in drives:
if drive == "/" or drive == "/home":
continue
paths = glob.glob(os.path.join(drive, "*", folder))
if len(paths) == 1:
if os.path.isfile(paths[0]):
folder, file = os.path.split(paths[0])
return folder
return None
def csl2dict(csl):
return dict(item.split("=") for item in csl.split(";"))
def dict2csl(_dict):
return ";".join(["=".join([key, str(val)]) for key, val in _dict.items()])
def vigenere_encode(key, string):
encoded_chars = []
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
encoded_chars.append(encoded_c)
encoded_string = "".join(encoded_chars)
return base64.urlsafe_b64encode(encoded_string)
def vigenere_decode(key, string):
decoded_chars = []
string = base64.urlsafe_b64decode(string)
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr(abs(ord(string[i]) - ord(key_c) % 256))
decoded_chars.append(encoded_c)
decoded_string = "".join(decoded_chars)
return decoded_string
MIME_TYPES = {
"bmp": "image/bmp",
"gif": "image/gif",
"jpeg": "image/jpeg",
"thm": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"3gpp": "video/3gpp",
"3gp": "video/3gpp",
"avi": "video/avi",
"mov": "video/quicktime",
"mp4": "video/mp4",
"lrv": "video/mp4",
"mpeg": "video/mpeg",
"mpg": "video/mpeg",
"mpeg4": "video/mpeg4",
"asf": "video/x-ms-asf",
"wmv": "video/x-ms-wmv"
}
def file_name_to_mimetype(file_name):
n, e = os.path.splitext(file_name)
try:
return MIME_TYPES[e.lower().lstrip(".")]
except KeyError:
return None
def get_cipher_key(base):
return base + "-" + str(os.getegid()) + "-" + getpass.getuser()