-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
533 lines (423 loc) · 15.5 KB
/
main.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#FotoRename:
#@author: Luca Bäck
#@date: 18.12.21
import shutil
import os
from colorama import Fore
import colorama
colorama.init()
from PIL import Image
from datetime import datetime, timedelta
from pathlib import Path
from hachoir.parser import createParser
from hachoir.metadata import extractMetadata
from PIL import ImageChops
from tkinter.filedialog import askdirectory
import sys
import calendar
#pip install defusedxml
import filecmp
import json
OriPath = r''
EndPath = r''
FileRenamed = '§'
print(Fore.GREEN + ' *********************************************************')
print(Fore.GREEN + ' Foto Programm:')
print(Fore.GREEN + ' @author: Luca Bäck')
print(Fore.GREEN + ' @date: 21.12.21')
print(Fore.GREEN + ' *********************************************************')
def read_jpg(file):
print('Read jpg-File...')
try:
"returns the image date from image (if available)"
std_fmt = '%Y:%m:%d %H:%M:%S'
# for subsecond prec, see doi.org/10.3189/2013JoG12J126 , sect. 2.2, 2.3
tags = [(36867, 37521), # (DateTimeOriginal, SubsecTimeOriginal)
(36868, 37522), # (DateTimeDigitized, SubsecTimeDigitized)
(306, 37520), ] # (DateTime, SubsecTime)
img = Image.open(open(file, 'rb'))
img.load()
exif = img._getexif()
print(Fore.WHITE + 'Got Exif Data')
for t in tags:
dat = exif.get(t[0])
# PIL.PILLOW_VERSION >= 3.0 returns a tuple
dat = dat[0] if type(dat) == tuple else dat
if dat != None: break
if dat == None: return 'No Exif Data'
full = '{}'.format(dat)
Time = datetime.strptime(full, std_fmt)
# T = time.mktime(time.strptime(dat, '%Y:%m:%d %H:%M:%S')) + float('0.%s' % sub)
Time = str(Time)
Final = Time.replace(':', '')
Final = Final.replace('-', '')
Final = Final.replace(' ', '_')
size = len(Final)
final_str = Final[:size - 2]
print(final_str)
return final_str
except:
return ('No Exif Data')
def getLastSunday(year, month):
last_sunday = max(week[-1] for week in calendar.monthcalendar(year, month))
date = datetime(year, month, last_sunday, 2, 0)
return (date)
def read_movAndmp4(file):
print('Read mov/mp4-File...')
try:
parser = createParser(file)
metadata = extractMetadata(parser)
Time = str(metadata.get('creation_date'))
date_time_obj = datetime.strptime(Time, '%Y-%m-%d %H:%M:%S')
date_time_obj = date_time_obj + timedelta(hours=1)
start = getLastSunday(date_time_obj.year, 3)
end = getLastSunday(date_time_obj.year, 10)
end = end + timedelta(hours=1)
if start <= date_time_obj <= end:
date_time_obj = date_time_obj + timedelta(hours=1)
print('1')
Time = date_time_obj.strftime("%Y-%m-%d %H:%M:%S")
Final = Time.replace(':', '')
Final = Final.replace('-', '')
Final = Final.replace(' ', '_')
size = len(Final)
final_str = Final[:size - 2]
print(final_str)
return final_str
except:
return ('No Exif Data')
def read_png(file):
print('Read png-File...')
try:
im = Image.open(file)
im.load() # Needed only for .png EXIF data (see citation above)
#print(im.info)
xmp = im.getxmp()
Time = xmp['xmpmeta']['RDF']['Description']['DateCreated']
Final = Time.replace(':', '')
Final = Final.replace('-', '')
Final = Final.replace('T', '_')
size = len(Final)
final_str = Final[:size - 2]
print(final_str)
return final_str
except:
return ('No Exif Data')
def getParentFolder():
return input(Fore.BLUE + "Herkunft: ")
def makeOutputRename(EndPath):
isOutput = os.path.isdir(os.path.join(EndPath, 'Output_Rename_1'))
counter = 1;
while(isOutput):
counter = counter+1
isOutput = os.path.isdir(os.path.join(EndPath, ('Output_Rename_' + str(counter))))
os.makedirs(os.path.join(EndPath, ('Output_Rename_' + str(counter))))
OutputPath = os.path.join(EndPath, ('Output_Rename_' + str(counter)))
os.makedirs(os.path.join(OutputPath, 'Renamed'))
os.makedirs(os.path.join(OutputPath, 'NotRenamed'))
print(OutputPath)
return OutputPath
def Rename(file, OutputPath, parentFolder, pfad):
date = ''
try:
if (file.endswith('.PNG') or file.endswith('.png')):
date = str(read_png(pfad))
elif (file.endswith('.MOV') or file.endswith('.mov')):
date = str(read_movAndmp4(pfad))
elif (file.endswith('.mp4') or file.endswith('.MP4')):
date = str(read_movAndmp4(pfad))
elif (file.endswith('.jpg') or file.endswith('.JPG')):
date = str(read_jpg(pfad))
except:
pass
if ('No Exif Data' in date or date == ''):
print(Fore.WHITE + 'File: ' + file + Fore.RED + ' New Name: ' + date)
shutil.copy2(pfad, os.path.join(OutputPath, 'NotRenamed'))
NewFileDest = os.path.join(OutputPath, 'NotRenamed')
NewFileDest = os.path.join(NewFileDest, file)
print(Fore.WHITE)
else:
indexName = file.rindex('.')
sub = len(file) - indexName
filename = file[:-sub]
date = date[2:]
newName = date + '_' + parentFolder + '_' + FileRenamed + '_' + filename + file.replace(filename, '')
try:
newName = newName.replace(' ', '')
newName = newName.replace(':', '-')
except:
pass
print(newName)
print(Fore.WHITE + 'File: ' + file + Fore.GREEN + ' New Name: ' + date)
shutil.copy2(pfad, os.path.join(OutputPath, 'Renamed'))
NewFileDest = os.path.join(OutputPath, 'Renamed')
NewFileDest = os.path.join(NewFileDest, file)
NewFileName = os.path.join(OutputPath, 'Renamed')
NewFileName = os.path.join(NewFileName, newName)
os.rename(NewFileDest, NewFileName)
print(Fore.WHITE)
def RunRenameFile(fileList):
if not(fileList.endswith('.txt')):
print(Fore.RED + 'Falsches Dateiformat')
start()
if not(Path(fileList).is_file()):
print(Fore.RED + 'Liste existiert nicht')
start()
EndPath = input('Wählen sie das Zielverzeichnis: ') # shows dialog box and return the path
OutputPath = makeOutputRename(EndPath)
print(Fore.WHITE)
parentFolder = getParentFolder()
with open(fileList, "r") as a_file:
for line in a_file:
stripped_line = line.strip()
pfad = stripped_line
try:
indexName = pfad.rindex(chr(47))
except:
pass
try:
indexName = pfad.rindex(chr(92))
except:
pass
sub = len(pfad) - indexName-1
onlypath = pfad[:-sub]
file = pfad.replace(onlypath, '')
print(file)
print(Fore.WHITE + "")
Rename(file, OutputPath, parentFolder, pfad)
def RunRename():
OriPath = input('Wählen sie das Ursprungsverzeichnis: ') # shows dialog box and return the path
EndPath = input('Wählen sie das Zielverzeichnis: ') # shows dialog box and return the path
print(OriPath)
print(EndPath)
OutputPath = makeOutputRename(EndPath)
print(Fore.WHITE)
parentFolder = getParentFolder()
for root, dirs, files in os.walk(OriPath):
for file in files:
pfad = os.path.join(root, file)
print(Fore.WHITE + "")
Rename(file, OutputPath, parentFolder, pfad)
break
print(Fore.WHITE)
def help():
print(Fore.GREEN + '')
print(Fore.GREEN + 'fotos -v')
print(Fore.GREEN + '-v Auswahl zweier Verzeichnisse zum Vergleich. Ergebnis sind 5 Vergleichslisten in Form von txt-Dateien')
print(Fore.GREEN + '')
print(Fore.GREEN + 'fotos -r [<Pfad>]')
print(Fore.GREEN + '-r Ersetze Dateinamen mit <YYMMTT_HHMM_Origin_§_Originalfilename.Filetype>')
print(Fore.GREEN + ' Zeiten immer an Sommerzeit und Winterzeit angepasst')
print(Fore.GREEN + '<Pfad> Pfad zu einer txt-Datei mit Pfaden zu den Dateien, die verarbeitet werden sollen')
print(Fore.GREEN + '')
print(Fore.GREEN + 'fotos -c <Pfad>')
print(Fore.GREEN + '-c Kopiere Dateien in ausgewähltes Zielverzeichnis')
print(Fore.GREEN + '<Pfad> Pfad zu einer txt-Datei mit Pfaden zu den Dateien, die verarbeitet werden sollen')
print(Fore.GREEN + '')
print(Fore.GREEN + 'fotos -d <Pfad>')
print(Fore.GREEN + '-d Lösche Dateien')
print(Fore.GREEN + '<Pfad> Pfad zu einer txt-Datei mit Pfaden zu den Dateien, die verarbeitet werden sollen')
print(Fore.GREEN + '')
start()
def makeOutputVergleich(ZielPath):
isOutput = os.path.isdir(os.path.join(ZielPath, 'Output_Vergleich_1'))
counter = 1;
while(isOutput):
counter = counter+1
isOutput = os.path.isdir(os.path.join(ZielPath, ('Output_Vergleich_' + str(counter))))
os.makedirs(os.path.join(ZielPath, ('Output_Vergleich_' + str(counter))))
OutputPath = os.path.join(ZielPath, ('Output_Vergleich_' + str(counter)))
print(OutputPath)
return OutputPath
def Vergleich():
ZielPath = r''
verz1 = input('Wählen Sie das erste Verzeichnis: ') # shows dialog box and return the path
verz2 = input('Wählen Sie das zweite Verzeichnis: ') # shows dialog box and return the path
ZielPath = input('Wählen Sie, wo das Ergebnis gespeichert werden soll: ') # shows dialog box and return the path
if(verz1 == verz2):
print(Fore.RED + 'Die Verzeichnisse dürfen nicht identisch sein')
start()
unique1 = []
unique2 = []
same = []
same1 = []
same2 = []
hide = []
print("Verz1: " + verz1)
print("Verz2: " + verz2)
print("Ziel: " + ZielPath)
for root1, dirs1, files1 in os.walk(verz1):
for file1 in files1:
pfad1 = os.path.join(root1, file1)
for root2, dirs2, files2 in os.walk(verz2):
for file2 in files2:
pfad2 = os.path.join(root2, file2)
if (file1.startswith('._') or file2.startswith('._')):
continue
if filecmp.cmp(pfad1, pfad2):
img = []
img.append(pfad1)
img.append(pfad2)
same.append(img)
break # auch bei Vergleich?
break # auch bei Vergleich?
for root1, dirs1, files1 in os.walk(verz1):
for file1 in files1:
if (file1.startswith('._')):
pfad1 = os.path.join(root1, file1)
hide.append(pfad1)
continue
pfad1 = os.path.join(root1, file1)
pfad1 = pfad1.replace(chr(92), '/')
b = False
for a in same:
for r in a:
f = r.replace(chr(92), '/')
if (pfad1 == f):
b = True
if not b:
unique1.append(pfad1)
break #auch bei Vergleich?
for root2, dirs2, files2 in os.walk(verz2):
for file2 in files2:
if (file2.startswith('._')):
pfad2 = os.path.join(root2, file2)
hide.append(pfad2)
continue
pfad2 = os.path.join(root2, file2)
pfad2 = pfad2.replace(chr(92), '/')
b = False
for a in same:
for r in a:
f = r.replace(chr(92), '/')
if (pfad2 == f):
b = True
if not b:
unique2.append(pfad2)
break # auch bei Vergleich?
for ar in same:
same1.append(ar[0])
same2.append(ar[1])
try:
indexName = verz1.rindex('/')
except:
pass
try:
indexName = verz1.rindex(chr(92))
except:
pass
sub = len(verz1) - indexName-1
name1 = verz1[:-sub]
name1 = verz1.replace(name1, '')
try:
indexName = verz2.rindex('/')
except:
pass
try:
indexName = verz2.rindex(chr(92))
except:
pass
sub = len(verz2) - indexName-1
name2 = verz2[:-sub]
name2 = verz2.replace(name2, '')
OutputPath = makeOutputVergleich(ZielPath)
print("name1: " + name1)
print("name2: " + name2)
myfile1 = Path(OutputPath+'/Einzigartig1_'+name1+'.txt')
myfile2 = Path(OutputPath+'/Einzigartig2_'+name2+'.txt')
print("Anzahl Dateien in Same: " + str(len(same)))
print("Anzahl Dateien nur in Verzeichnis1: " + str(len(unique1)))
print("Anzahl Dateien nur in Verzeichnis2: " + str(len(unique2)))
print("Anzahl versteckter Dateien in beiden Verzeichnissen: " + str(len(hide)))
f1 = open(myfile1, 'w+')
f2 = open(myfile2, 'w+')
for l in unique1:
f1.write(l)
f1.write("\n")
f1.close()
for l in unique2:
f2.write(l)
f2.write("\n")
f2.close()
myfile3 = Path(OutputPath+'/Überlappung_'+ name1 + '_' + name2+'.txt')
f3 = open(myfile3, 'w+')
for l in same:
for s in l:
f3.write(s)
f3.write(" \n")
f3.write(" \n")
f3.close()
myfile4 = Path(OutputPath + '/Überlappung1_' + name1 + '.txt')
f4 = open(myfile4, 'w+')
for l in same1:
f4.write(l)
f4.write(" \n")
f4.close()
myfile5 = Path(OutputPath + '/Überlappung2_' + name2 + '.txt')
f5 = open(myfile5, 'w+')
for l in same2:
f5.write(l)
f5.write(" \n")
f5.close()
myfile6 = Path(OutputPath + '/HiddenResourceFork' + '.txt')
f6 = open(myfile6, 'w+')
for l in hide:
f6.write(l)
f6.write(" \n")
f6.close()
print('Vergleich beendet')
start()
def Copy(fileList):
if not(fileList.endswith('.txt')):
print(Fore.RED + 'Falsches Dateiformat')
start()
if not(Path(fileList).is_file()):
print(Fore.RED + 'Liste existiert nicht')
start()
ZielPath = input('Wählen sie das Zielverzeichnis: ') # shows dialog box and return the path
with open(fileList, "r") as a_file:
for line in a_file:
stripped_line = line.strip()
if(os.path.isfile(stripped_line)):
shutil.copy2(stripped_line, ZielPath)
start()
def Delete(fileList):
if not(fileList.endswith('.txt')):
print(Fore.RED + 'Falsches Dateiformat')
start()
if not(Path(fileList).is_file()):
print(Fore.RED + 'Liste existiert nicht')
start()
with open(fileList, "r") as a_file:
for line in a_file:
stripped_line = line.strip()
if(os.path.isfile(stripped_line)):
os.remove(stripped_line)
start()
def start():
print(Fore.WHITE + 'Geben Sie ihren Befehl ein... (fotos -h für hilfe)')
command = input()
command = command.strip()
if not("fotos " in command):
start()
else:
command = command.replace("fotos ", '')
if('-h' in command):
help()
elif('-r' in command):
if('-r ' in command):
command = command.replace("-r ", '')
RunRenameFile(command)
else:
RunRename()
elif('-v' in command):
command = command.replace("-v", '')
Vergleich()
elif('-c ' in command):
command = command.replace("-c ", '')
Copy(command)
elif('-d ' in command):
command = command.replace("-d ", '')
Delete(command)
start()
start()