示例#1
0
 def setUp(self):
     self.dir = tempfile.TemporaryDirectory()
     self.session = lt.session(settings)
     self.ti = lt.torrent_info(dummy_data.DICT)
     self.atp = lt.add_torrent_params()
     self.atp.ti = self.ti
     self.atp.save_path = self.dir.name
     self.handle = self.session.add_torrent(self.atp)
示例#2
0
 def setUp(self):
     self.dir = tempfile.TemporaryDirectory()
     self.session = lt.session(settings)
     self.ti = lt.torrent_info(dummy_data.DICT)
     self.atp = lt.add_torrent_params()
     self.atp.ti = self.ti
     self.atp.save_path = self.dir.name
     self.handle = self.session.add_torrent(self.atp)
     self.wait_for(lambda: self.handle.status().state != lt.torrent_status.checking_files
                   and self.handle.status().state != lt.torrent_status.checking_resume_data, msg="checking")
示例#3
0
 def test_torrent_handle(self) -> None:
     atp = lt.add_torrent_params()
     atp.info_hashes = lt.info_hash_t(
         lt.sha1_hash(bytes.fromhex(self.info_hash_sha1)))
     session = lt.session(lib.get_isolated_settings())
     with tempfile.TemporaryDirectory() as path:
         atp.save_path = path
         handle = session.add_torrent(atp)
         uri = lt.make_magnet_uri(handle)
     self.assertEqual(uri, f"magnet:?xt=urn:btih:{self.info_hash_sha1}")
示例#4
0
    def addTorrent(self):
        print(self.torrent)
        if self.torrent.startswith('magnet:'):
            atp = lt.add_torrent_params()
            atp = lt.parse_magnet_uri(self.torrent)
            atp.save_path = '.'
            h = self.ses.add_torrent(atp)
        else:
            info = lt.torrent_info(self.torrent)
            h = self.ses.add_torrent({'ti': info, 'save_path': '.'})

        return h
示例#5
0
    async def test_config_defaults(self) -> None:
        save_path = str(await concurrency.to_thread(
            pathlib.Path("download").resolve))
        config = await asyncio.wait_for(services.get_config(), 5)
        self.assertEqual(config["torrent_default_save_path"], save_path)

        atp = await asyncio.wait_for(atp_services.get_default(), 5)

        self.assertEqual(atp.save_path, save_path)
        self.assertEqual(atp.flags, lt.torrent_flags.default_flags)
        self.assertEqual(atp.storage_mode,
                         lt.add_torrent_params().storage_mode)
    async def test_all_and_none(self) -> None:
        self.fake_eps.add("all", access_all, "tvaf.swarm.access_swarm")
        self.fake_eps.add("none", access_none, "tvaf.swarm.access_swarm")

        name_to_configure_swarm = await swarm.get_name_to_configure_swarm(
            INFO_HASHES)

        self.assertEqual(set(name_to_configure_swarm.keys()),
                         {"public", "all"})

        atp = lt.add_torrent_params()
        atp.info_hashes = INFO_HASHES
        await name_to_configure_swarm["all"](atp)
        self.assertEqual(atp.trackers, [TRACKER])
示例#7
0
文件: test.py 项目: v1p/libtorrent
    def test_add_torrent_params(self):
        atp = lt.add_torrent_params()

        for field_name in dir(atp):
            field = getattr(atp, field_name)
            print(field_name, field)

        atp.renamed_files = {}
        atp.merkle_tree = []
        atp.unfinished_pieces = {}
        atp.have_pieces = []
        atp.banned_peers = []
        atp.verified_pieces = []
        atp.piece_priorities = []
        atp.url_seeds = []
