Ejemplo n.º 1
0
def make_meta_file(path, url, piece_len_exp, flag=Event(), progress=dummy,
                   title=None, comment=None, safe=None, content_type=None,
                   target=None, url_list=None, name=None):
    data = {'announce': url.strip(), 'creation date': int(gmtime())}
    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, name, content_type)
    if flag.isSet():
        return
    check_info(info)
    h = file(f, 'wb')

    data['info'] = info
    lang = read_language_file() or 'en'
    if lang:
        data['locale'] = lang
    if title:
        data['title'] = title
    if comment:
        data['comment'] = comment
    if safe:
        data['safe'] = safe
    if url_list:
        data['url-list'] = url_list
    h.write(bencode(data))
    h.close()
Ejemplo n.º 2
0
def make_meta_file_dht(path,
                       nodes,
                       piece_len_exp,
                       flag=Event(),
                       progress=dummy,
                       title=None,
                       comment=None,
                       safe=None,
                       content_type=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, content_type)
    if flag.isSet():
        return
    check_info(info)
    info_hash = sha1(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(gmtime())}
    h = file(f, 'wb')

    data['info'] = info
    if title:
        data['title'] = title
    if comment:
        data['comment'] = comment
    if safe:
        data['safe'] = safe
    h.write(bencode(data))
    h.close()
Ejemplo n.º 3
0
def make_meta_file_dht(
    path,
    nodes,
    piece_len_exp,
    flag=Event(),
    progress=dummy,
    title=None,
    comment=None,
    safe=None,
    content_type=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, content_type)
    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
    if safe:
        data["safe"] = safe
    h.write(bencode(data))
    h.close()
Ejemplo n.º 4
0
def make_meta_file(path,
                   url,
                   piece_len_exp,
                   flag=Event(),
                   progress=dummy,
                   title=None,
                   comment=None,
                   safe=None,
                   content_type=None,
                   target=None,
                   url_list=None,
                   name=None,
                   micropayments=False):
    data = {'announce': url.strip(), 'creation date': int(gmtime())}
    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, name, content_type)
    if flag.isSet():
        return
    check_info(info)
    h = file(f, 'wb')

    data['info'] = info
    lang = read_language_file() or 'en'
    if lang:
        data['locale'] = lang
    if title:
        data['title'] = title
    if comment:
        data['comment'] = comment
    if safe:
        data['safe'] = safe
    if url_list:
        data['url-list'] = url_list
    if micropayments:
        data['micropayments'] = micropayments
    h.write(bencode(data))
    h.close()
Ejemplo n.º 5
0
def make_meta_file(
    path,
    url,
    piece_len_exp,
    flag=Event(),
    progress=dummy,
    title=None,
    comment=None,
    safe=None,
    content_type=None,
    target=None,
    url_list=None,
    name=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, name, content_type)
    if flag.isSet():
        return
    check_info(info)
    h = file(f, "wb")

    data["info"] = info
    lang = read_language_file() or "en"
    if lang:
        data["locale"] = lang
    if title:
        data["title"] = title
    if comment:
        data["comment"] = comment
    if safe:
        data["safe"] = safe
    if url_list:
        data["url-list"] = url_list
    h.write(bencode(data))
    h.close()
Ejemplo n.º 6
0
def make_meta_file_dht(path, nodes, piece_len_exp, flag=Event(),
                       progress=dummy, title=None, comment=None, safe=None,
                       content_type=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, content_type)
    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(gmtime())}
    h = file(f, 'wb')

    data['info'] = info
    if title:
        data['title'] = title
    if comment:
        data['comment'] = comment
    if safe:
        data['safe'] = safe
    h.write(bencode(data))
    h.close()