⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btdownloadcurses.py

📁 BitTorrent(简称BT
💻 PY
📖 第 1 页 / 共 2 页
字号:
            numCopies = statistics['numCopies']            nextCopies = ', '.join(["%d:%.1f%%" % (a,int(b*1000)/10) for a,b in                    zip(xrange(numCopies+1, 1000), statistics['numCopyList'])])            if not self.done:                self.seedStatus = '%d seen now, plus %d distributed copies ' \                                  '(%s)' % (statistics['numSeeds'],                                         statistics['numCopies'], nextCopies)            else:                self.seedStatus = '%d distributed copies (next: %s)' % (                    statistics['numCopies'], nextCopies)            self.peerStatus = '%d seen now' % statistics['numPeers']        self.fieldwin.erase()        self.fieldwin.addnstr(0, 0, self.file, self.fieldw, curses.A_BOLD)        self.fieldwin.addnstr(1, 0, self.fileSize, self.fieldw)        self.fieldwin.addnstr(2, 0, self.downloadTo, self.fieldw)        if self.progress:            self.fieldwin.addnstr(3, 0, self.progress, self.fieldw, curses.A_BOLD)        self.fieldwin.addnstr(4, 0, self.status, self.fieldw)        self.fieldwin.addnstr(5, 0, self.downRate, self.fieldw)        self.fieldwin.addnstr(6, 0, self.upRate, self.fieldw)        self.fieldwin.addnstr(7, 0, self.shareRating, self.fieldw)        self.fieldwin.addnstr(8, 0, self.seedStatus, self.fieldw)        self.fieldwin.addnstr(9, 0, self.peerStatus, self.fieldw)        self.spewwin.erase()        if not spew:            errsize = self.spewh            if self.errors:                self.spewwin.addnstr(0, 0, "error(s):", self.speww, curses.A_BOLD)                errsize = len(self.errors)                displaysize = min(errsize, self.spewh)                displaytop = errsize - displaysize                for i in range(displaysize):                    self.spewwin.addnstr(i, self.labelw, self.errors[displaytop + i],                                 self.speww-self.labelw-1, curses.A_BOLD)        else:            if self.errors:                self.spewwin.addnstr(0, 0, "error:", self.speww, curses.A_BOLD)                self.spewwin.addnstr(0, self.labelw, self.errors[-1],                                 self.speww-self.labelw-1, curses.A_BOLD)            self.spewwin.addnstr(2, 0, "  #     IP                 Upload           Download     Completed  Speed", self.speww, curses.A_BOLD)            if self.spew_scroll_time + SPEW_SCROLL_RATE < time():                self.spew_scroll_time = time()                if len(spew) > self.spewh-5 or self.spew_scroll_pos > 0:                    self.spew_scroll_pos += 1            if self.spew_scroll_pos > len(spew):                self.spew_scroll_pos = 0            for i in range(len(spew)):                spew[i]['lineno'] = i+1            spew.append({'lineno': None})            spew = spew[self.spew_scroll_pos:] + spew[:self.spew_scroll_pos]            for i in range(min(self.spewh - 5, len(spew))):                if not spew[i]['lineno']:                    continue                self.spewwin.addnstr(i+3, 0, '%3d' % spew[i]['lineno'], 3)                self.spewwin.addnstr(i+3, 4, spew[i]['ip'], 15)                ul = spew[i]['upload']                if ul[1] > 100:                    self.spewwin.addnstr(i+3, 20, '%6.0f KB/s' % (                        ul[1] / 1000), 11)                self.spewwin.addnstr(i+3, 32, '-----', 5)                if ul[2]:                    self.spewwin.addnstr(i+3, 33, 'I', 1)                if ul[3]:                    self.spewwin.addnstr(i+3, 35, 'C', 1)                dl = spew[i]['download']                if dl[1] > 100:                    self.spewwin.addnstr(i+3, 38, '%6.0f KB/s' % (                        dl[1] / 1000), 11)                self.spewwin.addnstr(i+3, 50, '-------', 7)                if dl[2]:                    self.spewwin.addnstr(i+3, 51, 'I', 1)                if dl[3]:                    self.spewwin.addnstr(i+3, 53, 'C', 1)                if dl[4]:                    self.spewwin.addnstr(i+3, 55, 'S', 1)                self.spewwin.addnstr(i+3, 58, '%5.1f%%' % (int(spew[i]['completed']*1000)/10), 6)                if spew[i]['speed'] is not None:                    self.spewwin.addnstr(i+3, 64, '%5.0f KB/s' % (spew[i]['speed']/1000), 10)            self.spewwin.addnstr(self.spewh-1, 0,                    "downloading %d pieces, have %d fragments, "                    "%d of %d pieces completed" %                    (statistics['storage_active'], statistics['storage_dirty'],                     statistics['storage_numcomplete'], self.numpieces),                    self.speww-1)        curses.panel.update_panels()        curses.doupdate()class DL(Feedback):    def __init__(self, metainfo, config, errlist):        self.doneflag = threading.Event()        self.metainfo = metainfo        self.config = config        self.errlist = errlist    def run(self, scrwin):        def reread():            self.multitorrent.rawserver.external_add_task(self.reread_config,0)        self.d = CursesDisplayer(scrwin, self.errlist, self.doneflag, reread)        try:            self.multitorrent = Multitorrent(self.config, self.doneflag,                                             self.global_error)            # raises BTFailure if bad            metainfo = ConvertedMetainfo(bdecode(self.metainfo))            torrent_name = metainfo.name_fs            if config['save_as']:                if config['save_in']:                    raise BTFailure('You cannot specify both --save_as and '                                    '--save_in')                saveas = config['save_as']            elif config['save_in']:                saveas = os.path.join(config['save_in'], torrent_name)            else:                saveas = torrent_name            self.d.set_torrent_values(metainfo.name, os.path.abspath(saveas),                                metainfo.total_bytes, len(metainfo.hashes))            self.torrent = self.multitorrent.start_torrent(metainfo,                                self.config, self, saveas)        except BTFailure, e:            errlist.append(str(e))            return        self.get_status()        self.multitorrent.rawserver.listen_forever()        self.d.display({'activity':'shutting down', 'fractionDone':0})        self.torrent.shutdown()    def reread_config(self):        try:            newvalues = configfile.get_config(self.config, 'btdownloadcurses')        except Exception, e:            self.d.error('Error reading config: ' + str(e))            return        self.config.update(newvalues)        # The set_option call can potentially trigger something that kills        # the torrent (when writing this the only possibility is a change in        # max_files_open causing an IOError while closing files), and so        # the self.failed() callback can run during this loop.        for option, value in newvalues.iteritems():            self.multitorrent.set_option(option, value)        for option, value in newvalues.iteritems():            self.torrent.set_option(option, value)    def get_status(self):        self.multitorrent.rawserver.add_task(self.get_status,                                             self.config['display_interval'])        status = self.torrent.get_status(self.config['spew'])        self.d.display(status)    def global_error(self, level, text):        self.d.error(text)    def error(self, torrent, level, text):        self.d.error(text)    def failed(self, torrent, is_external):        self.doneflag.set()    def finished(self, torrent):        self.d.finished()if __name__ == '__main__':    uiname = 'btdownloadcurses'    defaults = get_defaults(uiname)    if len(sys.argv) <= 1:        printHelp(uiname, defaults)        sys.exit(1)    try:        config, args = configfile.parse_configuration_and_args(defaults,                                       uiname, sys.argv[1:], 0, 1)        if args:            if config['responsefile']:                raise BTFailure, 'must have responsefile as arg or ' \                      'parameter, not both'            config['responsefile'] = args[0]        try:            if config['responsefile']:                h = file(config['responsefile'], 'rb')                metainfo = h.read()                h.close()            elif config['url']:                h = urlopen(config['url'])                metainfo = h.read()                h.close()            else:                raise BTFailure('you need to specify a .torrent file')        except IOError, e:            raise BTFailure('Error reading .torrent file: ', str(e))    except BTFailure, e:        print str(e)        sys.exit(1)    errlist = []    dl = DL(metainfo, config, errlist)    curses_wrapper(dl.run)    if errlist:       print "These errors occurred during execution:"       for error in errlist:          print error

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -