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

📄 convertedmetainfo.py

📁 bittorrent source by python. please enjoy
💻 PY
📖 第 1 页 / 共 2 页
字号:
# 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 Urpala# required for Python 2.2from __future__ import generatorsimport osimport sysimport loggingimport urlparsefrom BitTorrent.hash import shaimport socket#debug=Trueglobal_logger = logging.getLogger("BitTorrent.ConvertedMetainfo")from BitTorrent.translation import _from BitTorrent.obsoletepythonsupport import *from BitTorrent.bencode import bencodefrom BitTorrent import btformatsfrom BitTorrent import BTFailure, InfoHashTypefrom BitTorrent.platform import get_filesystem_encoding, encode_for_filesystemfrom BitTorrent.defer import ThreadedDeferredWINDOWS_UNSUPPORTED_CHARS = u'"*/:<>?\|'windows_translate = {}for x in WINDOWS_UNSUPPORTED_CHARS:    windows_translate[ord(x)] = u'-'noncharacter_translate = {}for i in xrange(0xD800, 0xE000):    noncharacter_translate[i] = ord('-')for i in xrange(0xFDD0, 0xFDF0):    noncharacter_translate[i] = ord('-')for i in (0xFFFE, 0xFFFF):    noncharacter_translate[i] = ord('-')del x, idef generate_names(name, is_dir):    if is_dir:        prefix = name + '.'        suffix = ''    else:        pos = name.rfind('.')        if pos == -1:            pos = len(name)        prefix = name[:pos] + '.'        suffix = name[pos:]    i = 0    while True:        yield prefix + str(i) + suffix        i += 1class ConvertedMetainfo(object):    def __init__(self, metainfo):        """metainfo is a dict.  When read from a metainfo (i.e.,           .torrent file), the file must first be bdecoded before           being passed to ConvertedMetainfo."""        self.bad_torrent_wrongfield = False        self.bad_torrent_unsolvable = False        self.bad_torrent_noncharacter = False        self.bad_conversion = False        self.bad_windows = False        self.bad_path = False        self.reported_errors = False        self.is_batch = False        self.orig_files = None        self.files_fs = None        self.total_bytes = 0        self.sizes = []        self.comment = None        self.title = None          # descriptive title text for whole torrent        self.creation_date = None        self.metainfo = metainfo        self.encoding = None        self.caches = None        btformats.check_message(metainfo, check_paths=False)        info = metainfo['info']        self.is_private = info.has_key("private") and info['private']        if 'encoding' in metainfo:            self.encoding = metainfo['encoding']        elif 'codepage' in metainfo:            self.encoding = 'cp%s' % metainfo['codepage']        if self.encoding is not None:            try:                for s in u'this is a test', u'these should also work in any encoding: 0123456789\0':                    assert s.encode(self.encoding).decode(self.encoding) == s            except:                self.encoding = 'iso-8859-1'                self.bad_torrent_unsolvable = True        if info.has_key('length'):            self.total_bytes = info['length']            self.sizes.append(self.total_bytes)        else:            self.is_batch = True            r = []            self.orig_files = []            self.sizes = []            i = 0            for f in info['files']:                l = f['length']                self.total_bytes += l                self.sizes.append(l)                path = self._get_attr(f, 'path')                if len(path[-1]) == 0:                    if l > 0:                        raise BTFailure(_("Bad file path component: ")+x)                    # BitComet makes .torrent files with directories                    # listed along with the files, which we don't support                    # yet, in part because some idiot interpreted this as                    # a bug in BitComet rather than a feature.                    path.pop(-1)                for x in path:                    if not btformats.allowed_path_re.match(x):                        raise BTFailure(_("Bad file path component: ")+x)                self.orig_files.append('/'.join(path))                k = []                for u in path:                    tf2 = self._to_fs_2(u)                    k.append((tf2, u))                r.append((k,i))                i += 1            # If two or more file/subdirectory names in the same directory            # would map to the same name after encoding conversions + Windows            # workarounds, change them. Files are changed as            # 'a.b.c'->'a.b.0.c', 'a.b.1.c' etc, directories or files without            # '.' as 'a'->'a.0', 'a.1' etc. If one of the multiple original            # names was a "clean" conversion, that one is always unchanged            # and the rest are adjusted.            r.sort()            self.files_fs = [None] * len(r)            prev = [None]            res = []            stack = [{}]            for x in r:                j = 0                x, i = x                while x[j] == prev[j]:                    j += 1                del res[j:]                del stack[j+1:]                name = x[j][0][1]                if name in stack[-1]:                    for name in generate_names(x[j][1], j != len(x) - 1):                        name = self._to_fs(name)                        if name not in stack[-1]:                            break                stack[-1][name] = None                res.append(name)                for j in xrange(j + 1, len(x)):                    name = x[j][0][1]                    stack.append({name: None})                    res.append(name)                self.files_fs[i] = os.path.join(*res)                prev = x        self.name = self._get_attr(info, 'name')        self.name_fs = self._to_fs(self.name)        self.piece_length = info['piece length']        self.announce = metainfo.get('announce')        self.announce_list = metainfo.get('announce-list')        if 'announce-list' not in metainfo and 'announce' not in metainfo:            self.is_trackerless = True        else:            self.is_trackerless = False        self.nodes = metainfo.get('nodes')        self.title = metainfo.get('title')        self.comment = metainfo.get('comment')        self.creation_date = metainfo.get('creation date')        self.locale = metainfo.get('locale')        self.url_list = metainfo.get('url-list', [])        if not isinstance(self.url_list, list):            self.url_list = [self.url_list, ]        self.caches = metainfo.get('caches')        self.hashes = [info['pieces'][x:x+20] for x in xrange(0,            len(info['pieces']), 20)]        self.infohash = InfoHashType(sha(bencode(info)).digest())    def show_encoding_errors(self, errorfunc):

⌨️ 快捷键说明

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