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

📄 oldtorrentqueue.py

📁 bittorrent source by python. please enjoy
💻 PY
📖 第 1 页 / 共 3 页
字号:
# The contents of this file are subject to the BitTorrent Open Source License# Version 1.1 (the License).  You may not copy or use this file, in either# source code or executable form, except in compliance with the License.  You# may obtain a copy of the License at http://www.bittorrent.com/license/.## Software distributed under the License is distributed on an AS IS basis,# WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License# for the specific language governing rights and limitations under the# License.# Written by Uoti Urpalafrom __future__ import divisionimport osimport sysimport shutilimport threadingimport tracebackfrom BitTorrent.platform import bttimefrom BitTorrent.download import Feedback, Multitorrentfrom BitTorrent.bencode import bdecodefrom BitTorrent.ConvertedMetainfo import ConvertedMetainfofrom BitTorrent.prefs import Preferencesfrom BitTorrent.BandwidthManager import BandwidthManagerfrom BitTorrent import BTFailure, BTShutdown, INFO, WARNING, ERROR, CRITICALfrom BitTorrent import configfilefrom BitTorrent import FAQ_URL#from BitTorrent.QueueButler import QueueButlerfrom BitTorrent import zurllibimport BitTorrentRUNNING = 0RUN_QUEUED = 1QUEUED = 2KNOWN = 3ASKING_LOCATION = 4state_dict = {RUNNING   : 'running',              RUN_QUEUED: 'paused',              QUEUED    : 'waiting',              KNOWN     : "``known''",              ASKING_LOCATION: "Dude, use the new TQ",}class TorrentInfo(object):    def __init__(self, config):        self.metainfo = None        self.dl = None        self.state = None        self.completion = None        self.finishtime = None        self.uptotal = 0        self.uptotal_old = 0        self.downtotal = 0        self.downtotal_old = 0        self.config = config    def _set_dlpath(self, value):        self.config['save_as'] = value    def _get_dlpath(self):        return self.config['save_as']    dlpath = property(_get_dlpath, _set_dlpath)def decode_position(l, pred, succ, default=None):    if default is None:        default = len(l)    if pred is None and succ is None:        return default    if pred is None:        return 0    if succ is None:        return len(l)    try:        if l[0] == succ and pred not in l:            return 0        if l[-1] == pred and succ not in l:            return len(l)        i = l.index(pred)        if l[i+1] == succ:            return i+1    except (ValueError, IndexError):        pass    return defaultclass TorrentQueue(Feedback):    def __init__(self, config, ui_options, rawserver, ipc):        self.ui_options = ui_options        self.rawserver = rawserver        self.ipc = ipc        self.config = config        self.config['def_running_torrents'] = 100 # !@# XXX        self.config['max_running_torrents'] = 100 # !@# XXX        self.doneflag = threading.Event()        self.torrents = {}        self.starting_torrent = None        self.running_torrents = []        self.queue = []        self.other_torrents = []        self.last_save_time = 0        self.last_version_check = 0        self.initialized = 0    # TEMP TEMP TEMP    def _get_total_rates(self):        u = 0.0        d = 0.0        for torrent in self.torrents.values():            if torrent is None:                continue            if torrent.dl is None:                continue            status = torrent.dl.get_status(False, False)            #print status            u += status.get('upRate', 0)            d += status.get('downRate', 0)        return u,d    def send_gui_rates(self, callback):        self.run_ui_task(callback, *self._get_total_rates())    def run(self, ui, ui_wrap, startflag):        zurllib.add_unsafe_thread()        self.rawserver.associate_thread()        startflag.set()        try:            self.ui = ui            self.run_ui_task = ui_wrap            self.multitorrent = Multitorrent(self.config, self.doneflag,                                             self.rawserver, self.global_error,                                             listen_fail_ok=True)            #upload_like_crazy.init(self.rawserver)            #self.rawserver.add_task(0, upload_like_crazy.queue_connections)            #def get_rates():            #    rate, delivered = upload_like_crazy.measure.get_last_rate()            #    return rate, 0            self.bwm = BandwidthManager(external_add_task=self.rawserver.external_add_task,                                        config=self.config,                                        set_config=self.set_config,                                        get_remote_endpoints=self.rawserver.get_remote_endpoints,                                        get_rates=self._get_total_rates)                                        #get_rates=get_rates)            self.ipc.start(self.external_command)            #qb = QueueButler(self.rawserver, self)            #qb.update()            try:                self._restore_state()            except BTFailure, e:                self.torrents = {}                self.running_torrents = []                self.queue = []                self.other_torrents = []                self.global_error(ERROR, _("Could not load saved state: ")+str(e))            else:                for infohash in self.running_torrents + self.queue + \                        self.other_torrents:                    t = self.torrents[infohash]                    if t.dlpath is not None:                        t.completion = self.multitorrent.get_completion(                            self.config, t.metainfo, t.dlpath)                    state = t.state                    if state == RUN_QUEUED:                        state = RUNNING                    self.run_ui_task(self.ui.new_displayed_torrent, infohash,                                     t.metainfo, t.dlpath, state, t.config,                                     t.completion, t.uptotal, t.downtotal, )            self._check_queue()            self.initialized = 1        except Exception, e:            # dump a normal exception traceback            traceback.print_exc()            # set the error flag            self.initialized = -1            # signal the gui thread to stop waiting            startflag.set()            return        self._queue_loop()        self.multitorrent.rawserver.listen_forever(self.doneflag)        if self.doneflag.isSet():            # this is where GUI cleanup used to be; now it does nothing            pass        self.multitorrent.close_listening_socket()        self.ipc.stop()        for infohash in list(self.running_torrents):            t = self.torrents[infohash]            if t.state == RUN_QUEUED:                continue            t.dl.shutdown()            if t.dl is not None:  # possibly set to none by failed()                totals = t.dl.get_total_transfer()                t.uptotal = t.uptotal_old + totals[0]                t.downtotal = t.downtotal_old + totals[1]        self._dump_state()    def _check_version(self):        now = bttime()        if self.last_version_check > 0 and \               self.last_version_check > now - 24*60*60:            return        self.last_version_check = now        self.run_ui_task(self.ui.check_version)    def _dump_config(self):        configfile.save_ui_config(self.config, 'bittorrent',                               self.ui_options, self.global_error)        for infohash,t in self.torrents.items():            ec = lambda level, message: self.error(t.metainfo, level, message)            config = t.config.getDict()            if config:                configfile.save_torrent_config(self.config['data_dir'],                                               infohash, config, ec)    def _dump_state(self):        self.last_save_time = bttime()        r = []        def write_entry(infohash, t):            if t.dlpath is None:                assert t.state == ASKING_LOCATION                r.append(infohash.encode('hex') + '\n')            else:                r.append(infohash.encode('hex') + ' ' +                         str(t.uptotal)         + ' ' +                         str(t.downtotal)       + '\n')        r.append('BitTorrent UI state file, version 4\n')        r.append('Running torrents\n')        for infohash in self.running_torrents:            write_entry(infohash, self.torrents[infohash])        #r.append('Queued torrents\n')        for infohash in self.queue:            write_entry(infohash, self.torrents[infohash])        #r.append('Known torrents\n')        for infohash in self.other_torrents:            write_entry(infohash, self.torrents[infohash])        r.append('Queued torrents\n')        r.append('Known torrents\n')        r.append('End\n')        f = None        try:            filename = os.path.join(self.config['data_dir'], 'ui_state')            f = file(filename + '.new', 'wb')            f.write(''.join(r))            f.close()            shutil.move(filename + '.new', filename)        except Exception, e:            self.global_error(ERROR, _("Could not save UI state: ") + str(e))            if f is not None:                f.close()    def _restore_state(self):        def decode_line(line):            hashtext = line[:40]            try:                infohash = hashtext.decode('hex')            except:                raise BTFailure(_("Invalid state file contents"))            if len(infohash) != 20:                raise BTFailure(_("Invalid state file contents"))            try:                path = os.path.join(self.config['data_dir'], 'metainfo',                                    hashtext)                f = file(path, 'rb')                data = f.read()                f.close()            except Exception, e:                try:                    f.close()                except:                    pass                self.global_error(ERROR,                                  (_("Error reading file \"%s\".") % path) +                                  " (" + str(e)+ "), " +                                  _("cannot restore state completely"))                return None            if infohash in self.torrents:                raise BTFailure(_("Invalid state file (duplicate entry)"))            t = TorrentInfo(Preferences(self.config))            self.torrents[infohash] = t            try:                t.metainfo = ConvertedMetainfo(bdecode(data))            except Exception, e:                self.global_error(ERROR,                                  (_("Corrupt data in \"%s\", cannot restore torrent.") % path) +                                  '('+str(e)+')')                return None            t.metainfo.reported_errors = True # suppress redisplay on restart            if infohash != t.metainfo.infohash:                self.global_error(ERROR,                                  (_("Corrupt data in \"%s\", cannot restore torrent.") % path) +                                  _("(infohash mismatch)"))                return None            if len(line) == 41:

⌨️ 快捷键说明

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