def getAllMetadataForInfohash(self, infohash):
     strinfohash = bin2str(infohash)
     query = QUERIES['SELECT PUBLISHERS FROM INFOHASH']
     channels = self._db.fetchall(query, (strinfohash, ))
     return [
         self.getMetadata(str2bin(entry[0]), infohash) for entry in channels
     ]
    def _getAllSubtitlesByKey(self, metadataKey):
        query = QUERIES['SELECT SUBS FK ALL']
        results = self._db.fetchall(query, (metadataKey,))
        subsDict = {}
        for entry in results:
            subsDict[entry[1]] = SubtitleInfo(entry[1], entry[2], str2bin(entry[3]))

        return subsDict
    def getAllLocalSubtitles(self):
        query = QUERIES['SELECT SUBTITLES WITH PATH']
        res = self._db.fetchall(query)
        result = {}
        for entry in res:
            path = entry[1]
            lang = entry[2]
            checksum = str2bin(entry[3])
            channel = str2bin(entry[4])
            infohash = str2bin(entry[5])
            s = SubtitleInfo(lang, path, checksum)
            if channel not in result:
                result[channel] = {}
            if infohash not in result[channel]:
                result[channel][infohash] = []
            result[channel][infohash].append(s)

        return result
 def _getSubtitleByKey(self, metadata_fk, lang):
     query = QUERIES['SELECT SUBS FK ONE']
     res = self._db.fetchall(query, (metadata_fk, lang))
     if len(res) == 0:
         return None
     if len(res) == 1:
         checksum = str2bin(res[0][3])
         return SubtitleInfo(res[0][1], res[0][2], checksum)
     raise MetadataDBException('Metadata DB Constraint violeted!')
    def getAllLocalSubtitles(self):
        query = QUERIES['SELECT SUBTITLES WITH PATH']
        res = self._db.fetchall(query)
        result = {}
        for entry in res:
            path = entry[1]
            lang = entry[2]
            checksum = str2bin(entry[3])
            channel = str2bin(entry[4])
            infohash = str2bin(entry[5])
            s = SubtitleInfo(lang, path, checksum)
            if channel not in result:
                result[channel] = {}
            if infohash not in result[channel]:
                result[channel][infohash] = []
            result[channel][infohash].append(s)

        return result
 def _getSubtitleByKey(self, metadata_fk, lang):
     query = QUERIES['SELECT SUBS FK ONE']
     res = self._db.fetchall(query, (metadata_fk, lang))
     if len(res) == 0:
         return None
     if len(res) == 1:
         checksum = str2bin(res[0][3])
         return SubtitleInfo(res[0][1], res[0][2], checksum)
     raise MetadataDBException('Metadata DB Constraint violeted!')
    def _getAllSubtitlesByKey(self, metadataKey):
        query = QUERIES['SELECT SUBS FK ALL']
        results = self._db.fetchall(query, (metadataKey, ))
        subsDict = {}
        for entry in results:
            subsDict[entry[1]] = SubtitleInfo(entry[1], entry[2],
                                              str2bin(entry[3]))

        return subsDict
 def getSubtitle(self, channel, infohash, lang):
     query = QUERIES['SELECT SUBS JOIN HASH ONE']
     infohash = bin2str(infohash)
     channel = bin2str(channel)
     res = self._db.fetchall(query, (infohash, channel, lang))
     if len(res) == 0:
         return None
     if len(res) == 1:
         checksum = str2bin(res[0][3])
         return SubtitleInfo(res[0][1], res[0][2], checksum)
     raise MetadataDBException('Metadata DB Constraint violeted!')
 def getSubtitle(self, channel, infohash, lang):
     query = QUERIES['SELECT SUBS JOIN HASH ONE']
     infohash = bin2str(infohash)
     channel = bin2str(channel)
     res = self._db.fetchall(query, (infohash, channel, lang))
     if len(res) == 0:
         return None
     if len(res) == 1:
         checksum = str2bin(res[0][3])
         return SubtitleInfo(res[0][1], res[0][2], checksum)
     raise MetadataDBException('Metadata DB Constraint violeted!')
    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 getHaveEntries(self, channel, infohash):
        query = QUERIES['GET ALL HAVE MASK']
        channel = bin2str(channel)
        infohash = bin2str(infohash)
        res = self._db.fetchall(query, (channel, infohash))
        returnlist = list()
        for entry in res:
            peer_id = str2bin(entry[0])
            haveMask = entry[1]
            timestamp = entry[2]
            returnlist.append((peer_id, haveMask, timestamp))

        return returnlist
    def getHaveEntries(self, channel, infohash):
        query = QUERIES['GET ALL HAVE MASK']
        channel = bin2str(channel)
        infohash = bin2str(infohash)
        res = self._db.fetchall(query, (channel, infohash))
        returnlist = list()
        for entry in res:
            peer_id = str2bin(entry[0])
            haveMask = entry[1]
            timestamp = entry[2]
            returnlist.append((peer_id, haveMask, timestamp))

        return returnlist
    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 getLocalSubtitles(self, channel, infohash):
        query = QUERIES['SELECT SUBTITLES WITH PATH BY CHN INFO']
        channel = bin2str(channel)
        infohash = bin2str(infohash)
        res = self._db.fetchall(query, (channel, infohash))
        result = {}
        for entry in res:
            location = entry[0]
            language = entry[1]
            checksum = str2bin(entry[2])
            subInfo = SubtitleInfo(language, location, checksum)
            result[language] = subInfo

        return result
    def getLocalSubtitles(self, channel, infohash):
        query = QUERIES['SELECT SUBTITLES WITH PATH BY CHN INFO']
        channel = bin2str(channel)
        infohash = bin2str(infohash)
        res = self._db.fetchall(query, (channel, infohash))
        result = {}
        for entry in res:
            location = entry[0]
            language = entry[1]
            checksum = str2bin(entry[2])
            subInfo = SubtitleInfo(language, location, checksum)
            result[language] = subInfo

        return result
 def getAllMetadataForInfohash(self, infohash):
     strinfohash = bin2str(infohash)
     query = QUERIES['SELECT PUBLISHERS FROM INFOHASH']
     channels = self._db.fetchall(query, (strinfohash,))
     return [ self.getMetadata(str2bin(entry[0]), infohash) for entry in channels ]