1 import xml.dom |
1 import xml.dom |
2 import Ft.Xml.Domlette |
2 import Ft.Xml.Domlette |
3 import xml.xpath |
3 import xml.xpath |
4 from datetime import datetime |
4 from datetime import datetime |
|
5 from ldt.ldt_utils.models import Content, Project |
|
6 |
|
7 DATE_FORMATS = ["%d/%m/%Y","%Y-%m-%d"] |
5 |
8 |
6 """ |
9 """ |
7 Serialize a project object to a cinelab compatible array |
10 Serialize a project object to a cinelab compatible array |
8 """ |
11 """ |
9 class ProjectSerializer: |
12 class ProjectSerializer: |
19 self.lists = [] |
22 self.lists = [] |
20 |
23 |
21 |
24 |
22 def __parse_ensemble(self, ensemble_node, content): |
25 def __parse_ensemble(self, ensemble_node, content): |
23 |
26 |
24 ensemble_id = ensemble_node.getAttributeNS("id",None) |
27 ensemble_id = ensemble_node.getAttributeNS(None,u"id") |
25 ensemble_author = ensemble_node.getAttributeNS("author",None) |
28 ensemble_author = ensemble_node.getAttributeNS(None,u"author") |
26 ensemble_title = ensemble_node.getAttributeNS("title",None) |
29 ensemble_title = ensemble_node.getAttributeNS(None,u"title") |
27 ensemble_description = ensemble_node.getAttributeNS("abstract",None) |
30 ensemble_description = ensemble_node.getAttributeNS(None,u"abstract") |
28 ensemble_created = datetime.utcnow().iso_format() |
31 ensemble_created = datetime.utcnow().isoformat() |
29 ensemble_modified = ensemble_created |
32 ensemble_modified = ensemble_created |
30 |
33 |
31 list_items = [] |
34 list_items = [] |
32 new_list = { |
35 new_list = { |
33 "id" : ensemble_id, |
36 "id" : ensemble_id, |
48 |
51 |
49 for decoupage_node in ensemble_node.childNodes: |
52 for decoupage_node in ensemble_node.childNodes: |
50 if decoupage_node.nodeType != xml.dom.Node.ELEMENT_NODE or decoupage_node.tagName != "decoupage" : |
53 if decoupage_node.nodeType != xml.dom.Node.ELEMENT_NODE or decoupage_node.tagName != "decoupage" : |
51 continue |
54 continue |
52 |
55 |
53 decoupage_id = decoupage_node.getAttributeNS("id",None) |
56 decoupage_id = decoupage_node.getAttributeNS(None, u"id") |
54 decoupage_creator = decoupage_node.getAttributeNS("author",None) |
57 decoupage_creator = decoupage_node.getAttributeNS(None,u"author") |
55 if not decoupage_creator: |
58 if not decoupage_creator: |
56 decoupage_creator = "IRI" |
59 decoupage_creator = "IRI" |
57 decoupage_contributor = decoupage_creator |
60 decoupage_contributor = decoupage_creator |
58 date_str = decoupage_node.getAttributeNS("date",None) |
61 date_str = decoupage_node.getAttributeNS(None,u"date") |
|
62 decoupage_created = None |
59 if date_str : |
63 if date_str : |
60 decoupage_created = datetime.strptime(date_str,"%d/%m/%Y").iso_format() |
64 for date_format in DATE_FORMATS: |
61 else : |
65 try: |
62 decoupage_created = datetime.utcnow().iso_format() |
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() |
63 decoupage_modified = decoupage_created |
72 decoupage_modified = decoupage_created |
64 |
73 |
65 decoupage_title = "" |
74 decoupage_title = "" |
66 for txtRes in xml.xpath.Evaluate("title/text()", decoupage_node): |
75 for txtRes in xml.xpath.Evaluate("title/text()", decoupage_node): |
67 decoupage_title += txtRes.data |
76 decoupage_title += txtRes.data |
87 self.annotation_types.append(new_annotation_types) |
96 self.annotation_types.append(new_annotation_types) |
88 |
97 |
89 res = xml.xpath.Evaluate("elements/element", decoupage_node) |
98 res = xml.xpath.Evaluate("elements/element", decoupage_node) |
90 for element_node in res: |
99 for element_node in res: |
91 |
100 |
92 element_id = element_node.getAttributeNS("id",None) |
101 element_id = element_node.getAttributeNS(None,u"id") |
93 element_begin = element_node.getAttributeNS("begin",None) |
102 element_begin = element_node.getAttributeNS(None,u"begin") |
94 element_duration = element_node.getAttributeNS("dur",None) |
103 element_duration = element_node.getAttributeNS(None,u"dur") |
95 element_media = content.iri_id |
104 element_media = content.iri_id |
96 element_color = element_node.getAttributeNS("color",None) |
105 element_color = element_node.getAttributeNS(None,u"color") |
97 |
106 |
98 element_title = "" |
107 element_title = "" |
99 for txtRes in xml.xpath.Evaluate("title/text()", element_node): |
108 for txtRes in xml.xpath.Evaluate("title/text()", element_node): |
100 element_title += txtRes.data |
109 element_title += txtRes.data |
101 |
110 |
105 |
114 |
106 element_audio_src = "" |
115 element_audio_src = "" |
107 element_audio_href = "" |
116 element_audio_href = "" |
108 res = xml.xpath.Evaluate("audio", element_node) |
117 res = xml.xpath.Evaluate("audio", element_node) |
109 if len(res) > 0: |
118 if len(res) > 0: |
110 element_audio_src = res[0].getAttributeNS(None, "source") |
119 element_audio_src = res[0].getAttributeNS(None, u"source") |
111 element_audio_href = res[0].value |
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) |
112 |
125 |
113 |
126 |
114 element_tags = [] |
127 element_tags = [] |
115 |
128 |
116 tags = element_node.getAttributeNS("tags",None) |
129 tags = element_node.getAttributeNS(None,u"tags") |
117 |
130 |
118 tags_list = map(lambda s:s.trim(),tags.split(",")) |
131 tags_list = map(lambda s:s.strip(),tags.split(",")) |
119 |
132 |
120 #tags |
133 #tags |
121 if tags is None or len(tags) == 0: |
134 if tags is None or len(tags) == 0: |
122 tags_list = [] |
135 tags_list = [] |
123 restagnode = xml.xpath.Evaluate("tag/text()", element_node) |
136 restagnode = xml.xpath.Evaluate("tag/text()", element_node) |
128 tags_list = [] |
141 tags_list = [] |
129 restagnode = xml.xpath.Evaluate("tags/tag/text()", element_node) |
142 restagnode = xml.xpath.Evaluate("tags/tag/text()", element_node) |
130 for tagnode in restagnode: |
143 for tagnode in restagnode: |
131 tags_list.append(tagnode.data) |
144 tags_list.append(tagnode.data) |
132 |
145 |
133 tag_date = datetime.utcnow().iso_format() |
146 tag_date = datetime.utcnow().isoformat() |
134 for tag_id in tags_list: |
147 for tag_id in tags_list: |
135 if tag_id not in self.tags: |
148 if tag_id not in self.tags: |
136 new_tag = { |
149 new_tag = { |
137 "id":tag_id, |
150 "id":tag_id, |
138 "metas" : { |
151 "metas" : { |
175 |
188 |
176 |
189 |
177 |
190 |
178 def __parse_ldt(self): |
191 def __parse_ldt(self): |
179 |
192 |
180 doc = xml.dom.minidom.parseString(self.project.ldt) |
193 doc = xml.dom.minidom.parseString(self.project.ldt.encode("utf-8")) |
181 self.ldt_doc = Ft.Xml.Domlette.ConvertDocument(doc) |
194 self.ldt_doc = Ft.Xml.Domlette.ConvertDocument(doc) |
182 con = xml.xpath.Context.Context(doc, 1, 1, None) |
195 con = xml.xpath.Context.Context(doc, 1, 1, None) |
183 |
196 |
184 res = xml.xpath.Evaluate("/iri/medias/media", context=con) |
197 res = xml.xpath.Evaluate("/iri/medias/media", context=con) |
185 for mediaNode in res: |
198 for mediaNode in res: |
186 iri_id = mediaNode.getAttributeNS(None,"id") |
199 iri_id = mediaNode.getAttributeNS(None,u"id") |
187 content = Content.objects.get(iri_id=iri_id) |
200 content = Content.objects.get(iri_id=iri_id) |
188 self.__parser_content(content) |
201 self.__parse_content(content) |
189 |
202 |
190 res = xml.xpath.Evaluate("/iri/annotations/content",context=con) |
203 res = xml.xpath.Evaluate("/iri/annotations/content",context=con) |
191 |
204 |
192 for content_node in res: |
205 for content_node in res: |
193 content_id = content_node.getAttributeNS(None, "id") |
206 content_id = content_node.getAttributeNS(None, u"id") |
194 content = Content.objects.get(iri_id=content_id) |
207 content = Content.objects.get(iri_id=content_id) |
195 for ensemble_node in content_node.childNodes: |
208 for ensemble_node in content_node.childNodes: |
196 if ensemble_node.nodeType != xml.dom.Node.ELEMENT_NODE or ensemble_node.tagName != "ensemble" : |
209 if ensemble_node.nodeType != xml.dom.Node.ELEMENT_NODE or ensemble_node.tagName != "ensemble" : |
197 continue |
210 continue |
198 self.__parse_ensemble(ensemble_node, content) |
211 self.__parse_ensemble(ensemble_node, content) |
237 new_media = { |
250 new_media = { |
238 "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0", |
251 "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0", |
239 "id" : content.iri_id, |
252 "id" : content.iri_id, |
240 "url" : content.videopath.rstrip('/') + "/" + content.src, |
253 "url" : content.videopath.rstrip('/') + "/" + content.src, |
241 "dc:creator" : author, |
254 "dc:creator" : author, |
242 "dc:created" : content.creation_date.iso_format(), |
255 "dc:created" : content.creation_date.isoformat(), |
243 "dc:contributor" : contributor, |
256 "dc:contributor" : contributor, |
244 "dc:modified" : content.update_date.iso_format(), |
257 "dc:modified" : content.update_date.isoformat(), |
245 "dc:creator.contents" : content_author, |
258 "dc:creator.contents" : content_author, |
246 "dc:created.contents" : content_date, |
259 "dc:created.contents" : content_date, |
247 "dc:title" : content.title, |
260 "dc:title" : content.title, |
248 "dc:description" : content.description, |
261 "dc:description" : content.description, |
249 "dc:duration" : content.get_duration(), |
262 "dc:duration" : content.get_duration(), |
261 def serialize_to_cinelab(self): |
274 def serialize_to_cinelab(self): |
262 |
275 |
263 res = {} |
276 res = {} |
264 |
277 |
265 self.__parse_ldt() |
278 self.__parse_ldt() |
266 self.__parse_contents() |
|
267 |
279 |
268 project_main_media = "" |
280 project_main_media = "" |
269 if len(self.medias) > 0: |
281 if len(self.medias) > 0: |
270 project_main_media = self.medias[0]["id"] |
282 project_main_media = self.medias[0]["id"] |
271 |
283 |
272 res['metas'] = { |
284 res['metas'] = { |
273 'id': self.project.ldt_id, |
285 'id': self.project.ldt_id, |
274 'dc:created':self.project.creation_date.iso_format(), |
286 'dc:created':self.project.creation_date.isoformat(), |
275 'dc:modified':self.project.modification_date.iso_format(), |
287 'dc:modified':self.project.modification_date.isoformat(), |
276 'dc:contributor':self.project.changed_by, |
288 'dc:contributor':self.project.changed_by, |
277 'dc:creator':self.project.created_by, |
289 'dc:creator':self.project.created_by, |
278 'dc:title':self.project.title, |
290 'dc:title':self.project.title, |
279 'dc:description':self.project.get_description(self.ldt_doc), # get from doc, parse ldt |
291 'dc:description':self.project.get_description(self.ldt_doc), # get from doc, parse ldt |
280 'main_media': {"id-ref":project_main_media} |
292 'main_media': {"id-ref":project_main_media} |