| author | verrierj |
| Fri, 27 Jan 2012 16:27:47 +0100 | |
| changeset 479 | 1cbfcfcb2a47 |
| parent 468 | d1ff0694500b |
| child 482 | c802e00c7131 |
| permissions | -rw-r--r-- |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
1 |
from django.conf import settings |
| 415 | 2 |
from ldt.ldt_utils.models import Project |
| 479 | 3 |
from ldt.ldt_utils.stat import get_string_from_buckets |
| 415 | 4 |
import lxml.etree |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
5 |
import uuid |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
6 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
7 |
DATE_FORMATS = ["%d/%m/%Y", "%Y-%m-%d"] |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
8 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
9 |
class SegmentSerializer(object): |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
10 |
""" |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
11 |
Serialize a set of annotations to a cinelab compatible array |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
12 |
""" |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
13 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
14 |
def __init__(self, content, segments, viewable_contents=[], default_color=2194379): |
| 420 | 15 |
""" |
16 |
viewable_contents should contain all contents from project that a user is allowed to see. The settings.FORBIDDDEN_STREAM_URL |
|
17 |
will be displayed if a content is not found in viewable_contents, and the real stream will be displayed if it is. |
|
18 |
""" |
|
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
19 |
self.content = content |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
20 |
self.segments = segments |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
21 |
self.viewable_contents = viewable_contents |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
22 |
self.default_color = default_color |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
23 |
self.views = None |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
24 |
self.annotation_types = None |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
25 |
self.medias = None |
| 426 | 26 |
self.annotations = None |
27 |
self.tags = {} |
|
28 |
||
| 415 | 29 |
self.xml_docs = {} |
30 |
||
31 |
def __get_cutting_title(self, project_id, content_id, ensemble_id, cutting_id): |
|
32 |
||
33 |
if not self.xml_docs.has_key(project_id): |
|
34 |
project = Project.objects.get(ldt_id=project_id) |
|
35 |
doc = lxml.etree.fromstring(project.ldt) |
|
36 |
self.xml_docs[project_id] = doc |
|
37 |
else: |
|
38 |
doc = self.xml_docs[project_id] |
|
39 |
||
| 426 | 40 |
cutting = doc.xpath('/iri/annotations/content[@id=\'%s\']/ensemble[@id=\'%s\']/decoupage[@id=\'%s\']/title' % (content_id, ensemble_id, cutting_id)) |
41 |
if not cutting: |
|
42 |
return None |
|
43 |
||
44 |
cutting = cutting[0] |
|
| 415 | 45 |
|
46 |
return cutting.text |
|
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
47 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
48 |
def __parse_views(self): |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
49 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
50 |
view = { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
51 |
"id": "0", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
52 |
"contents": [ |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
53 |
self.content.iri_id |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
54 |
], |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
55 |
"annotation_types": [ ], |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
56 |
} |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
57 |
|
| 424 | 58 |
stat = { |
59 |
"id": "stat", |
|
60 |
"contents": [ |
|
61 |
self.content.iri_id |
|
62 |
], |
|
63 |
"meta": { |
|
| 479 | 64 |
"stat": get_string_from_buckets(self.content.annotation_volume) |
| 424 | 65 |
} |
66 |
} |
|
67 |
||
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
68 |
self.annotation_types = [] |
| 415 | 69 |
|
70 |
annotation_types = [] |
|
71 |
for seg in self.segments: |
|
72 |
title = self.__get_cutting_title(seg.project_id, seg.iri_id, seg.ensemble_id, seg.cutting_id) |
|
73 |
annotation_types.append({'id': seg.cutting_id, 'title': title}) |
|
74 |
||
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
75 |
for a in annotation_types: |
| 426 | 76 |
|
| 415 | 77 |
view['annotation_types'].append(a['id']) |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
78 |
self.annotation_types.append({ |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
79 |
"dc:contributor": "undefined", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
80 |
"dc:creator": "undefined", |
| 415 | 81 |
"dc:title": a['title'], |
82 |
"id": a['id'], |
|
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
83 |
"dc:created": "", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
84 |
"dc:description": "", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
85 |
"dc:modified": "" |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
86 |
}) |
| 415 | 87 |
|
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
88 |
|
| 424 | 89 |
self.views = [view, stat] |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
90 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
91 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
92 |
def __parse_content(self): |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
93 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
94 |
href = "" |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
95 |
meta_item_value = "" |
|
427
e0158472f83a
Fix pluralized translations + clean-up segmentserializer
verrierj
parents:
426
diff
changeset
|
96 |
|
|
e0158472f83a
Fix pluralized translations + clean-up segmentserializer
verrierj
parents:
426
diff
changeset
|
97 |
if "Content" in settings.USE_GROUP_PERMISSIONS and self.content: |
|
e0158472f83a
Fix pluralized translations + clean-up segmentserializer
verrierj
parents:
426
diff
changeset
|
98 |
if self.content not in self.viewable_contents: |
|
e0158472f83a
Fix pluralized translations + clean-up segmentserializer
verrierj
parents:
426
diff
changeset
|
99 |
href = settings.FORBIDDEN_STREAM_URL |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
100 |
elif self.content.videopath: |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
101 |
href = self.content.videopath.rstrip('/') + "/" + self.content.src |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
102 |
meta_item_value = self.content.videopath.rstrip('/') + "/" |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
103 |
else: |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
104 |
href = self.content.src |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
105 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
106 |
media = { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
107 |
"http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
108 |
"id" : self.content.iri_id, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
109 |
"href" : href, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
110 |
"unit" : "ms", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
111 |
"origin" : "0", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
112 |
"meta": { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
113 |
"dc:creator" : "", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
114 |
"dc:created" : self.content.creation_date.isoformat(), |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
115 |
"dc:contributor" : "", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
116 |
"dc:modified" : self.content.update_date.isoformat(), |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
117 |
"dc:creator.contents" : "", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
118 |
"dc:title" : self.content.title, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
119 |
"dc:description" : self.content.description, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
120 |
"dc:duration" : self.content.get_duration(), |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
121 |
"item": { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
122 |
"name" : "streamer", |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
123 |
"value": meta_item_value, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
124 |
}, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
125 |
} |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
126 |
} |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
127 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
128 |
self.medias = [media] |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
129 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
130 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
131 |
def __parse_segments(self): |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
132 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
133 |
self.annotations = [] |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
134 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
135 |
for seg in self.segments: |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
136 |
|
| 426 | 137 |
segment_tags = [] |
138 |
for tag in seg.tags.split(';'): |
|
139 |
if not self.tags.has_key(tag): |
|
140 |
new_tag = { |
|
141 |
"meta": { |
|
142 |
"dc:contributor": "IRI", |
|
143 |
"dc:created": seg.date, |
|
144 |
"dc:title": tag, |
|
145 |
"dc:modified": seg.date, |
|
146 |
"dc:creator": "IRI" |
|
147 |
}, |
|
148 |
"id": unicode(uuid.uuid1()) |
|
149 |
} |
|
150 |
self.tags[tag] = new_tag |
|
151 |
segment_tags.append({'id-ref': self.tags[tag]['id']}) |
|
152 |
||
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
153 |
segment = { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
154 |
'begin': seg.start_ts, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
155 |
'end': seg.start_ts + seg.duration, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
156 |
'tags': seg.tags, |
|
427
e0158472f83a
Fix pluralized translations + clean-up segmentserializer
verrierj
parents:
426
diff
changeset
|
157 |
'id': seg.element_id, |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
158 |
'color': "%s" % self.default_color, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
159 |
'media': self.content.iri_id, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
160 |
'content': { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
161 |
'mimetype': 'application/x-ldt-structured', |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
162 |
'description': seg.abstract, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
163 |
'img': { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
164 |
'src': '' |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
165 |
}, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
166 |
'title': seg.title, |
|
427
e0158472f83a
Fix pluralized translations + clean-up segmentserializer
verrierj
parents:
426
diff
changeset
|
167 |
'color': self.default_color, |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
168 |
'polemics': [ ], |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
169 |
'audio': { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
170 |
'mimetype': 'audio/mp3', |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
171 |
'src': '', |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
172 |
'href': 'null' |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
173 |
} |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
174 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
175 |
}, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
176 |
'meta': { |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
177 |
"dc:creator": seg.author, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
178 |
"dc:contributor": seg.author, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
179 |
"dc:created": seg.date, |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
180 |
"dc:modified": seg.date, |
|
427
e0158472f83a
Fix pluralized translations + clean-up segmentserializer
verrierj
parents:
426
diff
changeset
|
181 |
"id-ref": seg.cutting_id, |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
182 |
"project": seg.project_id |
| 426 | 183 |
}, |
184 |
'tags': segment_tags |
|
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
185 |
} |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
186 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
187 |
self.annotations.append(segment) |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
188 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
189 |
def serialize_to_cinelab(self): |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
190 |
|
| 426 | 191 |
if not self.segments: |
192 |
return None |
|
193 |
||
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
194 |
self.__parse_content() |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
195 |
self.__parse_segments() |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
196 |
self.__parse_views() |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
197 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
198 |
res = {} |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
199 |
res['views'] = self.views |
| 426 | 200 |
res['tags'] = self.tags.values() if len(self.tags) > 0 else None |
|
412
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
201 |
res['lists'] = None |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
202 |
res['medias'] = self.medias |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
203 |
res['annotations'] = self.annotations |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
204 |
res['annotation-types'] = self.annotation_types |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
205 |
|
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
206 |
return res |
|
8d777b1d1d92
Add API to search annotations inside a content between two timecodes
verrierj
parents:
diff
changeset
|
207 |