def publishSubtitle(self, infohash, lang, pathToSrtSubtitle):
        channelid = bin2str(self.my_permid)
        base64infohash = bin2str(infohash)
        consinstent = self.channelcast_db.isItemInChannel(channelid, base64infohash)
        if not consinstent:
            msg = 'Infohash %s not found in my channel. Rejecting subtitle' % base64infohash
            if DEBUG:
                print >> sys.stderr, msg
            raise RichMetadataException(msg)
        try:
            filepath = self.subtitlesHandler.copyToSubtitlesFolder(pathToSrtSubtitle, self.my_permid, infohash, lang)
        except Exception as e:
            if DEBUG:
                print >> sys.stderr, 'Failed to read and copy subtitle to appropriate folder: %s' % str(e)

        metadataDTO = self.richMetadata_db.getMetadata(self.my_permid, infohash)
        if metadataDTO is None:
            metadataDTO = MetadataDTO(self.my_permid, infohash)
        else:
            metadataDTO.resetTimestamp()
        newSubtitle = SubtitleInfo(lang, filepath)
        if newSubtitle.subtitleExists():
            newSubtitle.computeChecksum()
        else:
            msg = 'Inconsistency found. The subtitle was not published'
            if DEBUG:
                print >> sys.stderr, msg
            raise RichMetadataException(msg)
        metadataDTO.addSubtitle(newSubtitle)
        metadataDTO.sign(self.my_keypair)
        self.richMetadata_db.insertMetadata(metadataDTO)
    def getMetadata(self, channel, infohash):
        query = QUERIES['SELECT METADATA']
        infohash = bin2str(infohash)
        channel = bin2str(channel)
        res = self._db.fetchall(query, (infohash, channel))
        if len(res) == 0:
            return
        if len(res) > 1:
            raise MetadataDBException('Metadata DB Constraint violated')
        metaTuple = res[0]
        subsDictionary = self._getAllSubtitlesByKey(metaTuple[0])
        publisher = str2bin(metaTuple[1])
        infohash = str2bin(metaTuple[2])
        timestamp = int(metaTuple[4])
        description = unicode(metaTuple[3])
        signature = str2bin(metaTuple[5])
        toReturn = MetadataDTO(publisher, infohash, timestamp, description, None, signature)
        for sub in subsDictionary.itervalues():
            toReturn.addSubtitle(sub)

        return toReturn
    def getMetadata(self, channel, infohash):
        query = QUERIES['SELECT METADATA']
        infohash = bin2str(infohash)
        channel = bin2str(channel)
        res = self._db.fetchall(query, (infohash, channel))
        if len(res) == 0:
            return
        if len(res) > 1:
            raise MetadataDBException('Metadata DB Constraint violated')
        metaTuple = res[0]
        subsDictionary = self._getAllSubtitlesByKey(metaTuple[0])
        publisher = str2bin(metaTuple[1])
        infohash = str2bin(metaTuple[2])
        timestamp = int(metaTuple[4])
        description = unicode(metaTuple[3])
        signature = str2bin(metaTuple[5])
        toReturn = MetadataDTO(publisher, infohash, timestamp, description,
                               None, signature)
        for sub in subsDictionary.itervalues():
            toReturn.addSubtitle(sub)

        return toReturn