Exemple #1
0
 def get_name(self):
     """ Returns the info['name'] field as raw string of bytes.
     @return String """
     if self.metainfo_valid:
         return self.input['name']  # string immutable
     else:
         raise TorrentDefNotFinalizedException()
Exemple #2
0
    def get_name_as_unicode(self):
        """ Returns the info['name'] field as Unicode string.
        @return Unicode string. """
        if not self.metainfo_valid:
            raise TorrentDefNotFinalizedException()

        if "name.utf-8" in self.metainfo["info"]:
            # There is an utf-8 encoded name.  We assume that it is
            # correctly encoded and use it normally
            try:
                return unicode(self.metainfo["info"]["name.utf-8"], "UTF-8")
            except UnicodeError:
                pass

        if "name" in self.metainfo["info"]:
            # Try to use the 'encoding' field.  If it exists, it
            # should contain something like 'utf-8'
            if "encoding" in self.metainfo:
                try:
                    return unicode(self.metainfo["info"]["name"], self.metainfo["encoding"])
                except UnicodeError:
                    pass
                except LookupError:
                    # Some encodings are not supported by python.  For
                    # instance, the MBCS codec which is used by
                    # Windows is not supported (Jan 2010)
                    pass

            # Try to convert the names in path to unicode, without
            # specifying the encoding
            try:
                return unicode(self.metainfo["info"]["name"])
            except UnicodeError:
                pass

            # Try to convert the names in path to unicode, assuming
            # that it was encoded as utf-8
            try:
                return unicode(self.metainfo["info"]["name"], "UTF-8")
            except UnicodeError:
                pass

            # Convert the names in path to unicode by replacing out
            # all characters that may -even remotely- cause problems
            # with the '?' character
            try:
                def filter_characters(name):
                    def filter_character(char):
                        if 0 < ord(char) < 128:
                            return char
                        else:
                            self._logger.debug("Bad character filter %s, isalnum? %s", ord(char), char.isalnum())
                            return u"?"
                    return u"".join([filter_character(char) for char in name])
                return unicode(filter_characters(self.metainfo["info"]["name"]))
            except UnicodeError:
                pass

        # We failed.  Returning an empty string
        return u""
Exemple #3
0
 def get_metainfo(self):
     """ Returns the torrent definition as a dictionary that follows the BT
     spec for torrent files.
     @return dict
     """
     if self.metainfo_valid:
         return self.metainfo
     else:
         raise TorrentDefNotFinalizedException()
Exemple #4
0
 def get_infohash(self):
     """ Returns the infohash of the torrent, for non-URL compatible
     torrents. Otherwise it returns the swarm identifier (either the root hash
     (Merkle torrents) or hash of the live-source authentication key.
     @return A string of length 20. """
     if self.metainfo_valid:
         return self.infohash
     else:
         raise TorrentDefNotFinalizedException()
Exemple #5
0
 def test_torrent_def_not_finalized(self):
     raise TorrentDefNotFinalizedException("TorrentDefNotFinalizedException")