-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.py
213 lines (194 loc) · 9.59 KB
/
go.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import shutil
import os.path
import filecmp
import stdio
import exifread
import uuid
import re
import datetime
import sys
from dateutil.parser import parse
def exifdate_date(exif_date):
dum = str(exif_date).split()
return dum[0].replace(':', '-')
def ensure_dir(tp):
if not os.path.exists(tp):
os.makedirs(tp)
def run_(target_path, source_path):
xl = get_files(source_path)
source_files_cnt = len(xl)
SHOW_ERRORS = True
SHOW_OUTPUT = True
SHOW_COUNTER = True
ensure_dir(target_path + 'no_exif_dump/')
print(' files:', "{0:6d}".format(source_files_cnt), file=open(target_path + "copy.log", "a"))
print('------------------------------------------------', file=open(target_path + "copy.log", "a"))
ensure_dir(target_path + 'no_exif_dump/')
print(' files:', "{0:6d}".format(source_files_cnt))
print('------------------------------------------------')
copy_file_cnt = 0
error_1_cnt = 0
error_2_cnt = 0
error_3_cnt = 0
error_4_cnt = 0
for n in xl:
print('process file:', n)
f = open(n, 'rb')
tags = exifread.process_file(f)
nome = str(n)[str(n).rfind('/') + 1:]
# print('file', n, nome)
try:
exif_dir = exifdate_date(tags['EXIF DateTimeOriginal'])
t_dir = target_path + exif_dir + '/'
ensure_dir(t_dir)
t_file = nome
if stdio.file_ok(t_dir + t_file):
# print('file exist', n, ' - ', nome)
if not filecmp.cmp(n, t_dir + t_file):
print('------------------------------------------------', file=open(target_path + "error.log", "a"))
print(' Ficheiro com EXIF duplicado mas diferente ', file=open(target_path + "error.log", "a"))
print(' nome:', t_file, file=open(target_path + "error.log", "a"))
print(' origem:', n, file=open(target_path + "error.log", "a"))
print(' para:', t_dir, file=open(target_path + "error.log", "a"))
print(' PC:', copy_file_cnt, file=open(target_path + "error.log", "a"))
print(' corregido:', t_file, file=open(target_path + "error.log", "a"))
print('------------------------------------------------', file=open(target_path + "error.log", "a"))
t_file = clean_file_name(t_file, ui=True)
shutil.copy2(n, t_dir + t_file)
copy_file_cnt += 1
error_1_cnt += 1
if SHOW_COUNTER:
print(' cópia:' + "{0:6d}".format(copy_file_cnt) + "{0:6d}".format(error_1_cnt)
+ "{0:6d}".format(error_2_cnt)
+ "{0:6d}".format(error_3_cnt)
+ "{0:6d}".format(error_4_cnt)
, sep=' ', end='\r', flush=True)
else:
print('------------------------------------------------', file=open(target_path + "error.log", "a"))
print(' Ficheiro com EXIF duplicado mas igual ', file=open(target_path + "error.log", "a"))
print(' nome:', t_file, file=open(target_path + "error.log", "a"))
print(' origem:', n, file=open(target_path + "error.log", "a"))
print(' para:', t_dir, file=open(target_path + "error.log", "a"))
print(' PC:', copy_file_cnt, file=open(target_path + "error.log", "a"))
print(' não corregido:', t_file, file=open(target_path + "error.log", "a"))
print('------------------------------------------------', file=open(target_path + "error.log", "a"))
error_4_cnt += 1
else:
# if SHOW_OUTPUT:
# print(' cópia:', 'O.K. ', '[' + exif_dir + ']', t_file)
print(' cópia:', 'O.K. ', '[' + exif_dir + ']', t_file, file=open(target_path + "copy.log", "a"))
shutil.copy2(n, t_dir + t_file)
copy_file_cnt += 1
if SHOW_COUNTER:
print(' cópia:' + "{0:6d}".format(copy_file_cnt) + "{0:6d}".format(error_1_cnt)
+ "{0:6d}".format(error_2_cnt)
+ "{0:6d}".format(error_3_cnt)
+ "{0:6d}".format(error_4_cnt)
, sep=' ', end='\r', flush=True)
except KeyError:
# f_date = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(n)))
f_date = get_date_from_filename(n)
exif_dir = target_path + f_date + '/'
# não tem exifread vai para exif_dir)
ensure_dir(exif_dir)
if stdio.file_ok(exif_dir + nome):
print('------------------------------------------------', file=open(target_path + "error.log", "a"))
print(' Ficheiro sem EXIF e duplicado ', file=open(target_path + "error.log", "a"))
print(' nome:', nome, file=open(target_path + "error.log", "a"))
print(' origem:', n, file=open(target_path + "error.log", "a"))
print(' PC:', copy_file_cnt, file=open(target_path + "error.log", "a"))
print('copy 2 no_exif:', 'no_exif_dump', file=open(target_path + "error.log", "a"))
print('------------------------------------------------', file=open(target_path + "error.log", "a"))
shutil.copy2(n, target_path + 'no_exif_dump/' + nome)
error_2_cnt += 1
if SHOW_COUNTER:
print(' cópia:' + "{0:6d}".format(copy_file_cnt) + "{0:6d}".format(error_1_cnt)
+ "{0:6d}".format(error_2_cnt)
+ "{0:6d}".format(error_3_cnt)
+ "{0:6d}".format(error_4_cnt)
, sep=' ', end='\r', flush=True)
else:
print(' cópia:', 'sem EXIF ', '[' + f_date + ']', nome)
shutil.copy2(n, exif_dir + nome)
print(' cópia:', 'sem EXIF ', '[' + f_date + ']', nome, file=open(target_path + "copy.log", "a"))
print(' cópia:', 'sem EXIF ', '[' + f_date + ']', nome, file=open(target_path + "error.log", "a"))
copy_file_cnt += 1
error_3_cnt += 1
# if SHOW_OUTPUT:
# print(' cópia:', 'sem EXIF ', '[' + f_date + ']', nome)
if SHOW_COUNTER:
print(' cópia:' + "{0:6d}".format(copy_file_cnt) + "{0:6d}".format(error_1_cnt)
+ "{0:6d}".format(error_2_cnt)
+ "{0:6d}".format(error_3_cnt)
+ "{0:6d}".format(error_4_cnt)
, sep=' ', end='\r', flush=True)
print(file=open(target_path + "copy.log", "a"))
print('------------------------------------------------', file=open(target_path + "copy.log", "a"))
print('source:', "{0:6d}".format(source_files_cnt), file=open(target_path + "copy.log", "a"))
print(' copy:', "{0:6d}".format(copy_file_cnt), file=open(target_path + "copy.log", "a"))
print()
print('------------------------------------------------')
print('source:', "{0:6d}".format(source_files_cnt))
print(' copy:', "{0:6d}".format(copy_file_cnt))
print('error1:', "{0:6d}".format(error_1_cnt))
print('error2:', "{0:6d}".format(error_2_cnt))
print('error3:', "{0:6d}".format(error_3_cnt))
print('error4:', "{0:6d}".format(error_4_cnt))
def clean_file_name(a, ui=False):
c = re.sub('[() ]', '_', a)
if ui:
b = str(uuid.uuid4())[9:13].upper()
c = c.replace('.', '_' + b + '.')
c = c.replace('__', '_')
return c
def get_date_from_filename(a):
xl = os.path.basename(a)
# print('input:', xl)
b = xl.replace(' ', '_')
b = b.replace('T', '_')
c = b.split('_')
for n in c:
d = n.replace('-', '')
# print(d, end='\n')
if is_date(d):
# print('date found...', d, '...')
return d[:4] + '-' + d[4:6] + '-' + d[6:8]
try:
return time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(a)))
except:
return 'no_exif_dump'
def is_date(string):
try:
parse(string)
return True
except ValueError:
return False
def main():
print('Organize photos 3.0 ©Jorge Espiridião')
source_path = '/media/zorze/CA4C01E74C01CEDF/' # 'c:/tmp/'
target_path = '/vmware/sandra/' # 'c:/target/'
print(datetime.datetime.now().strftime('%d.%b.%Y %H:%M') + '\n', source_path,
file=open(target_path + "copy.log", "a"))
print(' from:', source_path, file=open(target_path + "copy.log", "a"))
print(' to:', target_path, file=open(target_path + "copy.log", "a"))
print(' from:', source_path)
print(' to:', target_path)
t0 = time.time()
run_(target_path, source_path)
t1 = time.time()
print(' time:', t1 - t0)
print(' time:', t1 - t0, file=open(target_path + "copy.log", "a"))
if __name__ == '__main__':
main()
# xl = ['2017-06-03 22.30.32.jpg','20171112_132509.jpg', 'Airborne_Cargo_2016-08-25T192147.jpg',
# 'IMG_20150801_215449.jpg','20171112_135221.mp4', 'VID_20151219_173438.mp4',
# '20160810_155058.jpg']
# for n in xl:
# print(n)
# get_date_from_filename(n)
# print('----------')
# todo tirar data pelo nome do ficheiro
# atenção á duplicação do rawtherapy