8 """ |
8 """ |
9 Serialize a project object to a cinelab compatible array |
9 Serialize a project object to a cinelab compatible array |
10 """ |
10 """ |
11 class ProjectSerializer: |
11 class ProjectSerializer: |
12 |
12 |
13 def __init__(self, project): |
13 def __init__(self, project, from_contents=True, from_display=True): |
14 self.project = project |
14 self.project = project |
|
15 self.parsed = False |
15 self.ldt_doc = None |
16 self.ldt_doc = None |
16 self.medias = [] |
17 self.medias = [] |
17 self.annotations = [] |
18 self.annotations = [] |
18 self.tags = {} |
19 self.tags = {} |
|
20 self.tags_by_id = {} |
19 self.annotation_types = [] |
21 self.annotation_types = [] |
20 self.views = [] |
22 self.views = [] |
21 self.lists = [] |
23 self.lists = [] |
|
24 self.serialize_contents = from_contents |
|
25 self.from_display = from_display |
22 |
26 |
23 |
27 |
24 def __parse_ensemble(self, ensemble_node, content): |
28 def __parse_ensemble(self, ensemble_node, content): |
25 |
29 |
26 ensemble_id = ensemble_node.attrib[u"id"] |
30 ensemble_id = ensemble_node.attrib[u"id"] |
197 for mediaNode in res: |
202 for mediaNode in res: |
198 iri_id = mediaNode.attrib[u"id"] |
203 iri_id = mediaNode.attrib[u"id"] |
199 content = Content.objects.get(iri_id=iri_id) |
204 content = Content.objects.get(iri_id=iri_id) |
200 self.__parse_content(content) |
205 self.__parse_content(content) |
201 |
206 |
202 res = self.ldt_doc.xpath("/iri/annotations/content") |
207 res = self.ldt_doc.xpath("/iri/annotations/content") |
203 |
|
204 for content_node in res: |
208 for content_node in res: |
205 content_id = content_node.attrib[u"id"] |
209 content_id = content_node.attrib[u"id"] |
206 content = Content.objects.get(iri_id=content_id) |
210 content = Content.objects.get(iri_id=content_id) |
207 for ensemble_node in content_node: |
211 for ensemble_node in content_node: |
208 if ensemble_node.tag != "ensemble" : |
212 if ensemble_node.tag != "ensemble" : |
209 continue |
213 continue |
210 self.__parse_ensemble(ensemble_node, content) |
214 self.__parse_ensemble(ensemble_node, content) |
211 |
215 |
|
216 # res = self.ldt_doc.xpath("/iri/displays/display") |
|
217 # for display_node in res: |
|
218 # pass |
|
219 |
|
220 self.parsed = True |
212 |
221 |
213 def __parse_content(self, content): |
222 def __parse_content(self, content): |
214 |
223 |
215 doc = lxml.etree.parse(content.iri_file_path()) |
224 doc = lxml.etree.parse(content.iri_file_path()) |
216 |
225 |
237 |
246 |
238 res = doc.xpath("/iri/head/meta[@name='date']/@content") |
247 res = doc.xpath("/iri/head/meta[@name='date']/@content") |
239 if len(res) > 0: |
248 if len(res) > 0: |
240 content_date = res[0] |
249 content_date = res[0] |
241 |
250 |
242 |
251 href = "" |
|
252 meta_item_value = "" |
|
253 if content.videopath: |
|
254 href = content.videopath.rstrip('/') + "/" + content.src |
|
255 meta_item_value = content.videopath.rstrip('/') + "/" |
|
256 |
243 new_media = { |
257 new_media = { |
244 "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0", |
258 "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0", |
245 "id" : content.iri_id, |
259 "id" : content.iri_id, |
246 "href" : content.videopath.rstrip('/') + "/" + content.src, |
260 "href" : href, |
247 "unit" : "ms", |
261 "unit" : "ms", |
248 "origin" : "0", |
262 "origin" : "0", |
249 "meta": { |
263 "meta": { |
250 "dc:creator" : author, |
264 "dc:creator" : author, |
251 "dc:created" : content.creation_date.isoformat(), |
265 "dc:created" : content.creation_date.isoformat(), |
256 "dc:title" : content.title, |
270 "dc:title" : content.title, |
257 "dc:description" : content.description, |
271 "dc:description" : content.description, |
258 "dc:duration" : content.get_duration(), |
272 "dc:duration" : content.get_duration(), |
259 "item": { |
273 "item": { |
260 "name" : "streamer", |
274 "name" : "streamer", |
261 "value": content.videopath.rstrip('/') + "/" |
275 "value": meta_item_value, |
262 }, |
276 }, |
263 } |
277 } |
264 } |
278 } |
265 |
279 |
266 self.medias.append(new_media) |
280 self.medias.append(new_media) |
267 |
281 |
268 |
282 if self.serialize_contents: |
269 res = doc.xpath("/iri/body/ensembles/ensemble") |
283 res = doc.xpath("/iri/body/ensembles/ensemble") |
270 |
284 for ensemble_node in res: |
271 for ensemble_node in res: |
285 self.__parse_ensemble(ensemble_node, content) |
272 self.__parse_ensemble(ensemble_node, content) |
|
273 |
286 |
274 |
287 |
275 def serialize_to_cinelab(self): |
288 def serialize_to_cinelab(self): |
276 |
289 |
277 res = {} |
290 res = {} |
278 |
291 |
279 self.__parse_ldt() |
292 if not self.parsed: |
|
293 self.__parse_ldt() |
280 |
294 |
281 project_main_media = "" |
295 project_main_media = "" |
282 if len(self.medias) > 0: |
296 if len(self.medias) > 0: |
283 project_main_media = self.medias[0]["id"] |
297 project_main_media = self.medias[0]["id"] |
284 |
298 |
319 res['lists'] = self.lists |
333 res['lists'] = self.lists |
320 res['tags'] = tags |
334 res['tags'] = tags |
321 res['views'] = self.views # ignored for the moment |
335 res['views'] = self.views # ignored for the moment |
322 |
336 |
323 return res |
337 return res |
324 |
338 |
|
339 def getAnnotations(self, first_cutting=True): |
|
340 |
|
341 if not self.parsed: |
|
342 self.__parse_ldt() |
|
343 |
|
344 annotations = [] |
|
345 |
|
346 current_cutting = None |
|
347 uri = None |
|
348 for annot in self.annotations: |
|
349 if first_cutting and current_cutting and current_cuttings != annot['meta']['id-ref'] : |
|
350 break |
|
351 current_cuttings = annot['meta']['id-ref'] |
|
352 content_id = annot['media'] |
|
353 content = Content.objects.get(iri_id=content_id) |
|
354 if annot['tags']: |
|
355 tags_list = map(lambda tag_entry: self.tags_by_id[tag_entry['id-ref']]['meta']['dc:title'],annot['tags']) |
|
356 else: |
|
357 tags_list = [] |
|
358 begin = int(annot['begin']) |
|
359 duration = int(annot['end'])-begin |
|
360 if content.media_obj and content.media_obj.external_publication_url: |
|
361 uri = "%s#t=%d" % (content.media_obj.external_publication_url, begin) |
|
362 |
|
363 |
|
364 annotations.append({ |
|
365 'begin': begin, |
|
366 'duration':duration, |
|
367 'title':annot['content']['title'], |
|
368 'desc':annot['content']['description'], |
|
369 'tags': tags_list, |
|
370 'id':annot['id'], |
|
371 'uri':uri |
|
372 }) |
|
373 |
|
374 return annotations |
|
375 |
|
376 |