예제 #1
0
def make_meta_file(file,
                   url,
                   piece_len_exp,
                   flag=Event(),
                   progress=dummy,
                   progress_percent=1,
                   comment=None,
                   target=None):
    if piece_len_exp == None:
        piece_len_exp = 18
    piece_length = 2**piece_len_exp
    a, b = split(file)
    if not target:
        if b == '':
            f = a + '.torrent'
        else:
            f = join(a, b + '.torrent')
    else:
        f = target
    info = makeinfo(file, piece_length, flag, progress, progress_percent)
    if flag.isSet():
        return
    check_info(info)
    h = open(f, 'wb')
    data = {
        'info': info,
        'announce': strip(url),
        'creation date': long(time())
    }
    if comment:
        data['comment'] = comment
    h.write(bencode(data))
    h.close()
예제 #2
0
def make_meta_file(path,
                   url,
                   piece_len_exp,
                   flag=Event(),
                   progress=dummy,
                   comment=None,
                   target=None,
                   encoding='ascii'):
    piece_length = 2**piece_len_exp
    a, b = os.path.split(path)
    if not target:
        if b == '':
            f = a + '.torrent'
        else:
            f = os.path.join(a, b + '.torrent')
    else:
        f = target
    info = makeinfo(path, piece_length, flag, progress, encoding)
    if flag.isSet():
        return
    check_info(info)
    h = file(f, 'wb')
    data = {
        'info': info,
        'announce': url.strip(),
        'creation date': int(time())
    }
    if comment:
        data['comment'] = comment
    h.write(bencode(data))
    h.close()
예제 #3
0
def make_meta_file(path, url, piece_len_exp, flag=Event(), progress=dummy,
                   title=None, comment=None, target=None):
    data = {'announce': url.strip(), 'creation date': int(time())}
    piece_length = 2 ** piece_len_exp
    a, b = os.path.split(path)
    if not target:
        if b == '':
            f = a + '.torrent'
        else:
            f = os.path.join(a, b + '.torrent')
    else:
        f = target
    info = makeinfo(path, piece_length, flag, progress)
    if flag.isSet():
        return
    check_info(info)
    h = file(f, 'wb')

    data['info'] = info
    # TODO: encoding
    lang = read_language_file()
    if lang:
        data['locale'] = lang
    if title:
        data['title'] = title
    if comment:
        data['comment'] = comment
    h.write(bencode(data))
    h.close()
예제 #4
0
def make_meta_file_dht(path,
                       nodes,
                       piece_len_exp,
                       flag=Event(),
                       progress=dummy,
                       comment=None,
                       target=None,
                       encoding='ascii',
                       data_dir=None):
    # if nodes is empty, then get them out of the routing table in data_dir
    # else, expect nodes to be a string of comma seperated <ip>:<port> pairs
    # this has a lot of duplicated code from make_meta_file
    piece_length = 2**piece_len_exp
    a, b = os.path.split(path)
    if not target:
        if b == '':
            f = a + '.torrent'
        else:
            f = os.path.join(a, b + '.torrent')
    else:
        f = target
    info = makeinfo(path, piece_length, flag, progress, encoding)
    if flag.isSet():
        return
    check_info(info)
    info_hash = sha(bencode(info)).digest()

    if not nodes:
        x = open(os.path.join(data_dir, 'routing_table'), 'rb')
        d = bdecode(x.read())
        x.close()
        t = KTable(Node().initWithDict({
            'id': d['id'],
            'host': '127.0.0.1',
            'port': 0
        }))
        for n in d['rt']:
            t.insertNode(Node().initWithDict(n))
        nodes = [(node.host, node.port) for node in t.findNodes(info_hash)
                 if node.host != '127.0.0.1']
    else:
        nodes = [
            (a[0], int(a[1]))
            for a in [node.strip().split(":") for node in nodes.split(",")]
        ]
    data = {'nodes': nodes, 'creation date': int(time())}
    h = file(f, 'wb')

    data['info'] = info
    if comment:
        data['comment'] = comment
    h.write(bencode(data))
    h.close()
예제 #5
0
def make_meta_file_dht(path, nodes, piece_len_exp, flag=Event(), progress=dummy,
                   title=None, comment=None, target=None,
                   data_dir=None):
    # if nodes is empty, then get them out of the routing table in data_dir
    # else, expect nodes to be a string of comma seperated <ip>:<port> pairs
    # this has a lot of duplicated code from make_meta_file
    piece_length = 2 ** piece_len_exp
    a, b = os.path.split(path)
    if not target:
        if b == '':
            f = a + '.torrent'
        else:
            f = os.path.join(a, b + '.torrent')
    else:
        f = target
    info = makeinfo(path, piece_length, flag, progress)
    if flag.isSet():
        return
    check_info(info)
    info_hash = sha(bencode(info)).digest()

    if not nodes:
        x = open(os.path.join(data_dir, 'routing_table'), 'rb')
        d = bdecode(x.read())
        x.close()
        t = KTable(Node().initWithDict({'id':d['id'], 'host':'127.0.0.1','port': 0}))
        for n in d['rt']:
            t.insertNode(Node().initWithDict(n))
        nodes = [(node.host, node.port) for node in t.findNodes(info_hash) if node.host != '127.0.0.1']
    else:
        nodes = [(a[0], int(a[1])) for a in [node.strip().split(":") for node in nodes.split(",")]]
    data = {'nodes': nodes, 'creation date': int(time())}
    h = file(f, 'wb')

    data['info'] = info
    if title:
        data['title'] = title
    if comment:
        data['comment'] = comment
    h.write(bencode(data))
    h.close()
예제 #6
0
def make_meta_file(file, url, piece_len_exp = 18, 
        flag = Event(), progress = dummy, progress_percent=1, comment = None, target = None):
    piece_length = 2 ** piece_len_exp
    a, b = split(file)
    if target == '':
        if b == '':
            f = a + '.torrent'
        else:
            f = join(a, b + '.torrent')
    else:
        f = target
    info = makeinfo(file, piece_length, flag, progress, progress_percent)
    if flag.isSet():
        return
    check_info(info)
    h = open(f, 'wb')
    data = {'info': info, 'announce': strip(url), 'creation date': long(time())}
    if comment:
        data['comment'] = comment
    h.write(bencode(data))
    h.close()
def make_meta_file(path, url, piece_len_exp, flag=Event(), progress=dummy,
                   comment=None, target=None, encoding='ascii'):
    piece_length = 2 ** piece_len_exp
    a, b = os.path.split(path)
    if not target:
        if b == '':
            f = a + '.torrent'
        else:
            f = os.path.join(a, b + '.torrent')
    else:
        f = target
    info = makeinfo(path, piece_length, flag, progress, encoding)
    if flag.isSet():
        return
    check_info(info)
    h = file(f, 'wb')
    data = {'info': info, 'announce': url.strip(),'creation date': int(time())}
    if comment:
        data['comment'] = comment
    h.write(bencode(data))
    h.close()