| author | wakimd |
| Fri, 15 Oct 2010 12:36:43 +0200 | |
| changeset 94 | 9927a619d2b5 |
| parent 62 | 39b2dab4f939 |
| child 89 | 30c6e597a7de |
| permissions | -rw-r--r-- |
| 32 | 1 |
from copy import deepcopy |
2 |
from django.conf import settings |
|
| 0 | 3 |
from django.core.exceptions import ObjectDoesNotExist |
4 |
from ldt.utils import zipfileext |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
5 |
from models import Content, Media |
| 0 | 6 |
import fnmatch |
| 32 | 7 |
import lxml.etree |
8 |
import os.path |
|
9 |
import shutil |
|
10 |
import tempfile |
|
11 |
import urllib |
|
| 0 | 12 |
import uuid |
13 |
||
14 |
class FileImportError(Exception): |
|
15 |
def __init__(self, value): |
|
16 |
self.value = value |
|
17 |
def __str__(self): |
|
18 |
return repr(self.value) |
|
19 |
||
20 |
||
21 |
def Property(func): |
|
22 |
return property(**func()) |
|
23 |
||
24 |
class IriInfo(object): |
|
25 |
||
26 |
||
27 |
def __init__(self, id, order, titledesc, basepath="", videopath=settings.STREAM_URL, decoupage_blacklist = settings.DECOUPAGE_BLACKLIST, flatten = True): |
|
28 |
self.id = id |
|
29 |
self.basepath = basepath |
|
30 |
self.order = order |
|
31 |
self.src = "" |
|
32 |
self.annotations = None |
|
33 |
self.videopath = videopath |
|
34 |
self.videourl = "" |
|
35 |
self.title = None |
|
36 |
self.desc = None |
|
37 |
self.duration = None |
|
38 |
self.created = False |
|
39 |
self.content = None |
|
40 |
self.decoupage_blacklist = decoupage_blacklist |
|
41 |
if self.decoupage_blacklist is None: |
|
42 |
self.decoupage_blacklist = () |
|
43 |
self.flatten = flatten |
|
44 |
||
45 |
||
46 |
||
47 |
def processIri(self): |
|
48 |
# for just import a file ldt and get the title for every media |
|
49 |
if 'http' in self.src: |
|
| 32 | 50 |
#url = urllib.urlopen(self.src) |
51 |
path = url |
|
52 |
#doc = xml.dom.minidom.parse(url) |
|
| 0 | 53 |
#for import a zip, get title and copy file .iri in the media directory |
54 |
else: |
|
55 |
path = os.path.join(self.basepath, self.src) |
|
| 32 | 56 |
#doc = xml.dom.minidom.parse(path) |
57 |
||
58 |
doc = lxml.etree.parse(path) |
|
| 5 | 59 |
|
60 |
||
| 32 | 61 |
#doc = Ft.Xml.Domlette.ConvertDocument(doc) |
62 |
#con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
| 5 | 63 |
|
| 32 | 64 |
res = doc.xpath("/iri/head/meta[@name='title']/@content") |
65 |
#res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con) |
|
66 |
#self.title = res[0].value |
|
67 |
self.title = res[0] |
|
| 5 | 68 |
|
| 0 | 69 |
|
| 32 | 70 |
#res = xml.xpath.Evaluate("/iri/body/ensembles",context=con) |
71 |
res = doc.xpath("/iri/body/ensembles") |
|
| 0 | 72 |
ensemblesnode = res[0] |
73 |
||
74 |
ensembleids = [] |
|
75 |
||
| 32 | 76 |
for node in ensemblesnode: #ensemblesnode.childNodes: |
77 |
#if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble": |
|
78 |
if node.tag == "ensemble": |
|
79 |
#id = node.getAttributeNS(None,u"id") |
|
80 |
id = node.attrib["id"] |
|
| 0 | 81 |
if id not in ensembleids: |
82 |
ensembleids.append(id) |
|
83 |
||
84 |
if self.annotations is not None: |
|
85 |
newEnsemble = None |
|
| 32 | 86 |
#for cnode in self.annotations.childNodes: |
87 |
for cnode in self.annotations: |
|
88 |
#if cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "decoupage": |
|
89 |
if cnode.tag == "decoupage": |
|
| 0 | 90 |
if newEnsemble is None: |
| 32 | 91 |
#newensemble = doc.createElementNS(None,'ensemble') |
| 0 | 92 |
ensembleid = self.id+"_"+str(uuid.uuid1()) |
| 32 | 93 |
newensemble = lxml.etree.SubElement(ensemblesnode, |
94 |
'ensemble', |
|
95 |
{'id' : ensembleid, |
|
96 |
'title' : self.annotations.get('title') or "", |
|
97 |
'author' : self.annotations.get('author') or "", |
|
98 |
'date' : self.annotations.get('date') or "", |
|
99 |
'abstract' : self.annotations.get('abstract') or "" |
|
100 |
} |
|
101 |
) |
|
| 0 | 102 |
ensembleids.append(ensembleid) |
| 32 | 103 |
newDecoupageNode = deepcopy(cnode) |
104 |
newensemble.append(newDecoupageNode) |
|
105 |
#elif cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "ensemble": |
|
106 |
elif cnode.tag == "ensemble": |
|
107 |
#ensembleid = cnode.getAttribute(u"id") |
|
108 |
ensembleid = cnode.attrib['id'] |
|
109 |
cloneNode = deepcopy(cnode) |
|
| 0 | 110 |
if ensembleid in ensembleids: |
111 |
ensembleid = self.id+"_"+str(uuid.uuid1()) |
|
| 32 | 112 |
cloneNode.set(u"id", ensembleid) |
| 0 | 113 |
ensembleids.append(ensembleid) |
| 32 | 114 |
ensemblesnode.append(cloneNode) |
| 0 | 115 |
|
| 32 | 116 |
res = doc.xpath("/iri/body/medias/media[@id='video']/video") |
| 0 | 117 |
if self.flatten: |
| 32 | 118 |
src_video = res[0].get('src') |
| 0 | 119 |
self.videourl = os.path.basename(src_video) |
| 32 | 120 |
res[0].set('src', self.videourl) |
121 |
self.duration = res[0].get(u'dur') |
|
| 0 | 122 |
|
123 |
f = open(path, "w") |
|
124 |
try: |
|
| 32 | 125 |
#etree = lxml.etree.ElementTree(doc) |
126 |
doc.write(f, encoding="UTF-8", pretty_print=True, xml_declaration=True) |
|
127 |
# xml.dom.ext.Print(doc, stream=f) |
|
| 0 | 128 |
finally: |
129 |
f.close() |
|
130 |
||
131 |
||
132 |
destPath = os.path.join(os.path.join(os.path.join(settings.MEDIA_ROOT, "media"), "ldt"), self.id); |
|
133 |
if not os.path.exists(destPath): |
|
134 |
os.makedirs(destPath) |
|
135 |
shutil.move(os.path.join(self.basepath, self.src), os.path.join(destPath, os.path.basename(self.src))) |
|
136 |
self.src = self.id + u"/" + os.path.basename(self.src) |
|
137 |
||
138 |
||
139 |
||
140 |
def saveContent(self): |
|
141 |
#if 'http' in self.src: |
|
142 |
# url = self.src |
|
143 |
#else: |
|
144 |
# url = self.id + u"/" + os.path.basename(self.src) |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
145 |
defaults_media = {'src':unicode(self.videourl), 'title':unicode(self.title), 'description':unicode(self.desc), 'videopath': unicode(self.videopath.rstrip("/") + "/")} |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
146 |
media, media_created = Media.objects.get_or_create(src=unicode(self.videourl), defaults = defaults_media) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
147 |
if not media_created: |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
148 |
for key, value in defaults_media.items(): |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
149 |
setattr(media, key, value) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
150 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
151 |
media.save() |
| 0 | 152 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
153 |
defaults_content = { |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
154 |
'iriurl': unicode(self.src), |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
155 |
'title':unicode(self.title), |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
156 |
'description':unicode(self.desc), |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
157 |
'media':media, |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
158 |
'iri':unicode(self.id + u"/" + os.path.basename(self.src)), |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
159 |
'duration':int(self.duration) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
160 |
} |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
161 |
content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = defaults_content) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
162 |
if not self.created: |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
163 |
for key, value in defaults_content.items(): |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
164 |
setattr(content, key, value) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
165 |
|
| 0 | 166 |
content.save() |
167 |
||
168 |
self.content = content |
|
169 |
||
170 |
def process(self): |
|
171 |
self.processIri() |
|
172 |
self.saveContent() |
|
173 |
||
174 |
class BaseFileImport(object): |
|
175 |
||
176 |
def __init__(self, filepath, videopath): |
|
177 |
self.__filepath = filepath |
|
178 |
self.__tempdir = "" |
|
179 |
self.__videopath = videopath |
|
180 |
self.__author = None |
|
181 |
||
182 |
||
183 |
def filepath(): #@NoSelf |
|
184 |
doc = """Docstring""" #@UnusedVariable |
|
185 |
||
186 |
def fget(self): |
|
187 |
return self.__filepath |
|
188 |
||
189 |
def fset(self, value): |
|
190 |
self.__filepath = value |
|
191 |
||
192 |
def fdel(self): |
|
193 |
del self.__filepath |
|
194 |
||
195 |
return locals() |
|
196 |
||
197 |
filepath = property(**filepath()) |
|
198 |
||
199 |
def videopath(): #@NoSelf |
|
200 |
doc = """Docstring""" #@UnusedVariable |
|
201 |
||
202 |
def fget(self): |
|
203 |
return self.__videopath |
|
204 |
||
205 |
def fset(self, value): |
|
206 |
self.__videopath = value |
|
207 |
||
208 |
def fdel(self): |
|
209 |
del self.__videopath |
|
210 |
||
211 |
return locals() |
|
212 |
||
213 |
videopath = property(**videopath()) |
|
214 |
||
215 |
def author(): #@NoSelf |
|
216 |
doc = """Docstring""" #@UnusedVariable |
|
217 |
||
218 |
def fget(self): |
|
219 |
return self.__author |
|
220 |
||
221 |
def fset(self, value): |
|
222 |
self.__author = value |
|
223 |
||
224 |
def fdel(self): |
|
225 |
del self.__author |
|
226 |
||
227 |
return locals() |
|
228 |
||
229 |
author = property(**author()) |
|
230 |
||
231 |
||
232 |
class FileImport(BaseFileImport): |
|
233 |
||
234 |
def __init__(self, filepath, videopath, flatten): |
|
235 |
BaseFileImport.__init__(self, filepath, videopath) |
|
236 |
self.__checkExistingMedia = False |
|
237 |
self.__flatten = flatten |
|
238 |
||
239 |
def checkExistingMedia(): #@NoSelf |
|
240 |
doc = """Docstring""" #@UnusedVariable |
|
241 |
||
242 |
def fget(self): |
|
243 |
return self.__checkExistingMedia |
|
244 |
||
245 |
def fset(self, value): |
|
246 |
self.__checkExistingMedia = value |
|
247 |
||
248 |
def fdel(self): |
|
249 |
del self.__checkExistingMedia |
|
250 |
||
251 |
return locals() |
|
252 |
||
253 |
checkExistingMedia = property(**checkExistingMedia()) |
|
254 |
||
255 |
def flatten(): #@NoSelf |
|
256 |
doc = """Docstring""" #@UnusedVariable |
|
257 |
||
258 |
def fget(self): |
|
259 |
return self.__flatten |
|
260 |
||
261 |
def fset(self, value): |
|
262 |
self.__flatten = value |
|
263 |
||
264 |
def fdel(self): |
|
265 |
del self.__flatten |
|
266 |
||
267 |
return locals() |
|
268 |
flatten = property(**flatten()) |
|
269 |
||
270 |
def processLdt(self, ldtpath=None): |
|
271 |
||
272 |
# list iri |
|
273 |
# see if there is some comments |
|
274 |
# inject comment in iri |
|
275 |
# copy iri in folder |
|
276 |
# create or update content |
|
277 |
contents = {} |
|
| 32 | 278 |
filepath = ldtpath if ldtpath else self.filepath |
279 |
doc = lxml.etree.parse(filepath) |
|
280 |
#if ldtpath: |
|
281 |
#doc = xml.dom.minidom.parse(ldtpath) |
|
282 |
# doc = lxml.etree.parse(ldtpath) |
|
283 |
#else: |
|
284 |
#doc = xml.dom.minidom.parse(self.filepath) |
|
| 0 | 285 |
|
| 32 | 286 |
#con = xml.xpath.Context.Context(doc, 1, 1, None) |
| 0 | 287 |
|
288 |
#get author from file ldt |
|
| 32 | 289 |
#result = xml.xpath.Evaluate("/iri/project", context=con) |
290 |
result = doc.xpath("/iri/project") |
|
| 0 | 291 |
for pnode in result: |
| 32 | 292 |
#author = pnode.getAttributeNS(None,u"user") |
293 |
author = pnode.attrib[u"user"] |
|
| 0 | 294 |
if author: |
295 |
self.author = unicode(author) |
|
296 |
break |
|
297 |
||
| 32 | 298 |
result = doc.xpath("/iri/medias/media") |
| 0 | 299 |
|
300 |
for i, medianode in enumerate(result): |
|
301 |
# get iri file's id from file ldt |
|
| 32 | 302 |
#id = medianode.attributes['id'].value |
303 |
id = medianode.attrib['id'] |
|
| 0 | 304 |
if self.checkExistingMedia: |
305 |
try: |
|
306 |
Content.objects.get(iri_id=id) |
|
307 |
do_pass = True |
|
308 |
except ObjectDoesNotExist: #Content.DoesNotExist |
|
309 |
do_pass = False |
|
310 |
else: |
|
311 |
do_pass = False |
|
312 |
if not do_pass: |
|
313 |
if not (contents.has_key(id)): |
|
314 |
# Create instance iriInfo(id, order, titledesc, basepath="", videopath=settings.STREAM_URL) |
|
315 |
if ldtpath: |
|
316 |
contents[id] = IriInfo(id, i, "", os.path.dirname(ldtpath), flatten=self.flatten) |
|
317 |
else: |
|
318 |
contents[id] = IriInfo(id, i, "", flatten=self.flatten) |
|
319 |
# Get iri file's url from ldt. This url can be relative path or absolute path. |
|
| 32 | 320 |
#contents[id].src = medianode.attributes['src'].value |
321 |
contents[id].src = medianode.attrib['src'] |
|
322 |
if medianode.attrib['video'] !="": |
|
323 |
contents[id].videopath = medianode.attrib['video'] |
|
| 0 | 324 |
elif self.videopath !="" or self.videopath: |
325 |
contents[id].videopath = self.videopath |
|
326 |
else: |
|
327 |
contents[id].videopath =settings.STREAM_URL |
|
328 |
||
329 |
||
330 |
#get annotation of file ldt |
|
| 32 | 331 |
result = doc.xpath("/iri/annotations/content") |
| 0 | 332 |
|
333 |
for contentnode in result: |
|
| 32 | 334 |
id = contentnode.attrib['id'] |
| 0 | 335 |
if contents.has_key(id): |
336 |
if self.author: |
|
| 32 | 337 |
contentnode.set("author", unicode(self.author)) |
| 0 | 338 |
contents[id].annotations = contentnode |
339 |
||
340 |
#go throught values |
|
341 |
for iriinfo in contents.values(): |
|
342 |
||
343 |
iriinfo.process() |
|
344 |
||
345 |
# if yes update |
|
346 |
# if no create |
|
347 |
# move iri file to the proper place |
|
348 |
#return list of iriInfo |
|
349 |
||
350 |
def processFile(self): |
|
351 |
if self.filepath.name.endswith(".ldt"): |
|
352 |
self.processLdt() |
|
353 |
elif self.filepath.name.endswith(".zip"): |
|
354 |
self.processZip() |
|
355 |
else: |
|
356 |
raise FileImportError("Bad file type") |
|
357 |
||
358 |
||
359 |
def processZip(self): |
|
360 |
# """ extraire .zip, pass to method processLdt""" |
|
361 |
# create temp directory |
|
362 |
self.__tempdir = tempfile.mkdtemp() |
|
363 |
openfiles = [] |
|
364 |
flvfiles = [] |
|
365 |
processedids = [] |
|
366 |
||
367 |
try: |
|
368 |
zipfile = zipfileext.ZipFileExt(self.filepath) |
|
369 |
zipfile.unzip_into_dir(self.__tempdir) |
|
370 |
#load ldt |
|
371 |
foldersToProcess = [self.__tempdir] |
|
372 |
while len(foldersToProcess): |
|
373 |
currentFolder = foldersToProcess.pop() |
|
374 |
for entry in os.listdir(currentFolder): |
|
375 |
if entry in settings.ZIP_BLACKLIST: |
|
376 |
continue |
|
377 |
entryPath = os.path.join(currentFolder, entry) |
|
378 |
if(os.path.isdir(entryPath)): |
|
379 |
foldersToProcess.append(entryPath) |
|
380 |
elif fnmatch.fnmatch(entry, "*.ldt"): |
|
381 |
ldtid = self.processLdt(entryPath) |
|
382 |
processedids.append(ldtid) |
|
383 |
elif fnmatch.fnmatch(entry, "*.flv"): |
|
384 |
flvfiles.append(entryPath) |
|
385 |
||
386 |
if settings.CONTENT_ROOT and os.path.exists(settings.CONTENT_ROOT): |
|
387 |
for entry in flvfiles: |
|
388 |
shutil.copy(entry, settings.CONTENT_ROOT) |
|
389 |
||
390 |
finally: |
|
391 |
for f in openfiles: |
|
392 |
f.close() |
|
393 |
shutil.rmtree(self.__tempdir) |
|
394 |
# delete directory |
|
395 |
return processedids |
|
396 |
||
397 |
# def processFileLdt(self): |
|
398 |
# processedids = [] |
|
399 |
# ldtid = self.processLdt(self.filepath) |
|
400 |
# processedids.append(ldtid) |
|
401 |
# return processedids |