/
exportPhoto.py
154 lines (135 loc) · 5.32 KB
/
exportPhoto.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
import Image
import os
import getopt
import mimetypes
import datetime
import logging as log
from sys import exit, argv
from shutil import copy2
from filecmp import cmp
class Item(object):
def __init__(self, basesrc, filename, basedst, prefix=""):
self.source = os.path.join(basesrc, filename)
self.destination = os.path.join(basedst, prefix + filename)
def Processing(self, itemlist):
if os.path.isfile(self.destination) and\
cmp(self.source, self.destination):
log.debug("no need to process %s" % self.source)
else:
log.info("processing %s => %s" % (self.source, self.destination))
itemlist.append(self)
class exifImage(Item):
def __init__(self, basesrc, filename, basedst):
super(exifImage, self).__init__(basesrc, filename, basedst)
self.destination = self.parseExifForDst()
def parseExifForDst(self):
try:
log.debug("image : %s" % self.source)
image = Image.open(self.source)
cdate, ctime = image._getexif()[36867].split(' ')
except KeyError, err:
log.info("KeyError : %s on %s" % (err, self.source))
cdate, ctime = ("0000:00:00", "00:00:00")
except IOError, err:
log.info("IOError : %s on %s" % (err, self.source))
exit(3)
except AttributeError, err:
log.info("AttributeError : %s on %s" % (err, self.source))
cdate, ctime = ("0000:00:00", "00:00:00")
log.debug("cdate : %s | ctime : %s" % (cdate, ctime))
year, month, day = cdate.split(':')
try:
rdate = datetime.date(int(year), int(month), int(day))
except ValueError, err:
log.debug("ValueError : %s" % err)
rdate = datetime.date(1970, 01, 01)
return os.path.join(os.path.dirname(self.destination), '%s/%s-%s' % (
rdate.strftime("%Y/%m_%B/%d_%A"),
ctime.replace(':', ''),
os.path.basename(self.source)))
def process(filelist):
for item in filelist:
if not os.path.exists(os.path.dirname(item.destination)):
log.info("creating directory %s" %
os.path.dirname(item.destination))
os.makedirs(os.path.dirname(item.destination))
log.info("copying %s => %s" % (item.source, item.destination))
copy2(item.source, item.destination)
def main():
# Ajout des types de fichier que je sui sur de rencontrer
mimetypes.add_type('video', '.mkv')
try:
opts, args = getopt.getopt(argv[1:], 's:d:vDp')
except getopt.GetoptError, err:
print str(err)
exit(2)
for o, a in opts:
if o == '-s':
imgsrc = a
elif o == '-d':
imgdst = a
elif o == '-v':
log.basicConfig(level=log.INFO)
log.info("OK")
elif o == '-D':
log.basicConfig(level=log.DEBUG)
log.debug("OK")
elif o == '-p':
shownotprocess = 1
else:
log.error("unknown option %s" % o)
if 'imgsrc' not in locals() or 'imgdst' not in locals():
log.warn('imgdirs not defined : use -s source -d dest')
exit(2)
imglist = []
videolist = []
xmplist = []
otherlist = []
for dirname, dirnames, filenames in os.walk(imgsrc):
# print path to all filenames.
for filename in filenames:
ext = filename.split('.')[-1].lower()
try:
mtype = mimetypes.types_map['.%s' % ext].split('/')[0]
except KeyError, err:
log.debug("KeyError : %s on %s" % (err, filename))
mtype = 'other'
except TypeError, err:
log.debug("TypeError : %s on %s" % (err, filename))
mtype = 'other'
if mtype == 'image':
try:
item = exifImage(dirname, filename, imgdst)
except TypeError, err:
log.debug("TypeError : %s on %s" % (err, filename))
if filename.startswith("."):
log.debug("hidden file, nevermind...")
continue
else:
log.warn("file not handle %s" % filename)
otherlist.append(item)
continue
item.Processing(imglist)
# add xmp if exists
if os.path.isfile('%s.xmp' % item.source):
prefix = os.path.basename(item.destination).split("-")[0]
xmp = Item(os.path.dirname(item.source),
'%s.xmp' % filename,
os.path.dirname(item.destination),
prefix + "-")
xmp.Processing(xmplist)
elif mtype == 'video':
item = Item(dirname, filename, os.path.join(imgdst, "video"))
item.Processing(videolist)
else:
item = Item(dirname, filename, imgdst)
otherlist.append(item)
process(imglist)
process(videolist)
process(xmplist)
if "shownotprocess" in locals():
print "file not processed"
for item in otherlist:
print item.source
if __name__ == '__main__':
main()