Add API to search annotations inside a content between two timecodes
authorverrierj
Wed, 18 Jan 2012 15:30:35 +0100
changeset 412 8d777b1d1d92
parent 411 d126a67897c0
child 414 6fb6d534009a
child 415 4236f99104ba
Add API to search annotations inside a content between two timecodes
src/ldt/ldt/api/ldt/handlers.py
src/ldt/ldt/api/ldt/urls.py
src/ldt/ldt/ldt_utils/segmentserializer.py
src/ldt/ldt/ldt_utils/stat.py
--- a/src/ldt/ldt/api/ldt/handlers.py	Wed Jan 18 10:53:49 2012 +0100
+++ b/src/ldt/ldt/api/ldt/handlers.py	Wed Jan 18 15:30:35 2012 +0100
@@ -1,9 +1,11 @@
-from ldt.ldt_utils.models import Project, Content
+from ldt.ldt_utils.models import Project, Content, Segment
+from django.db.models import F, Q
 from piston.handler import BaseHandler
 from piston.utils import rc, require_extended
 from ldt.ldt_utils.utils import LdtAnnotation
 from ldt.ldt_utils.stat import add_annotation_to_stat
 from ldt.security.utils import protect_instance, unprotect_instance
+from ldt.ldt_utils.segmentserializer import SegmentSerializer
 import logging #@UnresolvedImport
 
 
