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 = DownloadConfig() anon_hops = parameters.get('anon_hops', 0) safe_seeding = bool(parameters.get('safe_seeding', 0)) 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 parameters.get('destination'): dest_dir = parameters['destination'] download_config.set_dest_dir(dest_dir) if 'selected_files' in parameters: download_config.set_selected_files(parameters['selected_files']) return download_config, None
def check_watch_folder(self): if not self.session.config.get_watch_folder_path().is_dir(): return # Make sure that we pass a str to os.walk watch_dir = str(self.session.config.get_watch_folder_path()) for root, _, files in os.walk(watch_dir): root = path_util.Path(root) for name in files: if not name.endswith(".torrent"): continue try: tdef = TorrentDef.load(root / name) if not tdef.get_metainfo(): self.cleanup_torrent_file(root, name) continue except: # torrent appears to be corrupt self.cleanup_torrent_file(root, name) continue infohash = tdef.get_infohash() if not self.session.dlmgr.download_exists(infohash): self._logger.info("Starting download from torrent file %s", name) dl_config = DownloadConfig() anon_enabled = self.session.config.get_default_anonymity_enabled() default_num_hops = self.session.config.get_default_number_hops() dl_config.set_hops(default_num_hops if anon_enabled else 0) dl_config.set_safe_seeding(self.session.config.get_default_safeseeding_enabled()) dl_config.set_dest_dir(self.session.config.get_default_destination_dir()) self.session.dlmgr.start_download(tdef=tdef, config=dl_config)
def test_downloadconfig(self): dlcfg = DownloadConfig() self.assertIsInstance(dlcfg.get_dest_dir(), Path) dlcfg.set_dest_dir(self.session_base_dir) self.assertEqual(dlcfg.get_dest_dir(), self.session_base_dir) dlcfg.set_hops(4) self.assertEqual(dlcfg.get_hops(), 4) dlcfg.set_safe_seeding(False) self.assertFalse(dlcfg.get_safe_seeding()) dlcfg.set_selected_files([1]) self.assertEqual(dlcfg.get_selected_files(), [1]) dlcfg.set_channel_download(True) self.assertTrue(dlcfg.get_channel_download()) dlcfg.set_add_to_channel(True) self.assertTrue(dlcfg.get_add_to_channel()) dlcfg.set_bootstrap_download(True) self.assertTrue(dlcfg.get_bootstrap_download())
class Bootstrap(TaskManager): """ A class to create a bootstrap downloads for inital file aka bootstrap file. Bootstrap class will be initialized at the start of Tribler by downloading/seeding bootstrap file. """ def __init__(self, config_dir, dht=None): super(Bootstrap, self).__init__() self._logger = logging.getLogger(self.__class__.__name__) self.dcfg = DownloadConfig(state_dir=config_dir) self.dcfg.set_bootstrap_download(True) self.bootstrap_dir = config_dir / 'bootstrap' if not self.bootstrap_dir.exists(): os.mkdir(self.bootstrap_dir) self.dcfg.set_dest_dir(self.bootstrap_dir) self.dcfg.set_safe_seeding(True) self.bootstrap_file = self.bootstrap_dir / "bootstrap.blocks" self.dht = dht self.bootstrap_finished = False self.infohash = None self.download = None self.bootstrap_nodes = {} self.register_task('fetch_bootstrap_peers', self.fetch_bootstrap_peers, interval=5) def start_by_infohash(self, download_function, infohash): """ Download bootstrap file from current seeders :param download_function: function to download via tdef :return: download on bootstrap file """ self._logger.debug("Starting bootstrap downloading %s", infohash) tdef = TorrentDefNoMetainfo(unhexlify(infohash), name='bootstrap.blocks') self.download = download_function(tdef=tdef, config=self.dcfg, hidden=True) self.infohash = infohash async def fetch_bootstrap_peers(self): if not self.download: return {} for peer in self.download.get_peerlist(): mid = peer['id'] if (mid not in self.bootstrap_nodes or not self.bootstrap_nodes[mid]) and mid != "0" * 40: if self.dht: try: nodes = await self.dht.connect_peer( bytes(unhexlify(mid))) except DHTError as e: self._logger.error("Failed to get DHT response:%s", e) continue if not nodes: return for node in nodes: self.bootstrap_nodes[hexlify(node.mid)] = hexlify( node.public_key.key_to_bin()) return self.bootstrap_nodes async def shutdown(self): await self.shutdown_task_manager()