def test_good_resume_data_bad_info_dict(conn: apsw.Connection,
                                        ti: lt.torrent_info) -> None:
    atp = lt.add_torrent_params()
    atp.ti = ti
    bdecoded = lt.write_resume_data(atp)
    bdecoded.pop(b"info")
    resume_data = lt.bencode(bdecoded)
    conn.cursor().execute(
        "INSERT INTO torrent (info_sha1, info_sha256, resume_data, info) VALUES "
        "(RANDOMBLOB(20), RANDOMBLOB(32), ?, X'00')",
        (resume_data, ),
    )

    atps = list(resume.iter_resume_data_from_db(conn))

    assert len(atps) == 1
    assert atps[0].ti is None
    async def test_public_swarm(self) -> None:
        # by default, the public swarm (and only the public swarm) should be
        # configured
        name_to_access_swarm = swarm.get_name_to_access_swarm()
        self.assertEqual(set(name_to_access_swarm.keys()), {"public"})

        # The public swarm should be accessible to an arbitrary torrent
        access = name_to_access_swarm["public"]
        configure_public_swarm = await access(INFO_HASHES)

        # The public ConfigureSwarm function should do nothing
        atp = lt.add_torrent_params()
        atp.info_hashes = INFO_HASHES
        before = lt.write_resume_data(atp)
        await configure_public_swarm(atp)
        after = lt.write_resume_data(atp)
        self.assertEqual(after, before)
示例#10
0
def add_torrent(ses, filename, options):
    atp = lt.add_torrent_params()
    if filename.startswith('magnet:'):
        atp = lt.parse_magnet_uri(filename)
    else:
        atp.ti = lt.torrent_info(filename)
        try:
            atp.resume_data = open(os.path.join(options.save_path, atp.info.name() + '.fastresume'), 'rb').read()
        except Exception:
            pass

    atp.save_path = options.save_path
    atp.storage_mode = lt.storage_mode_t.storage_mode_sparse
    atp.flags |= lt.torrent_flags.duplicate_is_error \
        | lt.torrent_flags.auto_managed \
        | lt.torrent_flags.duplicate_is_error
    ses.async_add_torrent(atp)
示例#11
0
def add_torrent(ses, filename, options):
    atp = lt.add_torrent_params()
    if filename.startswith('magnet:'):
        atp = lt.parse_magnet_uri(filename)
    else:
        atp.ti = lt.torrent_info(filename)
        try:
            atp.resume_data = open(os.path.join(options.save_path, atp.info.name() + '.fastresume'), 'rb').read()
        except Exception:
            pass

    atp.save_path = options.save_path
    atp.storage_mode = lt.storage_mode_t.storage_mode_sparse
    atp.flags |= lt.torrent_flags.duplicate_is_error \
        | lt.torrent_flags.auto_managed \
        | lt.torrent_flags.duplicate_is_error
    ses.async_add_torrent(atp)
示例#12
0
def _mkatp(tmp_path_factory: pytest.TempPathFactory,
           *,
           proto=Proto.HYBRID) -> lt.add_torrent_params:
    atp = lt.add_torrent_params()
    # As of 2.0.6, create_torrent.set_hash2 isn't bound in python
    tmp_path = tmp_path_factory.mktemp("test-atp")
    (tmp_path / "file.txt").write_bytes(random.randbytes(1024))
    fs = lt.file_storage()
    lt.add_files(fs, str(tmp_path))
    flags = 0
    if not (proto & V2):
        flags = lt.create_torrent.v1_only
    elif not (proto & V1):
        flags = lt.create_torrent.v2_only
    ct = lt.create_torrent(fs, flags=flags)
    lt.set_piece_hashes(ct, str(tmp_path.parent))
    atp.ti = lt.torrent_info(ct.generate())
    return atp
