def _on_torrent_created(result): """ Success callback :param result: from create_torrent_file """ with open(result['torrent_file_path'], 'rb') as f: torrent_64 = base64.b64encode(f.read()) # Download this torrent if specified if 'download' in request.args and len(request.args['download']) > 0 \ and request.args['download'][0] == "1": download_config = DownloadStartupConfig() download_config.set_dest_dir(result['base_path'] if len( file_path_list) == 1 else result['base_dir']) try: self.session.start_download_from_uri( 'file:' + result['torrent_file_path'], download_config) except DuplicateDownloadException: self._logger.warning( "The created torrent is already being downloaded.") request.write(json.dumps({"torrent": torrent_64})) # If the above request.write failed, the request will have already been finished if not request.finished: request.finish()
def setupSeeder(self): from Tribler.Core.Session import Session from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.DownloadConfig import DownloadStartupConfig self.setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() self.session2 = Session(self.config2, ignore_singleton=True) upgrader = self.session2.prestart() while not upgrader.is_done: time.sleep(0.1) assert not upgrader.failed, upgrader.current_status self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.finalize() torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding d = self.session2.start_download(tdef, dscfg) d.set_state_callback(self.seeder_state_callback) return torrentfn
def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from Tribler.Core.Session import Session self.seed_config = self.config.copy() self.seed_config.set_state_dir(self.getStateDir(2)) self.seed_config.set_megacache_enabled(True) self.seed_config.set_tunnel_community_socks5_listen_ports(self.get_socks5_ports()) if self.session2 is None: self.session2 = Session(self.seed_config) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://localhost/announce") tdef.finalize() torrentfn = os.path.join(self.session2.config.get_state_dir(), "gen.torrent") tdef.save(torrentfn) self.seed_tdef = tdef if hops > 0: # Safe seeding enabled self.tunnel_community_seeder = self.load_tunnel_community_in_session(self.session2) self.tunnel_community_seeder.build_tunnels(hops) else: self.sanitize_network(self.session2) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.start_download_from_tdef(tdef, dscfg) d.set_state_callback(self.seeder_state_callback)
def setUpPostSession(self): """ override TestAsServer """ TestAsServer.setUpPostSession(self) # Let Tribler start downloading an non-functioning torrent, so # we can talk to a normal download engine. self.torrentfn = os.path.join('extend_hs_dir', 'dummydata.merkle.torrent') tdef = TorrentDef.load(self.torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.config_path) self.session.start_download(tdef, dscfg) # This is the infohash of the torrent in test/extend_hs_dir self.infohash = '\xccg\x07\xe2\x9e!]\x16\xae{\xb8\x10?\xf9\xa5\xf9\x07\xfdBk' self.setUpMyListenSocket() # Must be changed in test/extend_hs_dir/dummydata.merkle.torrent as well self.mytrackerport = 4901 # Must be Tribler version <= 3.5.0. Changing this to 351 makes this test # fail, so it's a good test. self.myid = 'R350-----HgUyPu56789' self.mytracker = MyTracker(self.mytrackerport, self.myid, '127.0.0.1', self.mylistenport) self.mytracker.background_serve() print >> sys.stderr, "test: Giving MyTracker and myself time to start" time.sleep(5)
def __init__(self, dlconfig=None): if DefaultDownloadStartupConfig.__single: raise RuntimeError, "DefaultDownloadStartupConfig is singleton" DefaultDownloadStartupConfig.__single = self DownloadStartupConfig.__init__(self, dlconfig=dlconfig)
def setUpPostSession(self): """ override TestAsServer """ TestAsServer.setUpPostSession(self) # Let Tribler start downloading an non-functioning torrent, so # we can talk to a normal download engine. self.torrentfn = os.path.join('extend_hs_dir','dummydata.merkle.torrent') tdef = TorrentDef.load(self.torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.config_path) self.session.start_download(tdef,dscfg) # This is the infohash of the torrent in test/extend_hs_dir self.infohash = '\xccg\x07\xe2\x9e!]\x16\xae{\xb8\x10?\xf9\xa5\xf9\x07\xfdBk' self.setUpMyListenSocket() # Must be changed in test/extend_hs_dir/dummydata.merkle.torrent as well self.mytrackerport = 4901 # Must be Tribler version <= 3.5.0. Changing this to 351 makes this test # fail, so it's a good test. self.myid = 'R350-----HgUyPu56789' self.mytracker = MyTracker(self.mytrackerport,self.myid,'127.0.0.1',self.mylistenport) self.mytracker.background_serve() print >>sys.stderr,"test: Giving MyTracker and myself time to start" time.sleep(5)
def setupSeeder(self, hops=0, session=None): from Tribler.Core.Session import Session from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.DownloadConfig import DownloadStartupConfig self.setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() self.config2.set_state_dir(self.getStateDir(2)) if session is None: self.session2 = Session(self.config2, ignore_singleton=True, autoload_discovery=False) upgrader = self.session2.prestart() while not upgrader.is_done: time.sleep(0.1) self.session2.start() session = self.session2 tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.set_private() # disable dht tdef.finalize() torrentfn = os.path.join(session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = session.start_download(tdef, dscfg) d.set_state_callback(self.seeder_state_callback) return torrentfn
def __init__(self,dlconfig=None): if DefaultDownloadStartupConfig.__single: raise RuntimeError, "DefaultDownloadStartupConfig is singleton" DefaultDownloadStartupConfig.__single = self DownloadStartupConfig.__init__(self,dlconfig=dlconfig)
def setUpDownloadConfig(self): dscfg = DownloadStartupConfig() print >> sys.stderr, "test: Downloading to", self.config_path dscfg.set_dest_dir(self.config_path) dscfg.set_breakup_seed_bitfield(False) return dscfg
def setupSeeder(self): from Tribler.Core.Session import Session from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.DownloadConfig import DownloadStartupConfig self.setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() self.session2 = Session(self.config2, ignore_singleton=True) upgrader = self.session2.prestart() while not upgrader.is_done: time.sleep(0.1) assert not upgrader.failed, upgrader.current_status self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.finalize() torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(TESTS_DATA_DIR) # basedir of the file we are seeding d = self.session2.start_download(tdef, dscfg) d.set_state_callback(self.seeder_state_callback) return torrentfn
def resume_download(self, filename, setupDelay=0): tdef = dscfg = pstate = None try: pstate = self.load_download_pstate(filename) # SWIFTPROC metainfo = pstate.get('state', 'metainfo') if 'infohash' in metainfo: tdef = TorrentDefNoMetainfo(metainfo['infohash'], metainfo['name'], metainfo.get('url', None)) else: tdef = TorrentDef.load_from_dict(metainfo) if pstate.has_option('download_defaults', 'saveas') and \ isinstance(pstate.get('download_defaults', 'saveas'), tuple): pstate.set('download_defaults', 'saveas', pstate.get('download_defaults', 'saveas')[-1]) dscfg = DownloadStartupConfig(pstate) except: # pstate is invalid or non-existing _, file = os.path.split(filename) infohash = binascii.unhexlify(file[:-6]) torrent_data = self.torrent_store.get(infohash) if torrent_data: try: tdef = TorrentDef.load_from_memory(torrent_data) defaultDLConfig = DefaultDownloadStartupConfig.getInstance() dscfg = defaultDLConfig.copy() if self.mypref_db is not None: dest_dir = self.mypref_db.getMyPrefStatsInfohash(infohash) if dest_dir and os.path.isdir(dest_dir): dscfg.set_dest_dir(dest_dir) except ValueError: self._logger.warning("tlm: torrent data invalid") if pstate is not None: has_resume_data = pstate.get('state', 'engineresumedata') is not None self._logger.debug("tlm: load_checkpoint: resumedata %s", 'len %s ' % len(pstate.get('state', 'engineresumedata')) if has_resume_data else 'None') if tdef and dscfg: if dscfg.get_dest_dir() != '': # removed torrent ignoring try: if self.download_exists(tdef.get_infohash()): self._logger.info("tlm: not resuming checkpoint because download has already been added") elif dscfg.get_credit_mining() and not self.session.config.get_credit_mining_enabled(): self._logger.info("tlm: not resuming checkpoint since token mining is disabled") else: self.add(tdef, dscfg, pstate, setupDelay=setupDelay) except Exception as e: self._logger.exception("tlm: load check_point: exception while adding download %s", tdef) else: self._logger.info("tlm: removing checkpoint %s destdir is %s", filename, dscfg.get_dest_dir()) os.remove(filename) else: self._logger.info("tlm: could not resume checkpoint %s %s %s", filename, tdef, dscfg)
def _on_torrent_created(result): """ Success callback :param result: from create_torrent_file """ metainfo_dict = bdecode(result['metainfo']) # Download this torrent if specified if 'download' in request.args and len(request.args['download']) > 0 \ and request.args['download'][0] == "1": download_config = DownloadStartupConfig() download_config.set_dest_dir(result['base_path'] if len( file_path_list) == 1 else result['base_dir']) try: self.session.lm.ltmgr.start_download( tdef=TorrentDef(metainfo=metainfo_dict), dconfig=download_config) except DuplicateDownloadException: self._logger.warning( "The created torrent is already being downloaded.") request.write( json.dumps({"torrent": base64.b64encode(result['metainfo'])})) # If the above request.write failed, the request will have already been finished if not request.finished: request.finish()
def resume_download(self, filename, setupDelay=0): tdef = dscfg = pstate = None try: pstate = self.load_download_pstate(filename) # SWIFTPROC metainfo = pstate.get('state', 'metainfo') if 'infohash' in metainfo: tdef = TorrentDefNoMetainfo(metainfo['infohash'], metainfo['name'], metainfo.get('url', None)) else: tdef = TorrentDef.load_from_dict(metainfo) if pstate.has_option('download_defaults', 'saveas') and \ isinstance(pstate.get('download_defaults', 'saveas'), tuple): pstate.set('download_defaults', 'saveas', pstate.get('download_defaults', 'saveas')[-1]) dscfg = DownloadStartupConfig(pstate) except: # pstate is invalid or non-existing _, file = os.path.split(filename) infohash = binascii.unhexlify(file[:-6]) torrent_data = self.torrent_store.get(infohash) if torrent_data: try: tdef = TorrentDef.load_from_memory(torrent_data) defaultDLConfig = DefaultDownloadStartupConfig.getInstance() dscfg = defaultDLConfig.copy() if self.mypref_db is not None: dest_dir = self.mypref_db.getMyPrefStatsInfohash(infohash) if dest_dir and os.path.isdir(dest_dir): dscfg.set_dest_dir(dest_dir) except ValueError: self._logger.warning("tlm: torrent data invalid") if pstate is not None: has_resume_data = pstate.get('state', 'engineresumedata') is not None self._logger.debug("tlm: load_checkpoint: resumedata %s", 'len %s ' % len(pstate.get('state', 'engineresumedata')) if has_resume_data else 'None') if tdef and dscfg: if dscfg.get_dest_dir() != '': # removed torrent ignoring try: if not self.download_exists(tdef.get_infohash()): self.add(tdef, dscfg, pstate, setupDelay=setupDelay) else: self._logger.info("tlm: not resuming checkpoint because download has already been added") except Exception as e: self._logger.exception("tlm: load check_point: exception while adding download %s", tdef) else: self._logger.info("tlm: removing checkpoint %s destdir is %s", filename, dscfg.get_dest_dir()) os.remove(filename) else: self._logger.info("tlm: could not resume checkpoint %s %s %s", filename, tdef, dscfg)
def __init__(self, dlconfig=None): if DefaultDownloadStartupConfig.__single: raise RuntimeError("DefaultDownloadStartupConfig is singleton") DefaultDownloadStartupConfig.__single = self DownloadStartupConfig.__init__(self, dlconfig=dlconfig) self._logger = logging.getLogger(self.__class__.__name__)
def setup_seeder(self): self.seeder_setup_complete = threading.Event() self.dscfg = DownloadStartupConfig() self.dscfg.set_dest_dir(TESTS_API_DIR) self.download = self.session.start_download(self.tdef, self.dscfg) self.download.set_state_callback(self.seeder_state_callback) assert self.seeder_setup_complete.wait(30)
def __init__(self, dlconfig=None): if DefaultDownloadStartupConfig.__single: raise RuntimeError("DefaultDownloadStartupConfig is singleton") DefaultDownloadStartupConfig.__single = self DownloadStartupConfig.__init__(self, dlconfig=dlconfig) self._logger = logging.getLogger(self.__class__.__name__)
def start_anon_download(self, hops=1): """ Start an anonymous download in the main Tribler session. """ dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.getDestDir()) dscfg.set_hops(hops) download = self.session.start_download_from_tdef(self.seed_tdef, dscfg) download.add_peer(("127.0.0.1", self.session2.config.get_libtorrent_port())) return download
def setUpPreSession(self): """ override TestAsServer """ super(TestSeeding, self).setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() # not really necess self.config2.set_state_dir(self.getStateDir(2)) self.dscfg2 = DownloadStartupConfig() self.dscfg2.set_dest_dir(self.getDestDir(2))
def start_anon_download(self, hops=1): """ Start an anonymous download in the main Tribler session. """ dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.getDestDir()) dscfg.set_hops(hops) download = self.session.start_download_from_tdef(self.seed_tdef, dscfg) tc = self.session.lm.tunnel_community tc.bittorrent_peers[download] = [("127.0.0.1", self.session2.config.get_libtorrent_port())] return download
def updated_my_channel(self, new_torrent_path): """ Notify the core that we updated our channel. :param new_torrent_path: path to the new torrent file """ # Start the new download tdef = TorrentDef.load(new_torrent_path) dcfg = DownloadStartupConfig() dcfg.set_dest_dir(self.mds.channels_dir) dcfg.set_channel_download(True) self.add(tdef, dcfg)
def start_vod_download(self): self.tdef = TorrentDef() self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() dscfg = DownloadStartupConfig() dscfg.set_dest_dir(os.path.dirname(self.sourcefn)) download = self.session.start_download_from_tdef(self.tdef, dscfg) return download.get_handle()
def start_vod_download(self): self.tdef = TorrentDef() self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() dscfg = DownloadStartupConfig() dscfg.set_dest_dir(os.path.dirname(self.sourcefn)) download = self.session.start_download_from_tdef(self.tdef, dscfg) return download.get_handle()
def updated_my_channel(self, tdef): """ Notify the core that we updated our channel. """ with db_session: my_channel = self.session.lm.mds.ChannelMetadata.get_my_channel() if my_channel and my_channel.status == COMMITTED and not self.session.has_download(str(my_channel.infohash)): dcfg = DownloadStartupConfig() dcfg.set_dest_dir(self.session.lm.mds.channels_dir) dcfg.set_channel_download(True) self.session.lm.add(tdef, dcfg)
def register_file_stream(self): self.tdef = TorrentDef() self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() dscfg = DownloadStartupConfig() dscfg.set_dest_dir(os.path.dirname(self.sourcefn)) download = self.session.start_download(self.tdef, dscfg) while not download.handle: time.sleep(1)
def register_file_stream(self): self.tdef = TorrentDef() self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() dscfg = DownloadStartupConfig() dscfg.set_dest_dir(os.path.dirname(self.sourcefn)) download = self.session.start_download(self.tdef, dscfg) while not download.handle: time.sleep(1)
def resume_download(self, filename, setupDelay=0): tdef = dscfg = pstate = None pstate = self.load_download_pstate(filename) metainfo = pstate.get('state', 'metainfo') if 'infohash' in metainfo: tdef = TorrentDefNoMetainfo(metainfo['infohash'], metainfo['name'], metainfo.get('url', None)) else: tdef = TorrentDef.load_from_dict(metainfo) if pstate.has_option('download_defaults', 'saveas') and \ isinstance(pstate.get('download_defaults', 'saveas'), tuple): pstate.set('download_defaults', 'saveas', pstate.get('download_defaults', 'saveas')[-1]) dscfg = DownloadStartupConfig(pstate) if pstate is not None: has_resume_data = pstate.get('state', 'engineresumedata') is not None self._logger.debug( "tlm: load_checkpoint: resumedata %s", 'len %s ' % len(pstate.get('state', 'engineresumedata')) if has_resume_data else 'None') if tdef and dscfg: if dscfg.get_dest_dir() != '': # removed torrent ignoring try: if self.download_exists(tdef.get_infohash()): self._logger.info( "tlm: not resuming checkpoint because download has already been added" ) elif dscfg.get_credit_mining( ) and not self.session.config.get_credit_mining_enabled(): self._logger.info( "tlm: not resuming checkpoint since token mining is disabled" ) else: self.add(tdef, dscfg, pstate, setupDelay=setupDelay) except Exception as e: self._logger.exception( "tlm: load check_point: exception while adding download %s", tdef) else: self._logger.info("tlm: removing checkpoint %s destdir is %s", filename, dscfg.get_dest_dir()) os.remove(filename) else: self._logger.info("tlm: could not resume checkpoint %s %s %s", filename, tdef, dscfg)
def do_help(self, infohash, torrent_data, permid): basename = binascii.hexlify( infohash) + '.torrent' # ignore .tribe stuff, not vital torrentfilename = os.path.join(self.helpdir, basename) tfile = open(torrentfilename, "wb") tfile.write(torrent_data) tfile.close() if DEBUG: print >> sys.stderr, "helpmsg: Got metadata required for helping", show_permid_short( permid) print >> sys.stderr, "helpmsg: torrent: ", torrentfilename tdef = TorrentDef.load(torrentfilename) if self.dlconfig is None: dscfg = DownloadStartupConfig() else: dscfg = DownloadStartupConfig(self.dlconfig) dscfg.set_coopdl_coordinator_permid(permid) dscfg.set_dest_dir(self.helpdir) # Start new download self.session.start_download(tdef, dscfg)
def test_remove_torrent_id(self): """ Test whether removing a torrent id works. """ torrent_def = TorrentDef.load(TORRENT_UBUNTU_FILE) dcfg = DownloadStartupConfig() dcfg.set_dest_dir(self.getDestDir()) download = self.session.start_download_from_tdef(torrent_def, download_startup_config=dcfg, hidden=True) # Create a deferred which forwards the unhexlified string version of the download's infohash download_started = download.get_handle().addCallback(lambda handle: unhexlify(str(handle.info_hash()))) return download_started.addCallback(self.session.remove_download_by_id)
def download_channel(self, channel): """ Download a channel with a given infohash and title. :param channel: The channel metadata ORM object. """ finished_deferred = Deferred() dcfg = DownloadStartupConfig() dcfg.set_dest_dir(self.mds.channels_dir) dcfg.set_channel_download(True) tdef = TorrentDefNoMetainfo(infohash=str(channel.infohash), name=channel.title) download = self.session.start_download_from_tdef(tdef, dcfg) channel_id = channel.public_key download.finished_callback = lambda dl: self.on_channel_download_finished(dl, channel_id, finished_deferred) return download, finished_deferred
def start_seeding(self): # the metadata that we want to transfer self.tdef = TorrentDef() self.tdef.add_content(os.path.join(TESTS_DATA_DIR, "file.wmv")) self.tdef.set_tracker("http://localhost/announce") # we use a small piece length to obtain multiple pieces self.tdef.set_piece_length(1) self.tdef.finalize() self.dscfg = DownloadStartupConfig() self.dscfg.set_dest_dir(TESTS_DATA_DIR) self.download = self.session.start_download_from_tdef(self.tdef, self.dscfg) self.download.set_state_callback(self.seeder_state_callback) return self.seed_deferred
def copy(self): config = CallbackConfigParser() config._sections = { 'downloadconfig': copy.deepcopy(self.dlconfig._sections['downloadconfig']) } return DownloadStartupConfig(config)
def test_multifile_torrent(self): tdef = TorrentDef() dn = os.path.join(TESTS_DATA_DIR, "contentdir") tdef.add_content(dn, "dirintorrent") fn = os.path.join(TESTS_DATA_DIR, "video.avi") tdef.add_content(fn, os.path.join("dirintorrent", "video.avi")) tdef.set_tracker("http://tribler.org/announce") tdef.finalize() impl = LibtorrentDownloadImpl(self.session, tdef) # Override the add_torrent because it will be called impl.ltmgr = MockObject() impl.ltmgr.add_torrent = lambda _, _dummy2: fake_handler impl.set_selected_files = lambda: None fake_handler = MockObject() fake_handler.is_valid = lambda: True fake_handler.status = lambda: fake_status fake_handler.set_share_mode = lambda _: None fake_handler.resume = lambda: None fake_handler.resolve_countries = lambda _: None fake_status = MockObject() fake_status.share_mode = False # Create a dummy download config impl.dlconfig = DownloadStartupConfig().dlconfig.copy() # Create a dummy pstate pstate = CallbackConfigParser() pstate.add_section("state") test_dict = dict() test_dict["a"] = "b" pstate.set("state", "engineresumedata", test_dict) return impl.network_create_engine_wrapper(pstate)
def test_resume_download(self): """ Testing whether the API returns 200 if a download is being resumed """ video_tdef, _ = self.create_local_torrent( os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef( video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') def mocked_restart(): download.should_restart = True def verify_resumed(_): self.assertEqual(len(self.session.get_downloads()), 1) download = self.session.get_downloads()[0] self.assertTrue(download.should_restart) download.restart = mocked_restart request_deferred = self.do_request('downloads/%s' % infohash, post_data={"state": "resume"}, expected_code=200, expected_json={"modified": True}, request_type='PATCH') return request_deferred.addCallback(verify_resumed)
def test_remove_torrent_id(self): """ Test whether removing a torrent id works. """ torrent_def = TorrentDef.load(TORRENT_UBUNTU_FILE) dcfg = DownloadStartupConfig() dcfg.set_dest_dir(self.getDestDir()) download = self.session.start_download_from_tdef( torrent_def, download_startup_config=dcfg, hidden=True) # Create a deferred which forwards the unhexlified string version of the download's infohash download_started = download.get_handle().addCallback( lambda handle: unhexlify(str(handle.info_hash()))) return download_started.addCallback(self.session.remove_download_by_id)
def test_stop_download(self): """ Testing whether the API returns 200 if a download is being stopped """ video_tdef, _ = self.create_local_torrent( os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef( video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') original_stop = download.stop def mocked_stop(): download.should_stop = True download.stop = original_stop def verify_removed(_): self.assertEqual(len(self.session.get_downloads()), 1) download = self.session.get_downloads()[0] self.assertTrue(download.should_stop) download.stop = mocked_stop request_deferred = self.do_request( 'downloads/%s' % infohash, post_data={"state": "stop"}, expected_code=200, request_type='PATCH', expected_json={ "modified": True, "infohash": "8bb88a02da691636a7ed929b87d467f24700e490" }) return request_deferred.addCallback(verify_removed)
def test_select_download_file(self): """ Testing whether files can be correctly toggled in a download """ video_tdef, _ = self.create_local_torrent( os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef( video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') def mocked_set_selected_files(*_): mocked_set_selected_files.called = True mocked_set_selected_files.called = False def verify_method_called(_): self.assertTrue(mocked_set_selected_files.called) download.set_selected_files = mocked_set_selected_files return self.do_request('downloads/%s' % infohash, post_data={"selected_files[]": 0}, expected_code=200, request_type='PATCH', expected_json={"modified": True, "infohash": "8bb88a02da691636a7ed929b87d467f24700e490"})\ .addCallback(verify_method_called)
def test_recheck_download(self): """ Testing whether the API returns 200 if a download is being rechecked """ video_tdef, _ = self.create_local_torrent( os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef( video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') def mocked_recheck(): mocked_recheck.called = True mocked_recheck.called = False download.force_recheck = mocked_recheck def verify_rechecked(_): self.assertEqual(len(self.session.get_downloads()), 1) self.assertTrue(mocked_recheck.called) request_deferred = self.do_request( 'downloads/%s' % infohash, post_data={"state": "recheck"}, expected_code=200, request_type='PATCH', expected_json={ "modified": True, "infohash": "8bb88a02da691636a7ed929b87d467f24700e490" }) return request_deferred.addCallback(verify_rechecked)
def test_change_hops_fail(self): def on_remove_download(d, remove_content=False, remove_state=True, hidden=False): return fail(RuntimeError()) self.session.remove_download = on_remove_download video_tdef, _ = self.create_local_torrent( os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') return self.do_request('downloads/%s' % infohash, post_data={"remove_data": True}, expected_code=500, expected_json={ u'error': { u'message': u'', u'code': u'RuntimeError', u'handled': True } }, request_type='DELETE')
def test_get_downloads_with_channels(self): """ Testing whether the API returns the right download when a download is added """ test_channel_name = 'testchan' def verify_download(downloads): downloads_json = json.loads(downloads) self.assertEqual(len(downloads_json['downloads']), 3) self.assertEqual(test_channel_name, [d for d in downloads_json["downloads"] if d["channel_download"]][0]["name"]) video_tdef, _ = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) self.session.start_download_from_uri("file:" + pathname2url( os.path.join(TESTS_DATA_DIR, "bak_single.torrent"))) with db_session: my_channel = self.session.lm.mds.ChannelMetadata.create_channel(test_channel_name, 'test') my_channel.add_torrent_to_channel(video_tdef) torrent_dict = my_channel.commit_channel_torrent() self.session.lm.gigachannel_manager.updated_my_channel(TorrentDef.TorrentDef.load_from_dict(torrent_dict)) self.should_check_equality = False return self.do_request('downloads?get_peers=1&get_pieces=1', expected_code=200).addCallback(verify_download)
def test_stop_download(self): """ Testing whether the API returns 200 if a download is being stopped """ video_tdef, _ = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) download = self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = get_hex_infohash(video_tdef) original_stop = download.stop def mocked_stop(): download.should_stop = True download.stop = original_stop def verify_removed(_): self.assertEqual(len(self.session.get_downloads()), 1) download = self.session.get_downloads()[0] self.assertTrue(download.should_stop) download.stop = mocked_stop request_deferred = self.do_request('downloads/%s' % infohash, post_data={"state": "stop"}, expected_code=200, request_type='PATCH', expected_json={"modified": True, "infohash": "c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5"}) return request_deferred.addCallback(verify_removed)
def setup_seeder(self): self.seeder_setup_complete = False self.seeder_teardown_complete = False self.seeder_teardown = False self.dscfg = DownloadStartupConfig() self.dscfg.set_dest_dir(os.getcwd()) self.download = self.session.start_download(self.tdef, self.dscfg) self.download.set_state_callback(self.seeder_state_callback) counter = 0 while not self.seeder_setup_complete: counter += 1 time.sleep(1) assert counter < 30, "timeout" print >> sys.stderr, "test: setup_seeder() complete"
def on_torrent_insert(self, source_str, infohash, name): """ Callback function called by the source when a new torrent is discovered """ self._logger.debug('Received torrent %s from %s', infohash, source_str) if source_str not in self.sources: self._logger.debug('Skipping torrent %s (unknown source %s)', infohash, source_str) return # Did we already get this torrent from another source? if infohash in self.torrents: self.torrents[infohash].sources.add(source_str) self._logger.debug('Skipping torrent %s (already known)', infohash) return # If a download already exists or already has a checkpoint, skip this torrent if self.session.get_download(unhexlify(infohash)) or \ os.path.exists(os.path.join(self.session.get_downloads_pstate_dir(), infohash + '.state')): self._logger.debug( 'Skipping torrent %s (download already running or scheduled to run)', infohash) return if len(self.torrents) >= self.settings.max_torrents_listed: self._logger.debug('Skipping torrent %s (limit reached)', infohash) return self.torrents[infohash] = CreditMiningTorrent(infohash, name) self.torrents[infohash].sources.add(source_str) self._logger.info('Starting torrent %s', infohash) magnet = u'magnet:?xt=urn:btih:%s&dn=%s' % (infohash, name) dl_config = DownloadStartupConfig() dl_config.set_hops(self.settings.hops) dl_config.set_dest_dir(self.settings.save_path) dl_config.set_credit_mining(True) dl_config.set_user_stopped(True) self.session.lm.add(TorrentDefNoMetainfo(unhexlify(infohash), name, magnet), dl_config, hidden=True)
def setup_seeder(self): self.seeder_setup_complete = threading.Event() self.dscfg = DownloadStartupConfig() self.dscfg.set_dest_dir(os.path.join(BASE_DIR, "API")) self.download = self.session.start_download(self.tdef, self.dscfg) self.download.set_state_callback(self.seeder_state_callback) assert self.seeder_setup_complete.wait(30)
def setUpPostSession(self): """ override TestAsServer """ TestAsServer.setUpPostSession(self) # Let Tribler start downloading an non-functioning torrent, so # we can talk to a normal download engine. self.torrentfn = os.path.join('extend_hs_dir','dummydata.merkle.torrent') tdef = TorrentDef.load(self.torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.config_path) self.session.start_download(tdef,dscfg) # This is the infohash of the torrent in test/extend_hs_dir self.infohash = '\xccg\x07\xe2\x9e!]\x16\xae{\xb8\x10?\xf9\xa5\xf9\x07\xfdBk' self.mylistenport = 4810
def test_download_unknown_state(self): """ Testing whether the API returns error 400 if an unknown state is passed when modifying a download """ video_tdef, _ = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) self.should_check_equality = False return self.do_request('downloads/%s' % video_tdef.get_infohash().encode('hex'), expected_code=400, post_data={"state": "abc"}, request_type='PATCH')
def test_change_hops(self): """ Testing whether the API returns 200 if we change the amount of hops of a download """ video_tdef, _ = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi')) self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig()) infohash = video_tdef.get_infohash().encode('hex') return self.do_request('downloads/%s' % infohash, post_data={'anon_hops': 1}, expected_code=200, expected_json={'modified': True}, request_type='PATCH')
def setUpPostSession(self): """ override TestAsServer """ TestAsServer.setUpPostSession(self) # Let Tribler start downloading an non-functioning torrent, so # we can talk to a normal download engine. self.torrentfn = os.path.join('extend_hs_dir', 'dummydata.merkle.torrent') tdef = TorrentDef.load(self.torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(self.config_path) self.session.start_download(tdef, dscfg) # This is the infohash of the torrent in test/extend_hs_dir self.infohash = '\xccg\x07\xe2\x9e!]\x16\xae{\xb8\x10?\xf9\xa5\xf9\x07\xfdBk' self.mylistenport = 4810
def new_download(self, permid, infohash, torrent_data): """ Start a new download in order to get the pieces that will be requested by the doe. After the download is started, find the appropriate proxy object and call it's method. @param permid: The permid of the peer who sent the message @param infohash: the infohash of the torrent for which relay is requested @param torrent_data: the content of the .torrent file """ # Create the name for the .torrent file in the proxy cache basename = binascii.hexlify(infohash) + '.torrent' torrentfilename = os.path.join(self.proxydir, basename) # Write the .torrent information in the .torrent proxy cache file tfile = open(torrentfilename, "wb") tfile.write(torrent_data) tfile.close() if DEBUG: print >> sys.stderr, "proxy: new_download: Got metadata required for relaying" print >> sys.stderr, "proxy: new_download: torrent: ", torrentfilename tdef = TorrentDef.load(torrentfilename) if self.dlconfig is None: dscfg = DownloadStartupConfig() else: dscfg = DownloadStartupConfig(self.dlconfig) dscfg.set_proxyservice_role(PROXYSERVICE_ROLE_PROXY) dscfg.set_dest_dir(self.proxydir) dscfg.set_doe_mode( DOE_MODE_OFF ) # a proxy does not use other proxies for downloading data in the current stage # Start new download if DEBUG: print >> sys.stderr, "proxy: new_download: Starting a new download" self.session.add_observer(self.proxydownloader_started, NTFY_PROXYDOWNLOADER, [NTFY_STARTED]) d = self.session.start_download(tdef, dscfg) d.set_state_callback(self.state_callback, getpeerlist=False)
def setUpPreSession(self): """ override TestAsServer """ super(TestSeeding, self).setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() # not really necess self.config2.set_state_dir(self.getStateDir(2)) self.dscfg2 = DownloadStartupConfig() self.dscfg2.set_dest_dir(self.getDestDir(2))
def _on_torrent_created(result): """ Success callback :param result: from create_torrent_file """ with open(result['torrent_file_path'], 'rb') as f: torrent_64 = base64.b64encode(f.read()) # Download this torrent if specified if 'download' in request.args and len(request.args['download']) > 0 \ and request.args['download'][0] == "1": download_config = DownloadStartupConfig() download_config.set_dest_dir(result['base_path'] if len(file_path_list) == 1 else result['base_dir']) try: self.session.start_download_from_uri('file:' + result['torrent_file_path'], download_config) except DuplicateDownloadException: self._logger.warning("The created torrent is already being downloaded.") request.write(json.dumps({"torrent": torrent_64})) # If the above request.write failed, the request will have already been finished if not request.finished: request.finish()
def create_torrent(self): [srchandle, sourcefn] = mkstemp() self.content = '0' * self.contentlen os.write(srchandle, self.content) os.close(srchandle) self.tdef = TorrentDef() self.tdef.add_content(sourcefn) self.tdef.set_piece_length(self.piecelen) self.tdef.set_tracker("http://127.0.0.1:12/announce") torrentfn = os.path.join(self.session.config.get_state_dir(), "gen.torrent") self.tdef.save(torrentfn) dscfg = DownloadStartupConfig() destdir = os.path.dirname(sourcefn) dscfg.set_dest_dir(destdir) dscfg.set_mode(DLMODE_VOD) download = self.session.start_download_from_tdef(self.tdef, dscfg) download.set_state_callback(self.state_callback) self.session.set_download_states_callback(self.states_callback)
def create_torrent(self): [srchandle, self.sourcefn] = mkstemp() self.content = Rand.rand_bytes(self.contentlen) os.write(srchandle, self.content) os.close(srchandle) self.tdef = TorrentDef() self.tdef.add_content(self.sourcefn) self.tdef.set_piece_length(self.piecelen) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() self.torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent") self.tdef.save(self.torrentfn) dscfg = DownloadStartupConfig() destdir = os.path.dirname(self.sourcefn) dscfg.set_dest_dir(destdir) dscfg.set_video_event_callback(self.sesscb_vod_event_callback) self.session.set_download_states_callback(self.states_callback) self.session.start_download(self.tdef, dscfg)
def _on_torrent_created(result): """ Success callback :param result: from create_torrent_file """ metainfo_dict = bdecode(result['metainfo']) # Download this torrent if specified if 'download' in request.args and len(request.args['download']) > 0 \ and request.args['download'][0] == "1": download_config = DownloadStartupConfig() download_config.set_dest_dir(result['base_path'] if len(file_path_list) == 1 else result['base_dir']) try: self.session.lm.ltmgr.start_download( tdef=TorrentDef(metainfo=metainfo_dict), dconfig=download_config) except DuplicateDownloadException: self._logger.warning("The created torrent is already being downloaded.") request.write(json.dumps({"torrent": base64.b64encode(result['metainfo'])})) # If the above request.write failed, the request will have already been finished if not request.finished: request.finish()
def setupSeeder(self): from Tribler.Core.Session import Session from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.DownloadConfig import DownloadStartupConfig self.setUpPreSession() self.config.set_libtorrent(True) self.session2 = Session(self.config, ignore_singleton=True) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(BASE_DIR, "data", "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.finalize() torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(os.path.join(BASE_DIR, "data")) # basedir of the file we are seeding self.session2.start_download(tdef, dscfg) return torrentfn
def download_channel(self, channel): """ Download a channel with a given infohash and title. :param channel: The channel metadata ORM object. """ dcfg = DownloadStartupConfig() dcfg.set_dest_dir(self.session.lm.mds.channels_dir) dcfg.set_channel_download(True) tdef = TorrentDefNoMetainfo(infohash=str(channel.infohash), name=channel.dir_name) download = self.session.start_download_from_tdef(tdef, dcfg) def on_channel_download_finished(dl): channel_dirname = os.path.join(self.session.lm.mds.channels_dir, dl.get_def().get_name()) self.session.lm.mds.process_channel_dir(channel_dirname, channel.public_key, external_thread=True) self.session.lm.mds._db.disconnect() def _on_failure(failure): self._logger.error("Error when processing channel dir download: %s", failure) finished_deferred = download.finished_deferred.addCallback( lambda dl: deferToThread(on_channel_download_finished, dl)) finished_deferred.addErrback(_on_failure) return download, finished_deferred
def on_torrent_insert(self, source_str, infohash, name): """ Callback function called by the source when a new torrent is discovered """ self._logger.debug('Received torrent %s from %s', infohash, source_str) if source_str not in self.sources: self._logger.debug('Skipping torrent %s (unknown source %s)', infohash, source_str) return # Did we already get this torrent from another source? if infohash in self.torrents: self.torrents[infohash].sources.add(source_str) self._logger.debug('Skipping torrent %s (already known)', infohash) return # If a download already exists or already has a checkpoint, skip this torrent if self.session.get_download(unhexlify(infohash)) or \ os.path.exists(os.path.join(self.session.get_downloads_pstate_dir(), infohash + '.state')): self._logger.debug('Skipping torrent %s (download already running or scheduled to run)', infohash) return if len(self.torrents) >= self.settings.max_torrents_listed: self._logger.debug('Skipping torrent %s (limit reached)', infohash) return self.torrents[infohash] = CreditMiningTorrent(infohash, name) self.torrents[infohash].sources.add(source_str) self._logger.info('Starting torrent %s', infohash) magnet = u'magnet:?xt=urn:btih:%s&dn=%s' % (infohash, name) dl_config = DownloadStartupConfig() dl_config.set_hops(self.settings.hops) dl_config.set_dest_dir(self.settings.save_path) dl_config.set_credit_mining(True) dl_config.set_user_stopped(True) self.session.lm.add(TorrentDefNoMetainfo(unhexlify(infohash), name, magnet), dl_config, hidden=True)
def setup_seeder(self): self.seeder_setup_complete = False self.seeder_teardown_complete = False self.seeder_teardown = False self.dscfg = DownloadStartupConfig() self.dscfg.set_dest_dir(os.getcwd()) self.download = self.session.start_download(self.tdef, self.dscfg) self.download.set_state_callback(self.seeder_state_callback) counter = 0 while not self.seeder_setup_complete: counter += 1 time.sleep(1) assert counter < 30, "timeout" print >> sys.stderr, "test: setup_seeder() complete"
def create_dconfig_from_params(parameters): """ Create a download configuration based on some given parameters. Possible parameters are: - anon_hops: the number of hops for the anonymous download. 0 hops is equivalent to a plain download - safe_seeding: whether the seeding of the download should be anonymous or not (0 = off, 1 = on) - destination: the destination path of the torrent (where it is saved on disk) """ download_config = DownloadStartupConfig() anon_hops = 0 if 'anon_hops' in parameters and len(parameters['anon_hops']) > 0: if parameters['anon_hops'][0].isdigit(): anon_hops = int(parameters['anon_hops'][0]) safe_seeding = False if 'safe_seeding' in parameters and len(parameters['safe_seeding']) > 0 \ and parameters['safe_seeding'][0] == "1": safe_seeding = True if anon_hops > 0 and not safe_seeding: return None, "Cannot set anonymous download without safe seeding enabled" if anon_hops > 0: download_config.set_hops(anon_hops) if safe_seeding: download_config.set_safe_seeding(True) if 'destination' in parameters and len(parameters['destination']) > 0: dest_dir = cast_to_unicode_utf8(parameters['destination'][0]) download_config.set_dest_dir(dest_dir) if 'selected_files[]' in parameters: selected_files_list = [cast_to_unicode_utf8(f) for f in parameters['selected_files[]']] download_config.set_selected_files(selected_files_list) return download_config, None
def setup_seeder(self, filename='video.avi'): self.tdef = TorrentDef() self.sourcefn = os.path.join(TESTS_API_DIR, filename) self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://fake.net/announce") self.tdef.finalize() self.torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent") self.tdef.save(self.torrentfn) self._logger.debug("name is %s", self.tdef.metainfo['info']['name']) self.dscfg = DownloadStartupConfig() self.dscfg.set_dest_dir(TESTS_API_DIR) # basedir of the file we are seeding d = self.session.start_download(self.tdef, self.dscfg) d.set_state_callback(self.seeder_state_callback) self._logger.debug("starting to wait for download to reach seeding state") assert self.seeding_event.wait(60)
def new_download(self, permid, infohash, torrent_data): """ Start a new download in order to get the pieces that will be requested by the doe. After the download is started, find the appropriate proxy object and call it's method. @param permid: The permid of the peer who sent the message @param infohash: the infohash of the torrent for which relay is requested @param torrent_data: the content of the .torrent file """ # Create the name for the .torrent file in the proxy cache basename = binascii.hexlify(infohash) + ".torrent" torrentfilename = os.path.join(self.proxydir, basename) # Write the .torrent information in the .torrent proxy cache file tfile = open(torrentfilename, "wb") tfile.write(torrent_data) tfile.close() if DEBUG: print >>sys.stderr, "proxy: new_download: Got metadata required for relaying" print >>sys.stderr, "proxy: new_download: torrent: ", torrentfilename tdef = TorrentDef.load(torrentfilename) if self.dlconfig is None: dscfg = DownloadStartupConfig() else: dscfg = DownloadStartupConfig(self.dlconfig) dscfg.set_proxyservice_role(PROXYSERVICE_ROLE_PROXY) dscfg.set_dest_dir(self.proxydir) dscfg.set_doe_mode(DOE_MODE_OFF) # a proxy does not use other proxies for downloading data in the current stage # Start new download if DEBUG: print >>sys.stderr, "proxy: new_download: Starting a new download" self.session.add_observer(self.proxydownloader_started, NTFY_PROXYDOWNLOADER, [NTFY_STARTED]) d = self.session.start_download(tdef, dscfg) d.set_state_callback(self.state_callback, getpeerlist=False)