def statusFunc(self, dpflag=Event(), fractionDone=None, timeEst=None, downRate=None, upRate=None, activity=None, statistics=None, **kws): # Want to be able to query a few things between status calls self.lastStats['fracDone'] = fractionDone self.lastStats['timeEst'] = timeEst self.lastStats['downRate'] = downRate self.lastStats['upRate'] = upRate self.lastStats['activity'] = activity self.lastStats['numSeeds'] = statistics.numSeeds if statistics else None self.lastStats['numPeers'] = statistics.numPeers if statistics else None self.lastStats['downTotal']= statistics.downTotal if statistics else None self.lastStats['upTotal'] = statistics.upTotal if statistics else None try: if (RightNow() - self.lastUpdate) < self.minSecondsBetweenUpdates: return self.lastUpdate = RightNow() self.displayFunc(dpflag, fractionDone, timeEst, downRate, upRate, activity, statistics, **kws) finally: # Set this flag to let the caller know it's ready for the next update dpflag.set()
def finishedFunc(self): """ This function must rename the ".partial" function to the correct name """ self.finishTime = RightNow() LOGINFO('Download finished!') LOGINFO("Moving file") LOGINFO(" From: %s", self.savePath_temp) LOGINFO(" To: %s", self.savePath) shutil.move(self.savePath_temp, self.savePath) # Use caller-set function if it exists if self.hasCustomFunc('finishedFunc'): self.customCallbacks['finishedFunc']() if self.bt1dow: self.bt1dow.shutdown()
def setupTorrent(self, torrentFile, savePath=None): # Some things to reset on every setup operation self.lastStats = {} self.startTime = None self.finishTime = None self.dlFailed = False self.bt1dow = None self.response = None self.torrentSize = None self.torrentName = None self.savePath = None self.savePath_temp = None # Set torrent file, bail if it doesn't exist self.torrent = torrentFile self.torrentDNE = False if not self.torrent or not os.path.exists(self.torrent): LOGERROR('Attempted to setup TDM with non-existent torrent:') if self.torrent: LOGERROR('Torrent path: %s', self.torrent) self.torrentDNE = True return self.lastUpdate = RightNow() # Get some info about the torrent if not self.torrentDNE: self.response = get_response(self.torrent, '', self.errorFunc) self.torrentSize = self.response['info']['length'] self.torrentName = self.response['info']['name'] LOGINFO('Torrent name is: %s' % self.torrentName) LOGINFO('Torrent size is: %0.2f MB' % (self.torrentSize/float(MEGABYTE))) self.savePath = savePath if self.savePath is None: self.savePath = os.path.join(BTC_HOME_DIR, self.torrentName) self.savePath_temp = self.savePath + '.partial'
def notifyFinished(): print 'TorrentThread: Finished downloading at %s' % unixTimeToFormatStr(RightNow()) sys.stdout.flush()
def doTheDownloadThing(self): """ This was copied and modified directly from btdownloadheadless.py """ if self.disabled: LOGERROR('Attempted to start DL but DISABLE_TORRENT is True') return while 1: # Use this var to identify if we've started downloading self.startTime = RightNow() configdir = ConfigDir(self.cacheDir) defaultsToIgnore = ['responsefile', 'url', 'priority'] configdir.setDefaults(defaults, defaultsToIgnore) config = configdir.loadConfig() config['responsefile'] = self.torrent config['url'] = '' config['priority'] = '' config['saveas'] = self.savePath_temp config['save_options'] = 0 config['max_uploads'] = 0 config['max_files_open'] = 25 configdir.deleteOldCacheData(config['expire_cache_data']) myid = createPeerID() seed(myid) rawserver = RawServer( self.doneObj, config['timeout_check_interval'], config['timeout'], ipv6_enable = config['ipv6_enabled'], failfunc = self.failedFunc, errorfunc = self.errorFunc) upnp_type = UPnP_test(config['upnp_nat_access']) while True: try: listen_port = rawserver.find_and_bind( \ config['minport'], config['maxport'], config['bind'], ipv6_socket_style = config['ipv6_binds_v4'], upnp = upnp_type, randomizer = config['random_port']) break except socketerror, e: if upnp_type and e == UPnP_ERROR: LOGWARN('WARNING: COULD NOT FORWARD VIA UPnP') upnp_type = 0 continue LOGERROR("error: Couldn't listen - " + str(e)) self.failedFunc() return if not self.response: break infohash = sha(bencode(self.response['info'])).digest() LOGINFO('Downloading: %s', self.torrentName) curr,tot = [float(a)/MEGABYTE for a in self.fileProgress()] if curr == 0: LOGINFO('Starting new download') elif curr==tot: LOGINFO('Torrent already finished!') return else: LOGINFO('Picking up where left off at %0.0f of %0.0f MB' % (curr,tot)) self.bt1dow = BT1Download( self.statusFunc, self.finishedFunc, self.errorFunc, self.excFunc, self.doneObj, config, self.response, infohash, myid, rawserver, listen_port, configdir) if not self.bt1dow.saveAs(self.chooseFileFunc): break if not self.bt1dow.initFiles(old_style = True): break if not self.bt1dow.startEngine(): self.bt1dow.shutdown() break if self.nHashFailures >= self.killAfterNHashFails: self.bt1dow.shutdown() self.customCallbacks['errorFunc']('hashFail') break self.bt1dow.startRerequester() self.bt1dow.autoStats() if not self.bt1dow.am_I_finished(): self.statusFunc(activity = 'Connecting to peers') rawserver.listen_forever(self.bt1dow.getPortHandler()) self.statusFunc(activity = 'Shutting down') self.bt1dow.shutdown() break