예제 #1
0
def fromXML(xml):
    # FIXME: xml podria ser un file, o un string con un path.
    # Ojo si no existe.
    """
    Obter os datos do manifest, e despois do episode e o series
    """
    mp_uri = path.dirname(path.abspath(xml))
    mp = mediapackage.Mediapackage(uri=mp_uri)
    manifest = minidom.parse(xml)
    principal = manifest.getElementsByTagName("mediapackage")
    mp.setDuration(
        principal[0].getAttribute("duration")
    )  # FIXME check if empty and take out patch in listing.populatetreeview
    mp.setIdentifier(principal[0].getAttribute("id"))

    if principal[0].hasAttribute("start"):
        mp.setDate(
            datetime.strptime(principal[0].getAttribute("start"),
                              '%Y-%m-%dT%H:%M:%S'))

    without_galicaster = False

    try:
        galicaster = minidom.parse(path.join(mp.getURI(), 'galicaster.xml'))
        mp.status = int(_checknget(galicaster, "status"))
        for i in galicaster.getElementsByTagName("operation"):
            op = unicode(i.getAttribute("key"))
            status = _checknget(i, "status")
            mp.setOpStatus(op, int(status))
        for i in galicaster.getElementsByTagName("property"):
            op = unicode(i.getAttribute("name"))
            value = _checkget(i)
            mp.properties[op] = unicode(value)

    except IOError:
        print "WHITOUT galicaster.xml"
        without_galicaster = True

    for etype, tag in mediapackage.MANIFEST_TAGS.items():
        for i in manifest.getElementsByTagName(tag):
            if i.hasAttribute("id"):
                identifier = unicode(i.getAttribute("id"))
            else:
                identifier = None
            uri = _checknget(i, "url")
            flavor = unicode(i.getAttribute("type"))
            mime = _checknget(i, "mimetype")
            duration = _checknget(i, "duration")
            mp.add(_getElementAbsPath(uri, mp.getURI()), etype, flavor, mime,
                   duration, identifier)

            if uri == 'org.opencastproject.capture.agent.properties' and etype == mediapackage.TYPE_ATTACHMENT:
                mp.manual = False

            if etype == mediapackage.TYPE_TRACK and mp.status == mediapackage.NEW and without_galicaster:
                mp.status = mediapackage.RECORDED

    mp.marshalDublincore()
    return mp
예제 #2
0
def fromXML(xml, logger=None):
    # FIXME: xml podria ser un file, o un string con un path.
    # Ojo si no existe.
    """
    Obter os datos do manifest, e despois do episode e o series
    """
    mp_uri = path.dirname(path.abspath(xml))
    mp = mediapackage.Mediapackage(uri = mp_uri)
    manifest = minidom.parse(xml)          
    principal = manifest.getElementsByTagName("mediapackage")
    mp.setDuration(principal[0].getAttribute("duration")) # FIXME check if empty and take out patch in listing.populatetreeview   
    mp.setIdentifier(principal[0].getAttribute("id"))

    if principal[0].hasAttribute("start"):
        mp.setDate(datetime.strptime(principal[0].getAttribute("start"), '%Y-%m-%dT%H:%M:%S'))
   
    without_galicaster = False

    try:
        galicaster = minidom.parse(path.join(mp.getURI(), 'galicaster.xml'))
        mp.status = int(_checknget(galicaster, "status"))
        for i in galicaster.getElementsByTagName("operation"):
            op = unicode(i.getAttribute("key"))
            status = _checknget(i, "status")
            mp.setOpStatus(op, int(status))
        for i in galicaster.getElementsByTagName("property"):
            op = unicode(i.getAttribute("name"))
            value = _checkget(i)
            mp.properties[op] = unicode(value)
                
    except IOError:
        if logger:
            logger.debug("Mediapackage "+mp.identifier+" without galicaster.xml")
        without_galicaster = True

    
    for etype, tag in mediapackage.MANIFEST_TAGS.items(): 
        for i in manifest.getElementsByTagName(tag):
            if i.hasAttribute("id"):
                identifier = unicode(i.getAttribute("id"))
            else:
                identifier = None
            uri = _checknget(i, "url")
            flavor = unicode(i.getAttribute("type"))
            mime = _checknget(i, "mimetype")
            duration = _checknget(i, "duration")
            element_path = _getElementAbsPath(uri, mp.getURI())
            if not path.exists(element_path):
                raise IOError, "Not exists the element {} in the MP {}".format(element_path, mp.identifier)
            mp.add(element_path, etype, flavor, mime, duration, identifier)

            if uri == 'org.opencastproject.capture.agent.properties' and etype == mediapackage.TYPE_ATTACHMENT:
                mp.manual = False
                
            if etype == mediapackage.TYPE_TRACK and mp.status == mediapackage.NEW and without_galicaster:
                mp.status = mediapackage.RECORDED

    mp.marshalDublincore()
    return mp        
