web/blinkster/ldt/fileimport.py
changeset 29 cc9b7e14412b
parent 3 526ebd3988b0
child 35 8b65c9054eac
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
     1 import tempfile
     1 import tempfile
     2 import os
       
     3 import os.path
     2 import os.path
     4 import shutil
     3 import shutil
     5 from blinkster.utils import zipfileext
     4 from blinkster.utils import zipfileext
     6 import blinkster.utils.log
     5 import blinkster.utils.log
     7 import blinkster.utils.xml
     6 import blinkster.utils.xml
     8 from blinkster import settings
     7 from blinkster import settings
     9 from blinkster.ldt.models import Content, LdtProject
     8 from blinkster.ldt.models import Content, LdtProject
    10 import xml
     9 import Ft
    11 import xml.dom
       
    12 import xml.dom.minidom
    10 import xml.dom.minidom
    13 import xml.dom.ext
    11 import xml.dom.ext
    14 import xml.xpath
    12 import xml.xpath
    15 import fnmatch
    13 import fnmatch
    16 import uuid
       
    17 import shutil
       
    18 import lucene
    14 import lucene
    19 import uuid
    15 import uuid
    20 from blinkster.ldt import STORE
    16 from blinkster.ldt import STORE
    21 from blinkster.ldt import ANALYZER
    17 from blinkster.ldt import ANALYZER
    22 from blinkster.ldt.contentindexer import ContentIndexer
    18 from blinkster.ldt.contentindexer import ContentIndexer
    35         self.annotations = None
    31         self.annotations = None
    36         self.videopath = settings.STREAM_URL
    32         self.videopath = settings.STREAM_URL
    37         self.videourl = ""
    33         self.videourl = ""
    38         self.title = None
    34         self.title = None
    39         self.desc = None
    35         self.desc = None
       
    36         self.duration = None
    40         self.decoupageId = None
    37         self.decoupageId = None
    41         self.ensembleId = None
    38         self.ensembleId = None
    42         self.created = False
    39         self.created = False
    43         self.content = None
    40         self.content = None
    44         self.decoupage_blacklist = decoupage_blacklist
    41         self.decoupage_blacklist = decoupage_blacklist
    49     
    46     
    50     def processIri(self):
    47     def processIri(self):
    51         
    48         
    52         path = os.path.join(self.basepath, self.src)
    49         path = os.path.join(self.basepath, self.src)
    53         doc = xml.dom.minidom.parse(path)
    50         doc = xml.dom.minidom.parse(path)
       
    51         doc = Ft.Xml.Domlette.ConvertDocument(doc)        
    54         
    52         
    55         con = xml.xpath.Context.Context(doc, 1, 1, None)
    53         con = xml.xpath.Context.Context(doc, 1, 1, None)
    56         
    54         
    57         res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con)
    55         res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con)
    58         self.title = res[0].value
    56         self.title = res[0].value
    61         ensemblesnode = res[0]
    59         ensemblesnode = res[0]
    62 
    60 
    63         if self.annotations is not None:
    61         if self.annotations is not None:
    64             newensemble = doc.createElement('ensemble')
    62             newensemble = doc.createElement('ensemble')
    65             self.ensembleId = self.id+"_"+str(uuid.uuid1())
    63             self.ensembleId = self.id+"_"+str(uuid.uuid1())
    66             newensemble.setAttribute('id',self.ensembleId)
    64             newensemble.setAttributeNS(None,'id',self.ensembleId)
    67         
    65         
    68             newensemble.setAttribute('title', self.annotations.getAttribute('title'))
    66             newensemble.setAttributeNS(None,'title', self.annotations.getAttributeNS(None,'title'))
    69             newensemble.setAttribute('author', self.annotations.getAttribute('author'))
    67             newensemble.setAttributeNS(None,'author', self.annotations.getAttribute('author'))
    70             newensemble.setAttribute('date', self.annotations.getAttribute('date'))
    68             newensemble.setAttributeNS(None,'date', self.annotations.getAttribute('date'))
    71             newensemble.setAttribute('abstract', self.annotations.getAttribute('abstract'))
    69             newensemble.setAttributeNS(None,'abstract', self.annotations.getAttribute('abstract'))
    72             
    70             
    73             decoupageNode = None
    71             decoupageNode = None
    74             for node in self.annotations.childNodes:
    72             for node in self.annotations.childNodes:
    75                 if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "decoupage":
    73                 if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "decoupage":
    76                     decoupageNode = node
    74                     decoupageNode = node
    83             ensemblesnode.appendChild(newensemble)
    81             ensemblesnode.appendChild(newensemble)
    84         else:
    82         else:
    85             # find an ensemble child node
    83             # find an ensemble child node
    86             ensemble = None
    84             ensemble = None
    87             for node in ensemblesnode.childNodes:
    85             for node in ensemblesnode.childNodes:
    88                  if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble":
    86                 if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble":
    89                      ensemble = node
    87                     ensemble = node
    90                      break
    88                     break
    91             if ensemble is not None:
    89             if ensemble is not None:
    92                 self.ensembleId = ensemble.getAttribute(u"id")
    90                 self.ensembleId = ensemble.getAttributeNS(None,u"id")
    93                 decoupageNode = None
    91                 decoupageNode = None
    94                 for node in ensemble.childNodes:
    92                 for node in ensemble.childNodes:
    95                     if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "decoupage":
    93                     if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "decoupage":
    96                         decoupageNode = node
    94                         decoupageNode = node
    97                         break
    95                         break
    98                 if not decoupageNode is None:
    96                 if not decoupageNode is None:
    99                     self.decoupageId = decoupageNode.getAttribute(u"id")
    97                     self.decoupageId = decoupageNode.getAttributeNS(None,u"id")
   100                 
    98                 
   101         
    99         
   102         res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con)
   100         res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con)
   103         src_video = res[0].getAttribute('src')
   101         src_video = res[0].getAttributeNS(None,'src')
   104         self.videourl = os.path.basename(src_video)
   102         self.videourl = os.path.basename(src_video)
   105         srcnode = res[0].setAttribute('src', self.videourl)
   103         self.duration = res[0].getAttributeNS(None, 'dur')
   106         
   104         
   107         f = open(path, "w")
   105         f = open(path, "w")
   108         try:
   106         try:
   109             xml.dom.ext.Print(doc, stream=f)
   107             xml.dom.ext.Print(doc, stream=f)
   110         finally:
   108         finally:
   123         content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = { 'iri' : self.id + u"/" + os.path.basename(self.src), 'title':self.title, 'description':self.desc, })#'decoupage_id': self.decoupageId, 'ensemble_id': self.ensembleId});
   121         content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = { 'iri' : self.id + u"/" + os.path.basename(self.src), 'title':self.title, 'description':self.desc, })#'decoupage_id': self.decoupageId, 'ensemble_id': self.ensembleId});
   124         if not self.created:
   122         if not self.created:
   125             content.iri = self.id + u"/" + os.path.basename(self.src)
   123             content.iri = self.id + u"/" + os.path.basename(self.src)
   126             content.title = self.title
   124             content.title = self.title
   127             content.description = self.desc
   125             content.description = self.desc
       
   126             content.duration = int(self.duration)
   128             #content.decoupage_id = unicode(self.decoupageId)
   127             #content.decoupage_id = unicode(self.decoupageId)
   129             #content.ensemble_id = unicode(self.ensembleId)
   128             #content.ensemble_id = unicode(self.ensembleId)
   130             content.save()
   129             content.save()
   131         self.content = content
   130         self.content = content
   132             
   131             
   143 
   142 
   144     def __init__(self, filepath, author):
   143     def __init__(self, filepath, author):
   145         self.__filepath = filepath
   144         self.__filepath = filepath
   146         self.__tempdir = ""
   145         self.__tempdir = ""
   147         self.__author = author
   146         self.__author = author
   148         self.__writer = lucene.IndexModifier(STORE, ANALYZER, True)
   147         self.__writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED)
   149         self.__writer.setMaxFieldLength(1048576)
   148         self.__writer.setMaxFieldLength(1048576)
   150 
   149 
   151 
   150 
   152         
   151         
   153     def filepath(): #@NoSelf
   152     def filepath(): #@NoSelf
   180            
   179            
   181         return locals()
   180         return locals()
   182 
   181 
   183     author = property(**author())
   182     author = property(**author())
   184     
   183     
   185     def writer():
   184     def writer():            
   186             
       
   187         def fget(self):
   185         def fget(self):
   188             return self.__writer
   186             return self.__writer
   189                     
   187                     
   190         return locals()
   188         return locals()
   191         
   189         
   227         # inject comment in iri
   225         # inject comment in iri
   228         # copy iri in folder
   226         # copy iri in folder
   229         # create or update content
   227         # create or update content
   230         contents = {}
   228         contents = {}
   231         doc = xml.dom.minidom.parse(ldtpath)
   229         doc = xml.dom.minidom.parse(ldtpath)
       
   230         doc = Ft.Xml.Domlette.ConvertDocument(doc)
   232         
   231         
   233         con = xml.xpath.Context.Context(doc, 1, 1, None)
   232         con = xml.xpath.Context.Context(doc, 1, 1, None)
   234         result = xml.xpath.Evaluate("/iri/project", context=con)
   233         result = xml.xpath.Evaluate("/iri/project", context=con)
   235         for pnode in result:
   234         for pnode in result:
   236            author = pnode.getAttribute("user")
   235             #author = pnode.attributes["user"].value
   237            if author:
   236             if pnode.hasAttributeNS(None,"user"):
   238                self.author = unicode(author)
   237                 self.author = unicode(pnode.getAttributeNS(None, "user"))
   239                break 
   238                 break 
   240 
   239 
   241         result = xml.xpath.Evaluate("/iri/medias/media", context=con)
   240         result = xml.xpath.Evaluate("/iri/medias/media", context=con)
   242         
   241         
   243         for i, medianode in  enumerate(result):
   242         for i, medianode in  enumerate(result):
   244             id = medianode.attributes['id'].value
   243             id = medianode.getAttributeNS(None,'id')
   245             blinkster.utils.log.debug("FIRST ID : " + str(id))
   244             blinkster.utils.log.debug("FIRST ID : " + str(id))
   246             if self.checkExistingMedia:
   245             if self.checkExistingMedia:
   247                 try:
   246                 try:
   248                     Content.objects.get(iri_id=id)
   247                     Content.objects.get(iri_id=id)
   249                     do_pass = True
   248                     do_pass = True
   253                     do_pass = False
   252                     do_pass = False
   254             if not do_pass:
   253             if not do_pass:
   255                 if not (contents.has_key(id)):
   254                 if not (contents.has_key(id)):
   256                     blinkster.utils.log.debug("CREATE IRI INFO ID : " + str(id))
   255                     blinkster.utils.log.debug("CREATE IRI INFO ID : " + str(id))
   257                     contents[id] = IriInfo(id, os.path.dirname(ldtpath), i, "")
   256                     contents[id] = IriInfo(id, os.path.dirname(ldtpath), i, "")
   258                 contents[id].src = medianode.attributes['src'].value
   257                 contents[id].src = medianode.getAttributeNS(None,'src')
   259         
   258         
   260         result = xml.xpath.Evaluate("/iri/annotations/content", context=con)
   259         result = xml.xpath.Evaluate("/iri/annotations/content", context=con)
   261 
   260 
   262         blinkster.utils.log.debug("content xpath result : " + str(len(result)))        
   261         blinkster.utils.log.debug("content xpath result : " + str(len(result)))        
   263         for contentnode in result:
   262         for contentnode in result:
   264             id = contentnode.attributes['id'].value
   263             id = contentnode.getAttributeNS(None,'id')
   265             blinkster.utils.log.debug("ID : " + str(id))
   264             blinkster.utils.log.debug("ID : " + str(id))
   266             if contents.has_key(id):
   265             if contents.has_key(id):
   267                 if self.author:
   266                 if self.author:
   268                     contentnode.setAttribute("author", unicode(self.author))
   267                     contentnode.setAttributeNS(None,"author", unicode(self.author))
   269                 contents[id].annotations = contentnode
   268                 contents[id].annotations = contentnode
   270         
   269         
   271         #go throught values
   270         #go throught values
   272         for iriinfo in contents.values():
   271         for iriinfo in contents.values():
   273             
   272             
   332         doc = xml.dom.minidom.parse(self.filepath)
   331         doc = xml.dom.minidom.parse(self.filepath)
   333         
   332         
   334         con = xml.xpath.Context.Context(doc, 1, 1, None)
   333         con = xml.xpath.Context.Context(doc, 1, 1, None)
   335         result = xml.xpath.Evaluate("/iri/project", context=con)
   334         result = xml.xpath.Evaluate("/iri/project", context=con)
   336         for pnode in result:
   335         for pnode in result:
   337            author = pnode.setAttribute("user", unicode(self.author))
   336             pnode.setAttribute("user", unicode(self.author))
   338            break
   337             break
   339  
   338  
   340         result = xml.xpath.Evaluate("/iri/medias/media", context=con)
   339         result = xml.xpath.Evaluate("/iri/medias/media", context=con)
   341         
   340         
   342         for i, medianode in  enumerate(result):
   341         for i, medianode in  enumerate(result):
   343             id = medianode.attributes['id'].value
   342             id = medianode.attributes['id'].value
   344             blinkster.utils.log.debug("FIRST ID : " + str(id))
   343             blinkster.utils.log.debug("FIRST ID at " + str(i) + " : " + str(id))
   345             content = Content.objects.get(iri_id = id)
   344             content = Content.objects.get(iri_id = id)
   346             self.contents.append(content)
   345             self.contents.append(content)
   347             
   346             
   348             medianode.setAttribute('src', content.iri_url_template())
   347             medianode.setAttribute('src', content.iri_url_template())
   349             medianode.setAttribute('video', "${stream_url}")
   348             medianode.setAttribute('video', "${stream_url}")