示例#1
0
    def get_url(self):
        """ Returns the URL representation of this TorrentDef. The TorrentDef
        must be a Merkle or live torrent and must be set to URL-compatible 
        before finalizing."""

        if self.metainfo_valid:
            return makeurl.metainfo2p2purl(self.metainfo)
        else:
            raise TorrentDefNotFinalizedException()
 def get_url(self):
     """ Returns the URL representation of this TorrentDef. The TorrentDef
     must be a Merkle or live torrent and must be set to URL-compatible 
     before finalizing."""
     
     if self.metainfo_valid:
         return makeurl.metainfo2p2purl(self.metainfo)
     else:
         raise TorrentDefNotFinalizedException()
示例#3
0
    def finalize(self, userabortflag=None, userprogresscallback=None):
        """ Create BT torrent file by reading the files added with
        add_content() and calculate the torrent file's infohash. 
        
        Creating the torrent file can take a long time and will be carried out
        by the calling thread. The process can be made interruptable by passing 
        a threading.Event() object via the userabortflag and setting it when 
        the process should be aborted. The also optional userprogresscallback 
        will be called by the calling thread periodically, with a progress 
        percentage as argument.
        
        The userprogresscallback function will be called by the calling thread. 
        
        @param userabortflag threading.Event() object
        @param userprogresscallback Function accepting a fraction as first
        argument. 
        """
        if self.readonly:
            raise OperationNotPossibleAtRuntimeException()

        if self.metainfo_valid:
            return

        if 'live' in self.input:
            # Make sure the duration is an integral number of pieces, for
            # security (live source auth).
            secs = parse_playtime_to_secs(self.input['playtime'])
            pl = float(self.get_piece_length())
            length = float(self.input['bps'] * secs)

            print >> sys.stderr, "TorrentDef: finalize", length, pl
            diff = length % pl
            add = (pl - diff) % pl
            newlen = int(length + add)

            #print >>sys.stderr,"CHECK INFO LENGTH",secs,newlen

            d = self.input['files'][0]
            d['length'] = newlen

        # Note: reading of all files and calc of hashes is done by calling
        # thread.

        (infohash, metainfo) = maketorrent.make_torrent_file(
            self.input,
            userabortflag=userabortflag,
            userprogresscallback=userprogresscallback)
        if infohash is not None:

            if self.get_url_compat():
                url = makeurl.metainfo2p2purl(metainfo)
                # Make sure metainfo is preserved, in particular, the url-compat field.
                swarmid = makeurl.metainfo2swarmid(metainfo)
                self.infohash = swarmid
            else:
                self.infohash = infohash
            self.metainfo = metainfo

            self.input['name'] = metainfo['info']['name']
            # May have been 0, meaning auto.
            self.input['piece length'] = metainfo['info']['piece length']
            self.metainfo_valid = True
    def finalize(self,userabortflag=None,userprogresscallback=None):
        """ Create BT torrent file by reading the files added with
        add_content() and calculate the torrent file's infohash. 
        
        Creating the torrent file can take a long time and will be carried out
        by the calling thread. The process can be made interruptable by passing 
        a threading.Event() object via the userabortflag and setting it when 
        the process should be aborted. The also optional userprogresscallback 
        will be called by the calling thread periodically, with a progress 
        percentage as argument.
        
        The userprogresscallback function will be called by the calling thread. 
        
        @param userabortflag threading.Event() object
        @param userprogresscallback Function accepting a fraction as first
        argument. 
        """
        if self.readonly:
            raise OperationNotPossibleAtRuntimeException()
        
        if self.metainfo_valid:
            return

        if 'live' in self.input:
            # Make sure the duration is an integral number of pieces, for
            # security (live source auth).
            secs = parse_playtime_to_secs(self.input['playtime'])
            pl = float(self.get_piece_length())
            length = float(self.input['bps']*secs)

            print >>sys.stderr,"TorrentDef: finalize",length,pl
            diff = length % pl
            add = (pl - diff) % pl
            newlen = int(length + add)

                
            #print >>sys.stderr,"CHECK INFO LENGTH",secs,newlen

            d = self.input['files'][0]
            d['length'] = newlen


        # Note: reading of all files and calc of hashes is done by calling 
        # thread.
        
        (infohash,metainfo) = maketorrent.make_torrent_file(self.input,userabortflag=userabortflag,userprogresscallback=userprogresscallback)
        if infohash is not None:
            
            if self.get_url_compat():
                url = makeurl.metainfo2p2purl(metainfo)
                # Make sure metainfo is preserved, in particular, the url-compat field.
                swarmid = makeurl.metainfo2swarmid(metainfo) 
                self.infohash = swarmid
            else:
                self.infohash = infohash
            self.metainfo = metainfo
            
            self.input['name'] = metainfo['info']['name']
            # May have been 0, meaning auto.
            self.input['piece length'] = metainfo['info']['piece length']
            self.metainfo_valid = True