예제 #3
0
    def marshalDublincore(self):
        """
        Intarnal function to marshal mediapackage atributes and 
        metadata_episode dict with Dubincore episode file
        """
        for i in self.getCatalogs():
            if i.getFlavor() == "dublincore/episode":
                dom = minidom.parse(i.getURI())
                # retrive terms
                EPISODE_TERMS = []
                for node in dom.firstChild.childNodes:
                    if not node.nodeName.count('dcterms:'):
                        continue
                    EPISODE_TERMS.append(node.nodeName.split(':')[1])
                for name in EPISODE_TERMS:
                    if name in ["created"]:
                        creat = _checknget(dom, "dcterms:" + name)
                        if creat:
                            if creat[-1] == "Z":
                                self.setDate(
                                    datetime.strptime(creat,
                                                      "%Y-%m-%dT%H:%M:%SZ"))
                            else:
                                self.setDate(
                                    datetime.strptime(creat,
                                                      "%Y-%m-%dT%H:%M:%S"))
                                # parse erroneous format too
                    elif name in ['isPartOf', 'ispartof']:
                        new = _checknget(dom, "dcterms:" + name)
                        old = _checknget(dom, "dcterms:" + name.lower())
                        self.metadata_episode[
                            name] = new if new != None else old
                    else:
                        self.metadata_episode[name] = _checknget(
                            dom, "dcterms:" + name)
            elif i.getFlavor(
            ) == "dublincore/series":  # FIXME cover series data and create files if dont exist
                dom = minidom.parse(i.getURI())
                # retrive terms
                SERIES_TERMS = []
                for node in dom.firstChild.childNodes:
                    SERIES_TERMS.append(node.nodeName.split(':')[1])
                for name in SERIES_TERMS:
                    self.metadata_series[name] = _checknget(
                        dom, "dcterms:" + name)

        # Parse temporal metadatum
        if self.metadata_episode.has_key('temporal') and self.metadata_episode[
                'temporal'] and not self.hasTracks():
            try:
                g = re.search('start=(.*)Z; end=(.*)Z;',
                              self.metadata_episode['temporal'])
                start = datetime.strptime(g.group(1), "%Y-%m-%dT%H:%M:%S")
                stop = datetime.strptime(g.group(2), "%Y-%m-%dT%H:%M:%S")
                diff = stop - start
                self.setDuration(diff.seconds * 1000)
            except:
                pass
예제 #4
0
    def marshalDublincore(self):
        """
        Intarnal function to marshal mediapackage atributes and 
        metadata_episode dict with Dubincore episode file
        """
        for i in self.getCatalogs():
            if i.getFlavor() == "dublincore/episode":
                dom = minidom.parse(i.getURI())
                for name in DCTERMS:
                    if name in ["creator", "contributor",
                                "subject"]:  # FIXME do this to other metadata
                        creat = _checknget(
                            dom, "dcterms:" + name
                        )  # FIXME check Nones and empty string somewhere
                        if creat and creat not in self.metadata_episode[name]:
                            self.metadata_episode[name].append(creat)
                    else:
                        self.metadata_episode[name] = _checknget(
                            dom, "dcterms:" + name)
            elif i.getFlavor(
            ) == "dublincore/series":  # FIXME cover series data and create files if dont exist
                dom = minidom.parse(i.getURI())
                self.series_title = _checknget(dom, "dcterms:" + "title")
                self.series = _checknget(dom, "dcterms:" + "identifier")
                dom = minidom.parse(i.getURI())
                for name in DCTERMS:  # FIXME reduce it to series terms
                    self.metadata_series[name] = _checknget(
                        dom, "dcterms:" + name)

        #FIXME: Init set and attr
        #self.setTitle(self.metadata_episode['title']) # NOT NECESARY TITLE IS property
        self.setCreators(self.metadata_episode['creator']
                         )  # FIXME creators could be more than one
        self.setContributors(self.metadata_episode['contributor']
                             )  # FIXME creators could be more than one
        self.setSubjects(self.metadata_episode['subject']
                         )  # FIXME creators could be more than one

        if self.metadata_episode.has_key('temporal') and self.metadata_episode[
                'temporal'] and not self.hasTracks():
            try:
                g = re.search('start=(.*)Z; end=(.*)Z;',
                              self.metadata_episode['temporal'])
                start = datetime.strptime(g.group(1), "%Y-%m-%dT%H:%M:%S")
                stop = datetime.strptime(g.group(2), "%Y-%m-%dT%H:%M:%S")
                diff = stop - start
                self.setDuration(diff.seconds * 1000)
            except:
                pass
