Example #1
0
 def _decodeGETSUBSMessage(self, message):
     """
     From a bencoded GET_SUBS messages, returns its decoded contents.
     
     Decodes and checks for validity a bencoded GET_SUBS messages.
     If the message is succesfully decoded returns the tuple
     (channel_id,infohash,languages).
     
     channel_id is the binary identifier of the chanel that published
     the requested subtitles.
     infohash is the binary identifier of the torrent wich the subtitle
     refers to
     languages is a list of 3 characters language codes, for the languages
     of the requested subtitles
     
     @return: (channel_id,infohash,languages) or None if something is wrong
     """
     assert message[0] == GET_SUBS, SUBS_LOG_PREFIX + \
         "Invalid GET_SUBS Message header: %s" % message[0]
     
     try:
         values = bdecode(message[1:])
     except:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Error bdecoding message"
         return None
     
     if len(values) != 3:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid number of fields in GET_SUBS"
         return None
     channel_id, infohash, bitmask = values[0], values[1], values[2]
     if not validPermid(channel_id):
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid channel_id in GET_SUBS"
         return None
     elif not validInfohash(infohash):
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid infohash in GET_SUBS"
         return None
     elif not isinstance(bitmask, str) or not len(bitmask)==4:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid bitmask in GET_SUBS"
         return None
     
     try:
         bitmask = binaryStringToUint(bitmask)
         languages = self._languagesUtility.maskToLangCodes(bitmask)
     except:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid bitmask in GET_SUBS"
         return None
     
     return channel_id, infohash, languages
Example #2
0
def getSubtitleFileRelativeName(channel_id, infohash, langCode):
    # subtitles filenames are build from the sha1 hash
    # of the triple (channel_id, infohash, langCode)

    # channel_id and infohash are binary versions

    assert utilities.validPermid(channel_id), "Invalid channel_id %s" % utilities.show_permid_short(channel_id)
    assert utilities.validInfohash(infohash), "Invalid infohash %s" % bin2str(infohash)
    assert LanguagesProvider.getLanguagesInstance().isLangCodeSupported(langCode), (
        "Unsupported language code %s" % langCode
    )

    hasher = sha()
    for data in (channel_id, infohash, langCode):
        hasher.update(data)
    subtitleName = hasher.hexdigest() + SUBS_EXTENSION

    return subtitleName
Example #3
0
 def _decodeSUBSMessage(self, message):
     """
     From a bencoded SUBS message, returns its decoded contents.
     
     Decodes and checks for validity a bencoded SUBS message.
     If the message is succesfully decoded returns the tuple
     (channel_id, infohash, bitmask, contentsDictionary )
     
     channel_id is the binary identifier of the chanel that published
     the requested subtitles.
     infohash is the binary identifier of the torrent wich the subtitle
     refers to
     contentsDictionary is a dictionary having each entry like 
     {langCode : subtitleContents}.
     
     @return: the above described tuple, or None if something is wrong
     """
     assert message[0] == SUBS, SUBS_LOG_PREFIX + \
         "Invalid SUBS Message header: %s" % message[0]
         
     try:
         values = bdecode(message[1:])
     except:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Error bdecoding SUBS message"
         return None
     
     if len(values) != 4:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid number of fields in SUBS"
         return None
     channel_id, infohash, bitmask, contents = values[0], values[1], \
         values[2], values[3]
     
     if not validPermid(channel_id):
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid channel_id in SUBS"
         return None
     elif not validInfohash(infohash):
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid infohash in SUBS"
         return None
     elif not isinstance(bitmask, str) or not len(bitmask) == 4:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid bitmask in SUBS"
         return None
     
     try:
         bitmask = binaryStringToUint(bitmask)
         languages = self._languagesUtility.maskToLangCodes(bitmask)
     except:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid bitmask in SUBS"
         return None
     
     if not isinstance(contents, list):
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Invalid contents in SUBS"
         return None
     if len(languages) != len(contents):
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Bitmask and contents do not match in"\
                   " SUBS"
         return None
     
     numOfContents = len(languages)
     if numOfContents == 0:
         if DEBUG:
             print >> sys.stderr, SUBS_LOG_PREFIX + "Empty message. Discarding."
         return None
     
     
     contentsDictionary = dict()
     for i in range(numOfContents):
         lang = languages[i]
         subtitle = contents[i]
         if not isinstance(subtitle,unicode):
             try:
                 subtitle = unicode(subtitle)
             except:
                 return None
         if len(subtitle) <= self._maxSubSize:
             contentsDictionary[lang] = subtitle
         else:
             #drop that subtitle
             continue
         
     bitmask = self._languagesUtility.langCodesToMask(contentsDictionary.keys())
         
     
     return channel_id, infohash, bitmask, contentsDictionary