8
|
1 |
import xml.dom |
|
2 |
import Ft.Xml.Domlette |
|
3 |
import xml.xpath |
|
4 |
from datetime import datetime |
10
|
5 |
from ldt.ldt_utils.models import Content, Project |
|
6 |
|
|
7 |
DATE_FORMATS = ["%d/%m/%Y","%Y-%m-%d"] |
8
|
8 |
|
|
9 |
""" |
|
10 |
Serialize a project object to a cinelab compatible array |
|
11 |
""" |
|
12 |
class ProjectSerializer: |
|
13 |
|
|
14 |
def __init__(self, project): |
|
15 |
self.project = project |
|
16 |
self.ldt_doc = None |
|
17 |
self.medias = [] |
|
18 |
self.annotations = [] |
|
19 |
self.tags = {} |
|
20 |
self.annotation_types = [] |
|
21 |
self.views = [] |
|
22 |
self.lists = [] |
|
23 |
|
|
24 |
|
|
25 |
def __parse_ensemble(self, ensemble_node, content): |
|
26 |
|
10
|
27 |
ensemble_id = ensemble_node.getAttributeNS(None,u"id") |
|
28 |
ensemble_author = ensemble_node.getAttributeNS(None,u"author") |
|
29 |
ensemble_title = ensemble_node.getAttributeNS(None,u"title") |
|
30 |
ensemble_description = ensemble_node.getAttributeNS(None,u"abstract") |
|
31 |
ensemble_created = datetime.utcnow().isoformat() |
8
|
32 |
ensemble_modified = ensemble_created |
|
33 |
|
|
34 |
list_items = [] |
|
35 |
new_list = { |
|
36 |
"id" : ensemble_id, |
|
37 |
"items" : list_items, |
|
38 |
"metas" : { |
|
39 |
"dc:creator":ensemble_author, |
|
40 |
"dc:created": ensemble_created, |
|
41 |
"dc:contributor":"undefined", |
|
42 |
"dc:modified": ensemble_modified, |
|
43 |
"dc:title":ensemble_title, |
|
44 |
"dc:description": ensemble_description, |
|
45 |
"id-ref":content.iri_id, |
|
46 |
"editable":"false" |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
self.lists.append(new_list) |
|
51 |
|
|
52 |
for decoupage_node in ensemble_node.childNodes: |
|
53 |
if decoupage_node.nodeType != xml.dom.Node.ELEMENT_NODE or decoupage_node.tagName != "decoupage" : |
|
54 |
continue |
|
55 |
|
10
|
56 |
decoupage_id = decoupage_node.getAttributeNS(None, u"id") |
|
57 |
decoupage_creator = decoupage_node.getAttributeNS(None,u"author") |
8
|
58 |
if not decoupage_creator: |
|
59 |
decoupage_creator = "IRI" |
|
60 |
decoupage_contributor = decoupage_creator |
10
|
61 |
date_str = decoupage_node.getAttributeNS(None,u"date") |
|
62 |
decoupage_created = None |
8
|
63 |
if date_str : |
10
|
64 |
for date_format in DATE_FORMATS: |
|
65 |
try: |
|
66 |
decoupage_created = datetime.strptime(date_str,date_format).isoformat() |
|
67 |
break |
|
68 |
except Exception: |
|
69 |
decoupage_created = None |
|
70 |
if decoupage_created is None: |
|
71 |
decoupage_created = datetime.utcnow().isoformat() |
8
|
72 |
decoupage_modified = decoupage_created |
|
73 |
|
|
74 |
decoupage_title = "" |
|
75 |
for txtRes in xml.xpath.Evaluate("title/text()", decoupage_node): |
|
76 |
decoupage_title += txtRes.data |
|
77 |
|
|
78 |
decoupage_description = "" |
|
79 |
for txtRes in xml.xpath.Evaluate("abstract/text()", decoupage_node): |
|
80 |
decoupage_description += txtRes.data |
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
list_items.append({"id-ref":decoupage_id}) |
|
85 |
|
|
86 |
new_annotation_types = { |
|
87 |
"id":decoupage_id, |
|
88 |
"dc:creator":decoupage_creator, |
|
89 |
"dc:created":decoupage_created, |
|
90 |
"dc:contributor":decoupage_contributor, |
|
91 |
"dc:modified":decoupage_modified, |
|
92 |
"dc:title":decoupage_title, |
|
93 |
"dc:description":decoupage_description |
|
94 |
} |
|
95 |
|
|
96 |
self.annotation_types.append(new_annotation_types) |
|
97 |
|
|
98 |
res = xml.xpath.Evaluate("elements/element", decoupage_node) |
|
99 |
for element_node in res: |
|
100 |
|
10
|
101 |
element_id = element_node.getAttributeNS(None,u"id") |
|
102 |
element_begin = element_node.getAttributeNS(None,u"begin") |
|
103 |
element_duration = element_node.getAttributeNS(None,u"dur") |
8
|
104 |
element_media = content.iri_id |
10
|
105 |
element_color = element_node.getAttributeNS(None,u"color") |
8
|
106 |
|
|
107 |
element_title = "" |
|
108 |
for txtRes in xml.xpath.Evaluate("title/text()", element_node): |
|
109 |
element_title += txtRes.data |
|
110 |
|
|
111 |
element_description = "" |
|
112 |
for txtRes in xml.xpath.Evaluate("abstract/text()", element_node): |
|
113 |
element_description += txtRes.data |
|
114 |
|
|
115 |
element_audio_src = "" |
|
116 |
element_audio_href = "" |
|
117 |
res = xml.xpath.Evaluate("audio", element_node) |
|
118 |
if len(res) > 0: |
10
|
119 |
element_audio_src = res[0].getAttributeNS(None, u"source") |
|
120 |
ltext = [] |
|
121 |
for n in res[0].childNodes: |
|
122 |
if n.nodeType in (dom.Node.TEXT_NODE, dom.Node.CDATA_SECTION_NODE): |
|
123 |
ltext.append(n.data) |
|
124 |
element_audio_href = ''.join(ltext) |
8
|
125 |
|
|
126 |
|
|
127 |
element_tags = [] |
|
128 |
|
10
|
129 |
tags = element_node.getAttributeNS(None,u"tags") |
8
|
130 |
|
10
|
131 |
tags_list = map(lambda s:s.strip(),tags.split(",")) |
8
|
132 |
|
|
133 |
#tags |
|
134 |
if tags is None or len(tags) == 0: |
|
135 |
tags_list = [] |
|
136 |
restagnode = xml.xpath.Evaluate("tag/text()", element_node) |
|
137 |
for tagnode in restagnode: |
|
138 |
tags_list.append(tagnode.data) |
|
139 |
|
|
140 |
if tags_list is None or len(tags_list) == 0: |
|
141 |
tags_list = [] |
|
142 |
restagnode = xml.xpath.Evaluate("tags/tag/text()", element_node) |
|
143 |
for tagnode in restagnode: |
|
144 |
tags_list.append(tagnode.data) |
|
145 |
|
10
|
146 |
tag_date = datetime.utcnow().isoformat() |
8
|
147 |
for tag_id in tags_list: |
|
148 |
if tag_id not in self.tags: |
|
149 |
new_tag = { |
|
150 |
"id":tag_id, |
|
151 |
"metas" : { |
|
152 |
"dc:creator":"IRI", |
|
153 |
"dc:created": tag_date, |
|
154 |
"dc:contributor":"IRI", |
|
155 |
"dc:modified": tag_date, |
|
156 |
"dc:title":tag_id |
|
157 |
} |
|
158 |
} |
|
159 |
self.tags[tag_id] = new_tag |
|
160 |
element_tags.append({"id-ref":tag_id}) |
|
161 |
|
|
162 |
new_annotation = { |
|
163 |
"begin": element_begin, |
|
164 |
"end": int(element_begin) + int(element_duration), |
|
165 |
"id": element_id, |
|
166 |
"media": element_media, |
|
167 |
"content": { |
|
168 |
"mimetype": "application/x-ldt-structured", |
|
169 |
"title": element_title, |
|
170 |
"description": element_description, |
|
171 |
"color": element_color, |
|
172 |
"audio": { |
|
173 |
"src" : element_audio_src, |
|
174 |
"mimetype": "audio/mp3", |
|
175 |
"href": element_audio_href |
|
176 |
}, |
|
177 |
}, |
|
178 |
"metas": { |
|
179 |
"id-ref": decoupage_id, |
|
180 |
"dc:creator": decoupage_creator, |
|
181 |
"dc:contributor": decoupage_contributor, |
|
182 |
"dc:created": decoupage_created, |
|
183 |
"dc:modified": decoupage_modified |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
self.annotations.append(new_annotation) |
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
def __parse_ldt(self): |
|
192 |
|
10
|
193 |
doc = xml.dom.minidom.parseString(self.project.ldt.encode("utf-8")) |
8
|
194 |
self.ldt_doc = Ft.Xml.Domlette.ConvertDocument(doc) |
|
195 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
196 |
|
|
197 |
res = xml.xpath.Evaluate("/iri/medias/media", context=con) |
|
198 |
for mediaNode in res: |
10
|
199 |
iri_id = mediaNode.getAttributeNS(None,u"id") |
8
|
200 |
content = Content.objects.get(iri_id=iri_id) |
10
|
201 |
self.__parse_content(content) |
8
|
202 |
|
|
203 |
res = xml.xpath.Evaluate("/iri/annotations/content",context=con) |
|
204 |
|
|
205 |
for content_node in res: |
10
|
206 |
content_id = content_node.getAttributeNS(None, u"id") |
8
|
207 |
content = Content.objects.get(iri_id=content_id) |
|
208 |
for ensemble_node in content_node.childNodes: |
|
209 |
if ensemble_node.nodeType != xml.dom.Node.ELEMENT_NODE or ensemble_node.tagName != "ensemble" : |
|
210 |
continue |
|
211 |
self.__parse_ensemble(ensemble_node, content) |
|
212 |
|
|
213 |
#res = xml.xpath.Evaluate("/iri/displays/display",context=con) |
|
214 |
|
|
215 |
#for display_node in res: |
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
def __parse_content(self, content): |
|
220 |
|
|
221 |
doc = Ft.Xml.Domlette.ConvertDocument(xml.dom.minidom.parse(content.iri_file_path())) |
|
222 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
223 |
|
|
224 |
authors = content.authors.all() |
|
225 |
|
|
226 |
if len(authors) > 0 : |
|
227 |
author = authors[0].handle |
|
228 |
else : |
|
229 |
author = "IRI" |
|
230 |
|
|
231 |
if len(authors) > 1 : |
|
232 |
contributor = authors[1].handle |
|
233 |
else : |
|
234 |
contributor = author |
|
235 |
|
|
236 |
content_author = "" |
|
237 |
|
|
238 |
res = xml.xpath.Evaluate("/iri/head/meta[@name='author']/@content", context=con) |
|
239 |
if len(res) > 0: |
|
240 |
content_author = res[0].value |
|
241 |
|
|
242 |
|
|
243 |
content_date = "" |
|
244 |
|
|
245 |
res = xml.xpath.Evaluate("/iri/head/meta[@name='date']/@content", context=con) |
|
246 |
if len(res) > 0: |
|
247 |
content_date = res[0].value |
|
248 |
|
|
249 |
|
|
250 |
new_media = { |
|
251 |
"http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0", |
|
252 |
"id" : content.iri_id, |
|
253 |
"url" : content.videopath.rstrip('/') + "/" + content.src, |
|
254 |
"dc:creator" : author, |
10
|
255 |
"dc:created" : content.creation_date.isoformat(), |
8
|
256 |
"dc:contributor" : contributor, |
10
|
257 |
"dc:modified" : content.update_date.isoformat(), |
8
|
258 |
"dc:creator.contents" : content_author, |
|
259 |
"dc:created.contents" : content_date, |
|
260 |
"dc:title" : content.title, |
|
261 |
"dc:description" : content.description, |
|
262 |
"dc:duration" : content.get_duration(), |
|
263 |
} |
|
264 |
|
|
265 |
self.medias.append(new_media) |
|
266 |
|
|
267 |
|
|
268 |
res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble",context=con) |
|
269 |
|
|
270 |
for ensemble_node in res: |
|
271 |
self.__parse_ensemble(ensemble_node, content) |
|
272 |
|
|
273 |
|
|
274 |
def serialize_to_cinelab(self): |
|
275 |
|
|
276 |
res = {} |
|
277 |
|
|
278 |
self.__parse_ldt() |
|
279 |
|
|
280 |
project_main_media = "" |
|
281 |
if len(self.medias) > 0: |
|
282 |
project_main_media = self.medias[0]["id"] |
|
283 |
|
|
284 |
res['metas'] = { |
|
285 |
'id': self.project.ldt_id, |
10
|
286 |
'dc:created':self.project.creation_date.isoformat(), |
|
287 |
'dc:modified':self.project.modification_date.isoformat(), |
8
|
288 |
'dc:contributor':self.project.changed_by, |
|
289 |
'dc:creator':self.project.created_by, |
|
290 |
'dc:title':self.project.title, |
|
291 |
'dc:description':self.project.get_description(self.ldt_doc), # get from doc, parse ldt |
|
292 |
'main_media': {"id-ref":project_main_media} |
|
293 |
} |
|
294 |
|
|
295 |
res['medias'] = self.medias |
|
296 |
res['annotation-types'] = self.annotation_types |
|
297 |
res['annotations'] = self.annotations |
|
298 |
res['lists'] = self.lists |
|
299 |
res['tags'] = self.tags.values() |
|
300 |
res['views'] = self.views # ignored for the moment |
|
301 |
|
|
302 |
return res |