예제 #5
0
    def marshalDublincore(self):
        """
        Intarnal function to marshal mediapackage atributes and 
        metadata_episode dict with Dubincore episode file
        """
        for i in self.getCatalogs():               
            if i.getFlavor() == "dublincore/episode":
                dom = minidom.parse(i.getURI())                 
                # retrive terms
                EPISODE_TERMS = []
                for node in dom.firstChild.childNodes:
                    if not node.nodeName.count('dcterms:'):
                        continue
                    EPISODE_TERMS.append(node.nodeName.split(':')[1])    
                for name in EPISODE_TERMS:
                    if name in ["created"]:
                        creat = _checknget(dom, "dcterms:" + name)
                        if creat:
                            if creat[-1] == "Z":
                                self.setDate(datetime.strptime(creat, "%Y-%m-%dT%H:%M:%SZ"))
                            else:
                                self.setDate(datetime.strptime(creat, "%Y-%m-%dT%H:%M:%S")) 
                                # parse erroneous format too
                    elif name in ['isPartOf', 'ispartof']: 
                        new = _checknget(dom, "dcterms:"+name)
                        old = _checknget(dom, "dcterms:"+name.lower() )
                        self.metadata_episode[name] = new if new != None else old
                    else:
                        self.metadata_episode[name] = _checknget(dom, "dcterms:" + name)                     
            elif i.getFlavor() == "dublincore/series": # FIXME cover series data and create files if dont exist
                dom = minidom.parse(i.getURI())                 
                # retrive terms
                SERIES_TERMS = []
                for node in dom.firstChild.childNodes:
                    SERIES_TERMS.append(node.nodeName.split(':')[1])                
                for name in SERIES_TERMS:
                    self.metadata_series[name] = _checknget(dom, "dcterms:" + name)

        # Parse temporal metadatum
        if self.metadata_episode.has_key('temporal') and self.metadata_episode['temporal'] and not self.hasTracks():
            try:
                g = re.search('start=(.*)Z; end=(.*)Z;', self.metadata_episode['temporal'])
                start = datetime.strptime(g.group(1), "%Y-%m-%dT%H:%M:%S")
                stop =  datetime.strptime(g.group(2), "%Y-%m-%dT%H:%M:%S")
                diff = stop - start 
                self.setDuration(diff.seconds*1000)
                self.setDate(start)
            except:
                pass
예제 #6
0
    def marshalDublincore(self):
        """
        Intarnal function to marshal mediapackage atributes and 
        metadata_episode dict with Dubincore episode file
        """
        for i in self.getCatalogs():               
            if i.getFlavor() == "dublincore/episode":
                dom = minidom.parse(i.getURI()) 
                for name in DCTERMS:
                    if name in ["created"]:
                        creat = _checknget(dom, "dcterms:" + name)
                        if creat:
                            if creat[-1] == "Z":
                                self.setDate(datetime.strptime(creat, "%Y-%m-%dT%H:%M:%SZ"))
                            else:
                                self.setDate(datetime.strptime(creat, "%Y-%m-%dT%H:%M:%S"))
                    elif name in [ "creator", "contributor", "subject"]: # FIXME do this to other metadata
                        creat = _checknget(dom, "dcterms:" + name) # FIXME check Nones and empty string somewhere
                        if creat and creat not in self.metadata_episode[name]:
                            self.metadata_episode[name].append(creat)
                    elif name == 'isPartOf': 
                        new = _checknget(dom, "dcterms:"+name)
                        old = _checknget(dom, "dcterms:"+name.lower() )
                        self.metadata_episode[name] = new if new != None else old
                    else:
                        self.metadata_episode[name] = _checknget(dom, "dcterms:" + name)                     
            elif i.getFlavor() == "dublincore/series": # FIXME cover series data and create files if dont exist
                dom = minidom.parse(i.getURI())                 
                self.series_title = _checknget(dom, "dcterms:" + "title")
                self.series = _checknget(dom, "dcterms:" + "identifier") 
                dom = minidom.parse(i.getURI())
                for name in DCTERMS: # FIXME reduce it to series terms
                    self.metadata_series[name] = _checknget(dom, "dcterms:" + name)

        #FIXME: Init set and attr
        self.setCreators(self.metadata_episode['creator']) # FIXME creators could be more than one
        self.setContributors(self.metadata_episode['contributor']) # FIXME creators could be more than one
        self.setSubjects(self.metadata_episode['subject']) # FIXME creators could be more than one

        if self.metadata_episode.has_key('temporal') and self.metadata_episode['temporal'] and not self.hasTracks():
            try:
                g = re.search('start=(.*)Z; end=(.*)Z;', self.metadata_episode['temporal'])
                start = datetime.strptime(g.group(1), "%Y-%m-%dT%H:%M:%S")
                stop =  datetime.strptime(g.group(2), "%Y-%m-%dT%H:%M:%S")
                diff = stop - start 
                self.setDuration(diff.seconds*1000)
            except:
                pass