@@ -222,3 +224,27 @@
         """
         return Content.objects.get(iri_id=iri_id)
     
+
+class SegmentHandler(BaseHandler):
+    allowed_methods = ('GET', )
+    model = Segment
+    exclude = (
+               ("project_obj"),
+               ("content"),
+               )
+    
+    def read(self, request, iri_id, begin, end):
+        """
+        returns a single Segment
+        """
+        
+        content = Content.objects.get(iri_id=iri_id)
+        segments = Segment.objects.filter(content=content).filter(
+                    Q(start_ts__gt=begin, start_ts__lt=end) |                            # segment start between begin and end
+                    Q(start_ts__gt=begin-F('duration'), start_ts__lt=end-F('duration')) |# segment ends between begin and end
+                    Q(start_ts__lt=begin, start_ts__gt=end-F('duration'))                # period [begin:end] is included in the segment
+                    )
+        
+        a = SegmentSerializer(content, segments, viewable_contents=[content.iri_id])
+        return a.serialize_to_cinelab()
+    
--- a/src/ldt/ldt/api/ldt/urls.py	Wed Jan 18 10:53:49 2012 +0100
+++ b/src/ldt/ldt/api/ldt/urls.py	Wed Jan 18 15:30:35 2012 +0100
@@ -1,12 +1,15 @@
 from django.conf.urls.defaults import patterns, url
 from piston.resource import Resource
-from ldt.api.ldt.handlers import ProjectHandler, ContentHandler
+from ldt.api.ldt.handlers import ProjectHandler, ContentHandler, SegmentHandler
 
 project_handler = Resource(ProjectHandler, None)
 content_handler = Resource(ContentHandler, None)
+segment_handler = Resource(SegmentHandler, None)
 
 urlpatterns = patterns('',
     url(r'projects/(?P<project_id>[^/.]+)\.?(?P<emitter_format>.*)$', project_handler, name='project_api'),
     url(r'contents/(?P<iri_id>[^/.]+)\.?(?P<emitter_format>.*)$', content_handler, name='content_api'),
+    url(r'segments/(?P<iri_id>.*)/(?P<begin>\d+)/(?P<end>\d+)$', segment_handler),
 )
 
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/ldt_utils/segmentserializer.py	Wed Jan 18 15:30:35 2012 +0100
@@ -0,0 +1,143 @@
+from django.conf import settings
+from django.utils.datastructures import SortedDict
+import uuid
+
+DATE_FORMATS = ["%d/%m/%Y", "%Y-%m-%d"]
+
+class SegmentSerializer(object):
+    """
+    Serialize a set of annotations to a cinelab compatible array
+    """
+    
+    def __init__(self, content, segments, viewable_contents=[], default_color=2194379):
+        self.content = content
+        self.segments = segments
+        self.viewable_contents = viewable_contents
+        self.default_color = default_color
+        self.views = None
+        self.annotation_types = None
+        self.medias = None
+        self.annotations = None
+        
+    
+    def __parse_views(self):
+        
+        view = {
+                "id": "0",
+                "contents": [
+                    self.content.iri_id
+                ],
+                "annotation_types": [ ],
+        }
+        
+        self.annotation_types = []
+        annotation_types = [seg.cutting_id for seg in self.segments]
+        for a in annotation_types:
+            view['annotation_types'].append(a)
+            self.annotation_types.append({
+                    "dc:contributor": "undefined",
+                    "dc:creator": "undefined",
+                    "dc:title": "cutting %s" % a,
+                    "id": "%s" % a,
+                    "dc:created": "",
+                    "dc:description": "",
+                    "dc:modified": ""
+                })
+        
+        self.views = [view]
+        
+    
+    def __parse_content(self):
+        
+        href = ""
+        meta_item_value = ""
+        if self.content.iri_id not in self.viewable_contents:
+            href = settings.FORBIDDEN_STREAM_URL
+        elif self.content.videopath:
+            href = self.content.videopath.rstrip('/') + "/" + self.content.src
+            meta_item_value = self.content.videopath.rstrip('/') + "/"
+        else:
+            href = self.content.src
+        
+        media = {
+             "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0",
+             "id" : self.content.iri_id,
+             "href" : href,
+             "unit" : "ms",
+             "origin" : "0",
+             "meta": {
+                 "dc:creator" : "",
+                 "dc:created" : self.content.creation_date.isoformat(),
+                 "dc:contributor" : "",
+                 "dc:modified" : self.content.update_date.isoformat(),
+                 "dc:creator.contents" : "",
+                 "dc:title" : self.content.title,
+                 "dc:description" : self.content.description,
+                 "dc:duration" : self.content.get_duration(),
+                 "item": {
+                     "name" : "streamer",
+                     "value": meta_item_value,
+                 },
+             }
+        }
+        
+        self.medias = [media]
+        
+    
+    def __parse_segments(self):
+        
+        self.annotations = []
+        
+        for seg in self.segments:
+            
+            segment = {
+                'begin': seg.start_ts,
+                'end': seg.start_ts + seg.duration,
+                'tags': seg.tags,
+                'id': "s_%s" % unicode(uuid.uuid1()),
+                'color': "%s" % self.default_color,
+                'media': self.content.iri_id,
+                'content': {
+                    'mimetype': 'application/x-ldt-structured',
+                    'description': seg.abstract,
+                    'img': {
+                        'src': ''
+                    },
+                    'title': seg.title,
+                    'color': '2194379',
+                    'polemics': [ ],
+                    'audio': {
+                        'mimetype': 'audio/mp3',
+                        'src': '',
+                        'href': 'null'
+                    }
+                
+                },
+                'meta': {
+                         "dc:creator": seg.author,
+                         "dc:contributor": seg.author,
+                         "dc:created": seg.date,
+                         "dc:modified": seg.date,
+                         "id-ref": seg.iri_id,
+                         "project": seg.project_id
+                         }
+            }
+            
+            self.annotations.append(segment)
+    
+    def serialize_to_cinelab(self):
+        
+        self.__parse_content()
+        self.__parse_segments()
+        self.__parse_views()
+        
+        res = {}
+        res['views'] = self.views 
+        res['tags'] = None
+        res['lists'] = None
+        res['medias'] = self.medias
+        res['annotations'] = self.annotations
+        res['annotation-types'] = self.annotation_types
+        
+        return res
+    
\ No newline at end of file
--- a/src/ldt/ldt/ldt_utils/stat.py	Wed Jan 18 10:53:49 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/stat.py	Wed Jan 18 15:30:35 2012 +0100
@@ -131,19 +131,19 @@
         contribution_project.nb_annotation += 1
     content.nb_annotation += 1
     
-    print "before segment creation"
-    s = Segment(project_obj=project,
-                content=content,
-                project_id=project.ldt_id,
-                iri_id=content.iri_id,
-                ensemble_id='%s' % unicode(uuid.uuid1()),
-                cutting_id='t',    
-                element_id='a',
-                duration=end-begin,
-                start_ts=begin)
-    s.save()
-    print s.id
-    print "segment created"
+#    print "before segment creation"
+#    s = Segment(project_obj=project,
+#                content=content,
+#                project_id=project.ldt_id,
+#                iri_id=content.iri_id,
+#                ensemble_id='%s' % unicode(uuid.uuid1()),
+#                cutting_id='t',    
+#                element_id='a',
+#                duration=end-begin,
+#                start_ts=begin)
+#    s.save()
+#    print s.id
+#    print "segment created"
     
     if contribution_project.stat: