-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordenar-tobo.py
148 lines (130 loc) · 3.53 KB
/
ordenar-tobo.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
### ordenar archivos en tobo
import uuid
import glob
import os
from exif import Image
from pprint import pprint
from tqdm import tqdm
import shutil
import sys
import time
<<<<<<< HEAD
import traceback
print("inicio")
__SOURCE__ = '../tobo-local'
# __SOURCE__ = '../tobo-local-duplicates'
__DESTINATION__ = '../tobo-ordenado'
srcdir = os.path.join(__SOURCE__, '**', '*.jpg')
# print(srcdir)
files = glob.glob(srcdir, recursive=True)
print(f"{len(files)} loaded")
# sys.exit()
# print(len(files))
=======
import traceback as tb
print("Ordenando el tobo de fotos...")
# source
srcdir = os.path.join('..', 'tobo', '**', '*.jpg')
# # cambio para transferir 2019
# srcdir = os.path.join('..', 'tobo-ordenado-2019-no-transferido','**', '*.jpg')
# destination
DESTINATION = 'tobo-ordenado-2019-no-transferido'
DESTINATION = 'tobo-ordenado'
print(f'{srcdir=}')
files = glob.glob(srcdir, recursive=True)
print(f'{len(files)=}')
print(files[1:10])
>>>>>>> 1a0289011a19c37f3bcfe4880a6653a66776ba59
# sys.exit()
c=0
for file in tqdm(files):
try:
c+=1
src = file
basename = os.path.basename(src)
basedir = os.path.dirname(src)
fsize = os.path.getsize(src)
if fsize == 0:
zerodir = os.path.join('..', DESTINATION, 'size-zero')
if not os.path.exists(zerodir):
os.mkdir(zerodir)
dst = os.path.join(zerodir, basename)
# print(f"{src}->{dst}")
shutil.move(src, dst)
continue
image = Image(open(file,'rb'))
# pprint(dir(image))
# print(image)
try:
datetime = image.get("datetime")
if not datetime:
print('no date time')
print(src)
continue
# break
except KeyError:
# tb.print_exc()
tqdm.write(f'KeyError getting timestamp from file {file} ... skipping')
continue
# datetime = image.datetime
datestr = datetime.split(" ")[0]
timestr = datetime.split(" ")[1]
year,month,day = datestr.split(":")
hour,minutes,seconds = timestr.split(":")
<<<<<<< HEAD
basedir = __DESTINATION__
=======
>>>>>>> 1a0289011a19c37f3bcfe4880a6653a66776ba59
dirname = f"{year}-{month}-{day}"
minutes = '00' if int(minutes) < 30 else '30'
subdirname = f"n{year[-2:]}{month}{day}_{hour}{minutes}"
<<<<<<< HEAD
dstdir = os.path.join(__DESTINATION__, dirname, subdirname)
=======
dstdir = os.path.join('..', DESTINATION, dirname, subdirname)
dstfilename = os.path.join(dstdir, basename)
# tqdm.write(src)
>>>>>>> 1a0289011a19c37f3bcfe4880a6653a66776ba59
# tqdm.write(dstdir)
# tqdm.write(dstfilename)
# sys.exit()
if not os.path.exists(dstdir):
os.makedirs(dstdir)
try:
<<<<<<< HEAD
# tqdm.write(f"{src}->{dstdir}")
destinations = [
os.path.join(dstdir, basename),
# os.path.join(__SOURCE__, basename),
# os.path.join(__SOURCE__, basename) + f'.{uuid.uuid1()}.jpg',
]
for destination in destinations:
if not os.path.exists(destination):
# shutil.move(src, destination)
tqdm.write(f'gotta be moved to {destination}')
sys.exit()
break
else:
pass
# tqdm.write('. ', end='')
# print(f'File already exists in destination. Not doing anything. {src}')
# sys.exit()
=======
if not os.path.exists(dstfilename):
# tqdm.write(f"{src}->{dstdir}")
shutil.move(src, dstfilename)
# sys.exit()
else:
print(f'File {dstfilename} already exists. Not moving {src}')
>>>>>>> 1a0289011a19c37f3bcfe4880a6653a66776ba59
except Exception as error:
if "already exists" in str(error):
print(f"Ignoring error: {error}")
else:
traceback.print_exc()
a=1
# print(error)
except Exception as err:
tqdm.write(file)
tqdm.write(err)
raise(err)