def test_resume_data_external_info(conn: apsw.Connection,
                                   ti: lt.torrent_info) -> None:
    orig = lt.add_torrent_params()
    orig.ti = ti
    bdecoded = lt.write_resume_data(orig)
    bdecoded.pop(b"info")
    resume_data = lt.bencode(bdecoded)
    info = ti.info_section()
    info_hashes = ti.info_hashes()
    conn.cursor().execute(
        "INSERT INTO torrent (info_sha1, info_sha256, resume_data, info) "
        "VALUES (?, ?, ?, ?)",
        (info_hashes.v1.to_bytes(), info_hashes.v2.to_bytes(), resume_data,
         info),
    )

    atps = list(resume.iter_resume_data_from_db(conn))

    assert len(atps) == 1
    assert normalize(atps[0]) == normalize(orig)
示例#14
0
def add_torrent(ses, filename, options):
    atp = lt.add_torrent_params()
    if filename.startswith('magnet:'):
        atp = lt.parse_magnet_uri(filename)
    else:
        ti = lt.torrent_info(filename)
        resume_file = os.path.join(options.save_path,
                                   ti.name() + '.fastresume')
        try:
            atp = lt.read_resume_data(open(resume_file, 'rb').read())
        except Exception as e:
            print('failed to open resume file "%s": %s' % (resume_file, e))
        atp.ti = ti

    atp.save_path = options.save_path
    atp.storage_mode = lt.storage_mode_t.storage_mode_sparse
    atp.flags |= lt.torrent_flags.duplicate_is_error \
        | lt.torrent_flags.auto_managed \
        | lt.torrent_flags.duplicate_is_error
    ses.async_add_torrent(atp)
示例#15
0
def add_torrent(ses: lt.session, filename: str,
                options: optparse.Values) -> None:
    atp = lt.add_torrent_params()
    if filename.startswith("magnet:"):
        atp = lt.parse_magnet_uri(filename)
    else:
        ti = lt.torrent_info(filename)
        resume_file = os.path.join(options.save_path,
                                   ti.name() + ".fastresume")
        try:
            atp = lt.read_resume_data(open(resume_file, "rb").read())
        except Exception as e:
            print('failed to open resume file "%s": %s' % (resume_file, e))
        atp.ti = ti

    atp.save_path = options.save_path
    atp.storage_mode = lt.storage_mode_t.storage_mode_sparse
    atp.flags |= (lt.torrent_flags.duplicate_is_error
                  | lt.torrent_flags.auto_managed
                  | lt.torrent_flags.duplicate_is_error)
    ses.async_add_torrent(atp)
示例#16
0
文件: test.py 项目: wqqgt/libtorrent
    def test_add_torrent_params(self):
        atp = lt.add_torrent_params()

        for field_name in dir(atp):
            field = getattr(atp, field_name)
            print(field_name, field)
示例#17
0
 def atp(self) -> lt.add_torrent_params:
     atp = lt.add_torrent_params()
     self.configure_atp(atp)
     return atp
示例#18
0
    def test_add_torrent_params(self):
        atp = lt.add_torrent_params()

        for field_name in dir(atp):
            field = getattr(atp, field_name)
            print(field_name, field)
示例#19
0
#!/usr/bin/env python3
# Copyright Arvid Norberg 2008. Use, modification and distribution is
# subject to the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
from __future__ import print_function

import sys
import time

import libtorrent as lt

ses = lt.session({"listen_interfaces": "0.0.0.0:6881"})

atp = lt.add_torrent_params()
atp.ti = lt.torrent_info(sys.argv[1])
atp.save_path = "."
h = ses.add_torrent(atp)
s = h.status()
print("starting", s.name)

while not s.is_seeding:
    s = h.status()

    print(
        "\r%.2f%% complete (down: %.1f kB/s up: %.1f kB/s peers: %d) %s" % (
            s.progress * 100,
            s.download_rate / 1000,
            s.upload_rate / 1000,
            s.num_peers,
            s.state,
        ),
示例#20
0
async def get_default() -> lt.add_torrent_params:
    atp = lt.add_torrent_params()
    for _, func in sorted(_DEFAULT_FUNCS.get().items()):
        await func(atp)
    return atp