예제 #7
0
def fromXML(xml, logger=None):
    # FIXME: xml could be a file or a path.
    # TODO: if does not exist.
    """
    Gets information from manifest.xml by parsing it. Then tries to get information from galicaster.xml if the file exists.
    At the end calls marshalDublinCore(), a mediaPackage method in which the instance from the Mediapackage class gets the information from series.xml and episode.xml files by parsing them.
    Args:
        xml (str): absolute path of the manifest.xml file.
        logger (Logger): the object that prints all the information, warning and error messages. See galicaster/context/logger.
    Returns:
        Mediapackage: the object that represent a set of records.
    """
    mp_uri = path.dirname(path.abspath(xml))
    mp = mediapackage.Mediapackage(uri=mp_uri)
    manifest = minidom.parse(xml)
    principal = manifest.getElementsByTagName("mediapackage")
    mp.setDuration(
        principal[0].getAttribute("duration") or 0
    )  # FIXME check if empty and take out patch in listing.populatetreeview
    mp.setIdentifier(principal[0].getAttribute("id"))

    if principal[0].hasAttribute("start"):
        mp.setDate(
            datetime.strptime(principal[0].getAttribute("start"),
                              '%Y-%m-%dT%H:%M:%S'))

    without_galicaster = False

    galicaster_json = False

    try:
        with open(path.join(mp.getURI(), 'galicaster.json')) as file:
            galicaster_json = json.load(file)
    except IOError:
        if logger:
            logger.warning(
                "The Mediapackage: " + mp.identifier +
                " : has no galicaster.json file. Trying to load outdated galicaster.xml"
            )
        # Keep The XML read logic for backwards compatibility:
        try:
            galicaster = minidom.parse(path.join(mp.getURI(),
                                                 'galicaster.xml'))

            mp.status = int(_checknget(galicaster, "status"))
            for i in galicaster.getElementsByTagName("operation"):
                op = str(i.getAttribute("key"))
                status = _checknget(i, "status")
                mp.setOpStatus(op, int(status))
            for i in galicaster.getElementsByTagName("property"):
                op = str(i.getAttribute("name"))
                value = _checkget(i)
                mp.properties[op] = str(value)
        except IOError:
            if logger:
                logger.error(
                    "The Mediapackage: " + mp.identifier +
                    " : has no galicaster.xml or galicaster.json file")
            without_galicaster = True

    if galicaster_json and 'galicaster' in galicaster_json:
        galicaster_json = galicaster_json['galicaster']
        mp.status = galicaster_json['status']
        mp.operations = galicaster_json['operations']
        mp.properties = galicaster_json['properties']

    for etype, tag in list(mediapackage.MANIFEST_TAGS.items()):
        for i in manifest.getElementsByTagName(tag):
            if i.hasAttribute("id"):
                identifier = str(i.getAttribute("id"))
            else:
                identifier = None
            uri = _checknget(i, "url")
            flavor = str(i.getAttribute("type"))
            mime = _checknget(i, "mimetype")
            duration = _checknget(i, "duration")
            element_path = _getElementAbsPath(uri, mp.getURI())
            ref = str(i.getAttribute("ref"))

            tags = []
            for tag_elem in i.getElementsByTagName("tags"):
                tag_text = str(_checknget(tag_elem, "tag"))
                tags.append(tag_text)

            if i.hasAttribute("ref"):
                ref = str(i.getAttribute("ref"))
            else:
                ref = None
            if not path.exists(element_path):
                raise IOError("Not exists the element {} in the MP {}".format(
                    element_path, mp.identifier))
            mp.add(element_path, etype, flavor, mime, duration, ref,
                   identifier, tags)

            if uri == 'org.opencastproject.capture.agent.properties' and etype == mediapackage.TYPE_ATTACHMENT:
                mp.manual = False

            if etype == mediapackage.TYPE_TRACK and mp.status == mediapackage.NEW and without_galicaster:
                mp.status = mediapackage.RECORDED

    mp.marshalDublincore()

    # To rewrite the galicaster.xml file with a galicaster.json file
    if not without_galicaster and not galicaster_json:
        from galicaster.mediapackage import serializer
        serializer.save_in_dir(mp)

    return mp
