-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediaCls.py
More file actions
186 lines (165 loc) · 5.9 KB
/
mediaCls.py
File metadata and controls
186 lines (165 loc) · 5.9 KB
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
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import os
import win32com.client as wclient
from urllib import request as urlRequest
import fileLocal
from folderCls import Folder
import loggerFct as log
""" classe et fonctions communes pour les médias, imageCls et videoCls """
dateFormatDay = '%Y-%m-%d'
dateFormatHour = dateFormatDay + '-%H-%M'
metadataWindow =[
'Name', 'Size', 'Item type', 'Date modified', 'Date created', 'Date accessed', 'Attributes', 'Offline status', 'Availability', 'Perceived type',
'Owner', 'Kind', 'Date taken', 'Contributing artists', 'Album', 'Year', 'Genre', 'Conductors', 'Tags', 'Rating',
'Authors', 'Title', 'Subject', 'Categories', 'Comments', 'Copyright', '#', 'Length', 'Bit rate', 'Protected',
'Camera model', 'Dimensions', 'Camera maker', 'Company', 'File description', 'Masters keywords'
]
alpabet = 'abcdefghijklmnopqrstuvwxyz'
extensions =[ 'jpg', 'bmp', 'gif', 'png', 'mp4', 'avi', 'mov', 'heic', 'heif' ]
def mediaAtraiter (imgTitle):
imgTitle = imgTitle.lower()
if imgTitle[:4] in ('img_', 'img-', 'vid_') or imgTitle[:6] == 'video_': return True
else:
l=0
while l<26:
if alpabet[l] in imgTitle: l=27
l+=1
if l==28: return False
else: return True
def getNameSpace (pathWindow):
if pathWindow[-1] == os.sep: pathWindow = pathWindow[:-1]
shellApp = wclient.gencache.EnsureDispatch ('Shell.Application',0)
nameSpace = shellApp.NameSpace (pathWindow)
return nameSpace
def getDateCreation (nameImg, nameSpace, addHour=False):
fileData = nameSpace.ParseName (nameImg)
# repérer la date de création. Date taken ou Date created
dateCreation = nameSpace.GetDetailsOf (fileData, 12).replace (" ","")
if dateCreation and '202' in dateCreation:
dateCreation = dateCreation.replace ('', "")
dateCreation = dateCreation.replace ('', '/')
else: dateCreation = nameSpace.GetDetailsOf (fileData, 4).replace (" ",'/')
# mettre en forme la date de création
dateCreation = dateCreation.replace (':', '-')
dateList = dateCreation.split ('/')
dateCreation = dateList[2] +'-'+ dateList[1] +'-'+ dateList[0]
if addHour: dateCreation = dateCreation +'-'+ dateList[3]
return dateCreation
def createDatedName (pathImg, extImg, dateCreation):
newName =""
l=0
while l<26:
newName = pathImg + dateCreation +" "+ alpabet[l] +'.'+ extImg
if not os.path.exists (newName): l=27
l+=1
if l<27:
newName =""
print ("je n'ai pas réussi à renommer l'image:", pathImg)
return newName
def fromWeb (imgUrl, imgFile):
try: urlRequest.urlretrieve (imgUrl, imgFile)
except Exception as e:
print (e)
return False
else: return True
class MediaFile():
def __init__ (self, filePath=""):
self.path =""
self.title =""
self.extension =""
self.pathRoot =""
if filePath:
self.path = filePath
self.fromPath()
def renameDate (self, nameSpace, addHour=False):
nameCreation = self.renameDateEtape1 (nameSpace, addHour);
if nameCreation: os.rename (self.path, nameCreation)
def renameDateEtape1 (self, nameSpace, addHour=False):
""" renommer un fichier en prenant en compte la date
la fonction utilise les métadonnées de window
"""
if mediaAtraiter (self.title):
self.toPath()
dateCreation = getDateCreation (self.title +'.'+ self.extension, nameSpace, addHour)
nameCreation = createDatedName (self.pathRoot, self.extension, dateCreation)
if nameCreation:
self.open()
return nameCreation
else: return None
else: return None
def draw (self):
self.toPath()
chars = '/\\\t\n'; c=0
while chars != 'error' and c<4:
if chars[c] in self.title:
print ('le fichier est mal formé:', self.title [:100])
print (c, chars[c])
chars = 'error'
c+=1
if chars == 'error': return False
else:
self.toPath()
return True
def remove (self):
self.toPath()
if os.path.exists (self.path): os.remove (self.path)
def fromPath (self):
if self.pathRoot: return
self.path = fileLocal.shortcut (self.path)
if os.sep not in self.path or '.' not in self.path:
# print ('fichier malformé:\n' + self.path)
return
elif self.path.rfind (os.sep) > self.path.rfind ('.'):
# print ('fichier malformé:\n' + self.path)
return
posS = self.path.rfind (os.sep) +1
posE = self.path.rfind ('.')
self.title = self.path [posS:posE]
self.extension = self.path[posE+1:]
self.pathRoot = self.path [:posS]
def toPath (self):
self.path = self.pathRoot + self.title +'.'+ self.extension
def __lt__ (self, newFile):
""" nécessaire pour trier les listes """
self.toPath()
newFile.toPath()
return self.path < newFile.path
def __str__ (self):
self.toPath()
return self.path
class MediaFolder (Folder):
def __init__ (self, path='i/'):
Folder.__init__(self, path)
def renameDate (self, addHour=False):
nameSpace = getNameSpace (self.path[:-1])
self.get ('new')
for image in self.list:
if os.sep not in image.path: image.path = self.path + image.path
image.renameDate (nameSpace, addHour)
# image.path = image.path.replace (self.path, "")
def get (self, detail=""):
self.fromPath()
for dirpath, SousListDossiers, subList in os.walk (self.path):
if not subList: continue
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if subList[i][-3:] not in extensions or subList[i][-4] !='.': trash = subList.pop(i)
if detail == 'new':
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if not mediaAtraiter (subList[i][:-4]): trash = subList.pop(i)
elif detail:
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if detail not in subList[i]: trash = subList.pop(i)
if subList:
for image in subList:
# fileTmp = MediaFile (os.path.join (dirpath, image))
self.list.append (os.path.join (dirpath, image))
self.list.sort()
def __str__ (self):
strText = 'liste à %d éléments dans le dossier %s' %( len (self.list), self.path)
self.fromPath()
for image in self.list[:6]: strText = strText + '\n\t' + str (image).replace (self.path, "")
return strText