예제 #8
0
def fromXML(xml, logger=None):
    # FIXME: xml could be a file or a path.
    # TODO: if does not exist.
    """
    Gets information from manifest.xml by parsing it. Then tries to get information from galicaster.xml if the file exists.
    At the end calls marshalDublinCore(), a mediaPackage method in which the instance from the Mediapackage class gets the information from series.xml and episode.xml files by parsing them.
    Args:
        xml (str): absolute path of the manifest.xml file.
        logger (Logger): the object that prints all the information, warning and error messages. See galicaster/context/logger.
    Returns:
        Mediapackage: the object that represent a set of records.
    """
    mp_uri = path.dirname(path.abspath(xml))
    mp = mediapackage.Mediapackage(uri = mp_uri)
    manifest = minidom.parse(xml)          
    principal = manifest.getElementsByTagName("mediapackage")
    mp.setDuration(principal[0].getAttribute("duration") or 0) # FIXME check if empty and take out patch in listing.populatetreeview   
    mp.setIdentifier(principal[0].getAttribute("id"))

    if principal[0].hasAttribute("start"):
        mp.setDate(datetime.strptime(principal[0].getAttribute("start"), '%Y-%m-%dT%H:%M:%S'))
   
    without_galicaster = False

    try:
        galicaster = minidom.parse(path.join(mp.getURI(), 'galicaster.xml'))
        mp.status = int(_checknget(galicaster, "status"))
        for i in galicaster.getElementsByTagName("operation"):
            op = unicode(i.getAttribute("key"))
            status = _checknget(i, "status")
            mp.setOpStatus(op, int(status))
        for i in galicaster.getElementsByTagName("property"):
            op = unicode(i.getAttribute("name"))
            value = _checkget(i)
            mp.properties[op] = unicode(value)
                
    except IOError:
        if logger:
            logger.debug("Mediapackage "+mp.identifier+" without galicaster.xml")
        without_galicaster = True

    
    for etype, tag in mediapackage.MANIFEST_TAGS.items(): 
        for i in manifest.getElementsByTagName(tag):
            if i.hasAttribute("id"):
                identifier = unicode(i.getAttribute("id"))
            else:
                identifier = None
            uri = _checknget(i, "url")
            flavor = unicode(i.getAttribute("type"))
            mime = _checknget(i, "mimetype")
            duration = _checknget(i, "duration")
            element_path = _getElementAbsPath(uri, mp.getURI())
            ref = unicode(i.getAttribute("ref"))
            
            tags = []
            for tag_elem in i.getElementsByTagName("tags"):
                tag_text = unicode(_checknget(tag_elem, "tag"))
                tags.append(tag_text)

            if i.hasAttribute("ref"):
            	ref = unicode(i.getAttribute("ref"))
            else:
                ref = None
            if not path.exists(element_path):
                raise IOError, "Not exists the element {} in the MP {}".format(element_path, mp.identifier)
            mp.add(element_path, etype, flavor, mime, duration, ref, identifier, tags)

            if uri == 'org.opencastproject.capture.agent.properties' and etype == mediapackage.TYPE_ATTACHMENT:
                mp.manual = False
                
            if etype == mediapackage.TYPE_TRACK and mp.status == mediapackage.NEW and without_galicaster:
                mp.status = mediapackage.RECORDED

    mp.marshalDublincore()
    return mp