def __init__(self): self.session = lt.session() self.session.listen_on(6881, 6891) self.session.set_alert_mask( lt.alert.category_t.error_notification | lt.alert.category_t.storage_notification | lt.alert.category_t.status_notification | lt.alert.category_t.progress_notification | lt.alert.category_t.performance_warning) self.torrents = {} self.limits_updated = False settings = lt.session_settings() # TODO: set upload ratio settings, rate limits, etc settings.user_agent = 'Downpour/%s libtorrent/%d.%d' % (VERSION, lt.version_major, lt.version_minor) # settings.share_ratio_limit = float(self.manager.get_setting('upload_ratio', 0)) self.session.set_settings(settings) # Update torrent status self.status_update = task.LoopingCall(self.status_update) self.status_update.start(2.0) # Process generated alerts self.alert_monitor = task.LoopingCall(self.process_alerts) self.alert_monitor.start(2.0) # Update rate / connection limits self.limit_update = task.LoopingCall(self.limit_update) self.limit_update.start(5.0)
def init_libtorrent(self, max_download_speed=0, max_upload_speed=0): """Perform the initialization of things that should be initialized once""" if self.session: return settings = libtorrent.session_settings() settings.user_agent = encode_utf8( 'Torrent Launcher (libtorrent/{})'.format( decode_utf8(libtorrent.version))) """When running on a network where the bandwidth is in such an abundance that it's virtually infinite, this algorithm is no longer necessary, and might even be harmful to throughput. It is adviced to experiment with the session_setting::mixed_mode_algorithm, setting it to session_settings::prefer_tcp. This setting entirely disables the balancing and unthrottles all connections.""" settings.mixed_mode_algorithm = 0 # Fingerprint = 'LT1080' == LibTorrent 1.0.8.0 fingerprint = libtorrent.fingerprint( b'LT', *(int(i) for i in libtorrent.version.split('.'))) self.session = libtorrent.session(fingerprint=fingerprint) self.session.listen_on( 6881, 6891 ) # This is just a port suggestion. On failure, the port is automatically selected. # Prevent conversion to C int error settings.download_rate_limit = min(max_download_speed, 999999) * 1024 settings.upload_rate_limit = min(max_upload_speed, 999999) * 1024 self.session.set_settings(settings)
def _get_session(createIfNeeded=True): global _lt_sess if _lt_sess is None and createIfNeeded: _lt_sess = lt.session() _lt_sess.set_download_rate_limit(sickbeard.LIBTORRENT_MAX_DL_SPEED * 1024) _lt_sess.set_upload_rate_limit(sickbeard.LIBTORRENT_MAX_UL_SPEED * 1024) settings = lt.session_settings() settings.user_agent = 'sickbeard_bricky-%s/%s' % (version.SICKBEARD_VERSION.replace(' ', '-'), lt.version) settings.rate_limit_utp = True # seems this is rqd, otherwise uTP connections don't obey the rate limit settings.active_downloads = 8 settings.active_seeds = 12 settings.active_limit = 20 _lt_sess.listen_on(sickbeard.LIBTORRENT_PORT_MIN, sickbeard.LIBTORRENT_PORT_MAX) _lt_sess.set_settings(settings) _lt_sess.set_alert_mask(lt.alert.category_t.error_notification | #lt.alert.category_t.port_mapping_notification | lt.alert.category_t.storage_notification | #lt.alert.category_t.tracker_notification | lt.alert.category_t.status_notification | #lt.alert.category_t.port_mapping_notification | lt.alert.category_t.performance_warning ) try: state = {} # @todo: save/restore this _lt_sess.start_dht(state) _lt_sess.add_dht_router('router.bittorrent.com', 6881) _lt_sess.add_dht_router('router.utorrent.com', 6881) _lt_sess.add_dht_router('router.bitcomet.com', 6881) except Exception, ex: # just ignore any dht errors, this is just for bootstrapping logger.log(u'Exception starting dht: ' + str(ex), logger.WARNING)
def __init__(self): '''Starts a bittorrent client''' log.info('Starting bittorrent client') # Settings self.session = lt.session() session_settings = lt.session_settings() session_settings.user_agent = '%s libtorrent/%d.%d' % (settings.SOFTWARE_USER_AGENT, lt.version_major, lt.version_minor) session_settings.active_downloads = settings.BITTORRENT_MAX_DOWNLOADS + settings.BITTORRENT_MAX_METADATA_DOWNLOADS session_settings.active_seeds = settings.BITTORRENT_MAX_SEEDS session_settings.active_limit = settings.BITTORRENT_MAX_DOWNLOADS + settings.BITTORRENT_MAX_SEEDS + settings.BITTORRENT_MAX_METADATA_DOWNLOADS self.session.set_settings(session_settings) # Start BT server ports = settings.BITTORRENT_PORTS self.session.listen_on(ports[0], ports[1]) # Start DHT server dht_data = get_cache('bt_dht_data') self.session.start_dht(dht_data) self.params = { 'save_path': settings.DOWNLOAD_DIR.encode('ascii'), # FIXME: support non-ascii characters 'storage_mode': lt.storage_mode_t(2), 'paused': False, 'auto_managed': True, 'duplicate_is_error': True} self.handle_dict = {}
def _init_session(self): sts = lt.session_settings() sts.ssl_listen = False sts.user_agent = "vtrans system" sts.tracker_completion_timeout = 5 sts.tracker_receive_timeout = 5 sts.stop_tracker_timeout = 5 sts.active_downloads = -1 sts.active_seeds = -1 sts.active_limit = -1 sts.auto_scrape_min_interval = 5 sts.udp_tracker_token_expiry = 120 sts.min_announce_interval = 1 sts.inactivity_timeout = 60 sts.connection_speed = 10 sts.allow_multiple_connections_per_ip = True sts.max_out_request_queue = 128 sts.request_queue_size = 3 #sts.use_read_cache = False ##############test##################### sts.use_read_cache = True #sts.disable_hash_cache = True #sts.no_recheck_incomplete_resume = True #broken-resume switch ####################################### self.session.set_settings(sts) self.session.set_alert_mask(lt.alert.category_t.tracker_notification | lt.alert.category_t.status_notification) self.session.set_alert_mask(lt.alert.category_t.status_notification) print "libtorrent session listen on %s to %s" % (TORRENT_BEGIN_PORT, TORRENT_END_PORT) self.session.listen_on(TORRENT_BEGIN_PORT, TORRENT_END_PORT) upload_rate_limit = 0 if int(upload_rate_limit) >= 100: self.session.set_upload_rate_limit(upload_rate_limit * 1024) self.session.set_local_upload_rate_limit(upload_rate_limit * 1024)
def get_torrent_info_magnet(v1, v3, u, p_bar, tmp_dir): global handle, ses, info, cnt, cnt_limit, file_name, ui, progress, tmp_dir_folder ui = u progress = p_bar tmp_dir_folder = tmp_dir progress.setValue(0) progress.show() sett = lt.session_settings() sett.user_agent = 'qBittorrent v3.3.5' sett.always_send_user_agent = True fingerprint = lt.fingerprint('qB', 3, 3, 5, 0) ses = lt.session(fingerprint) ses.listen_on(40000, 50000) ses.set_settings(sett) handle = lt.add_magnet_uri(ses, v1, {'save_path': v3}) i = 0 while (not handle.has_metadata()): time.sleep(1) i = i + 1 print('finding metadata {0}'.format(i)) if i > 300: print('No Metadata Available: {0}s'.format(i)) break info = handle.get_torrent_info() handle.set_sequential_download(True) print(handle.trackers()) return handle, ses, info
def get_torrent_info_magnet(v1,v3,u,p_bar,tmp_dir): global handle,ses,info,cnt,cnt_limit,file_name,ui,progress,tmp_dir_folder ui = u progress = p_bar tmp_dir_folder = tmp_dir progress.setValue(0) progress.show() #print(v1,'------------hello----------info---') sett = lt.session_settings() sett.user_agent = 'qBittorrent v3.3.5' sett.always_send_user_agent = True fingerprint = lt.fingerprint('qB',3,3,5,0) ses = lt.session(fingerprint) ses.listen_on(40000, 50000) ses.set_settings(sett) handle = lt.add_magnet_uri(ses,v1,{'save_path':v3}) i = 0 while (not handle.has_metadata()): time.sleep(1) i = i+1 print('finding metadata') if i > 300: print('No Metadata Available') break info = handle.get_torrent_info() handle.set_sequential_download(True) print(handle.trackers()) return handle,ses,info
def get_torrent_info_magnet(v1, v3, u): global handle, ses, info, cnt, cnt_limit, file_name, ui ui = u #print(v1,'------------hello----------info---') sett = lt.session_settings() sett.user_agent = 'qBittorrent v3.3.5' sett.always_send_user_agent = True fingerprint = lt.fingerprint('qB', 3, 3, 5, 0) ses = lt.session(fingerprint) ses.listen_on(40000, 50000) ses.set_settings(sett) handle = lt.add_magnet_uri(ses, v1, {'save_path': v3}) i = 0 while (not handle.has_metadata()): time.sleep(1) i = i + 1 print('finding metadata') if i > 300: print('No Metadata Available') break info = handle.get_torrent_info() handle.set_sequential_download(True) print(handle.trackers()) return handle, ses, info
def __init__(self, path_to_store, args=None, state_file="~/.btclient_state", **kwargs): super(BTClient,self).__init__(path_to_store, args=args) self._torrent_params={'save_path':path_to_store, 'storage_mode':lt.storage_mode_t.storage_mode_sparse } self._state_file=os.path.expanduser(state_file) self._ses=lt.session() if os.path.exists(self._state_file): with open(self._state_file) as f: state=pickle.load(f) self._ses.load_state(state) #self._ses.set_alert_mask(lt.alert.category_t.progress_notification) if args: s=lt.session_settings() s.download_rate_limit=int(round(args.bt_download_limit*1024)) s.upload_rate_limit=int(round(args.bt_upload_limit*1024)) self._ses.set_settings(s) self._ses.listen_on(args.listen_port_min, args.listen_port_max) else: self._ses.listen_on(6881, 6891) self._start_services() self._th=None self._monitor.add_listener(self._check_ready) self._dispatcher=BTClient.Dispatcher(self) self._dispatcher.add_listener(self._update_ready_pieces) self._hash = None self._url=None if args and args.debug_log and args.trace: self.add_monitor_listener(self.debug_download_queue) self.add_dispatcher_listener(self.debug_alerts)
def __init__(self, trsession, ignore_singleton=False): if not ignore_singleton: if LibtorrentMgr.__single: raise RuntimeError("LibtorrentMgr is singleton") LibtorrentMgr.__single = self self.trsession = trsession settings = lt.session_settings() settings.user_agent = 'Tribler/' + version_id # Elric: Strip out the -rcX, -beta, -whatever tail on the version string. fingerprint = ['TL'] + map(int, version_id.split('-')[0].split('.')) + [0] # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369) self.ltsession = lt.session(lt.fingerprint(*fingerprint), flags=1) self.ltsession.set_settings(settings) self.ltsession.set_alert_mask( lt.alert.category_t.stats_notification | lt.alert.category_t.error_notification | lt.alert.category_t.status_notification | lt.alert.category_t.storage_notification | lt.alert.category_t.performance_warning | lt.alert.category_t.debug_notification) self.ltsession.listen_on(self.trsession.get_listen_port(), self.trsession.get_listen_port() + 10) self.set_upload_rate_limit(-1) self.set_download_rate_limit(-1) self.upnp_mapper = self.ltsession.start_upnp() print >> sys.stderr, "LibtorrentMgr: listening on %d" % self.ltsession.listen_port( ) # Start DHT self.dht_ready = False try: dht_state = open( os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME)).read() self.ltsession.start_dht(lt.bdecode(dht_state)) except: print >> sys.stderr, "LibtorrentMgr: could not restore dht state, starting from scratch" self.ltsession.start_dht(None) self.ltsession.add_dht_router('router.bittorrent.com', 6881) self.ltsession.add_dht_router('router.utorrent.com', 6881) self.ltsession.add_dht_router('router.bitcomet.com', 6881) self.torlock = NoDispersyRLock() self.torrents = {} self.metainfo_requests = {} self.metainfo_lock = threading.RLock() self.metainfo_cache = {} self.trsession.lm.rawserver.add_task(self.process_alerts, 1) self.trsession.lm.rawserver.add_task(self.reachability_check, 1) self.trsession.lm.rawserver.add_task(self.monitor_dht, 5) self.upnp_mappings = {}
def download(torrent, path, upload_rate_limit=0, seedtime=100): try: session = lt.session() session.set_alert_queue_size_limit(1024 * 1024) sts = lt.session_settings() sts.ssl_listen = False sts.user_agent = "Thunder deploy system" sts.tracker_completion_timeout = 5 sts.tracker_receive_timeout = 5 sts.stop_tracker_timeout = 5 sts.active_downloads = -1 sts.active_seeds = -1 sts.active_limit = -1 sts.auto_scrape_min_interval = 5 sts.udp_tracker_token_expiry = 120 sts.min_announce_interval = 1 sts.inactivity_timeout = 60 sts.connection_speed = 10 sts.allow_multiple_connections_per_ip = True sts.max_out_request_queue = 128 sts.request_queue_size = 3 sts.use_read_cache = False session.set_settings(sts) session.set_alert_mask(lt.alert.category_t.tracker_notification | lt.alert.category_t.status_notification) session.set_alert_mask(lt.alert.category_t.status_notification) ipaddr = get_wan_ip_address() #print ipaddr if ipaddr == "": session.listen_on(6881, 6881) else: session.listen_on(6881, 6881, ipaddr) limit = int(upload_rate_limit) if limit>=100: session.set_upload_rate_limit(limit*1024) session.set_local_upload_rate_limit(limit*1024) print session.upload_rate_limit() torrent_info = lt.torrent_info(torrent) add_params = { 'save_path': path, 'storage_mode': lt.storage_mode_t.storage_mode_sparse, 'paused': False, 'auto_managed': True, 'ti': torrent_info, } handle = session.add_torrent(add_params) read_alerts(session) st = time.time() dl_status(handle) et = time.time() - st print '\nall file download in %.2f\nstart to seeding\n' % et sys.stdout.write('\n') handle.super_seeding() seed_status(handle, seedtime) remove_torrents(handle, session) assert len(session.get_torrents()) == 0 finally: print 'download finished'
def create_session(self, hops=0): settings = lt.session_settings() if hops == 0: settings.user_agent = 'Tribler/' + version_id # Elric: Strip out the -rcX, -beta, -whatever tail on the version string. fingerprint = ['TL'] + map(int, version_id.split('-')[0].split('.')) + [0] # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369) ltsession = lt.session(lt.fingerprint(*fingerprint), flags=1) enable_utp = self.trsession.get_libtorrent_utp() settings.enable_outgoing_utp = enable_utp settings.enable_incoming_utp = enable_utp pe_settings = lt.pe_settings() pe_settings.prefer_rc4 = True ltsession.set_pe_settings(pe_settings) else: settings.enable_outgoing_utp = True settings.enable_incoming_utp = True settings.enable_outgoing_tcp = False settings.enable_incoming_tcp = False settings.anonymous_mode = True # No PEX for anonymous sessions ltsession = lt.session(flags=0) ltsession.add_extension(lt.create_ut_metadata_plugin) ltsession.add_extension(lt.create_smart_ban_plugin) ltsession.set_settings(settings) ltsession.set_alert_mask(lt.alert.category_t.stats_notification | lt.alert.category_t.error_notification | lt.alert.category_t.status_notification | lt.alert.category_t.storage_notification | lt.alert.category_t.performance_warning | lt.alert.category_t.tracker_notification) # Load proxy settings if hops == 0: proxy_settings = self.trsession.get_libtorrent_proxy_settings() else: proxy_settings = list(self.trsession.get_anon_proxy_settings()) proxy_host, proxy_ports = proxy_settings[1] proxy_settings[1] = (proxy_host, proxy_ports[hops - 1]) self.set_proxy_settings(ltsession, *proxy_settings) # Set listen port & start the DHT if hops == 0: listen_port = self.trsession.get_listen_port() ltsession.listen_on(listen_port, listen_port + 10) if listen_port != ltsession.listen_port(): self.trsession.set_listen_port_runtime(ltsession.listen_port()) try: dht_state = open(os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME)).read() ltsession.start_dht(lt.bdecode(dht_state)) except Exception, exc: self._logger.info("could not restore dht state, got exception: %r. starting from scratch" % exc) ltsession.start_dht(None)
def test_deprecated_settings(self): # this detects whether libtorrent was built with deprecated APIs if hasattr(lt, 'version'): s = lt.session({'enable_dht': False}) sett = lt.session_settings() sett.num_want = 10 s.set_settings(sett) s.set_settings({'num_want': 33}) self.assertEqual(s.get_settings()['num_want'], 33)
def test_deprecated_settings(self): # this detects whether libtorrent was built with deprecated APIs if hasattr(lt, 'version'): s = lt.session({}) sett = lt.session_settings() sett.num_want = 10; s.set_settings(sett) s.set_settings({'num_want': 33}) self.assertEqual(s.get_settings()['num_want'], 33)
def storeSettings(self, path): obj = {} settings = libtorrent.session_settings() obj['download_rate_limit'] = int(settings.download_rate_limit) obj['upload_rate_limit'] = int(settings.upload_rate_limit) data = json.dumps(obj) file = open(path, 'wb') file.write(data) file.close()
def storeSettings(self, path): obj = {} settings = libtorrent.session_settings() obj['download_rate_limit'] = int(settings.download_rate_limit) obj['upload_rate_limit'] = int(settings.upload_rate_limit) data = json.dumps(obj) file = open(path,'wb') file.write(data) file.close()
def open_torrent(self, path, magnet=None): if self.torrenthandle is not None: print 'Another torrent is already in progress' return self.torrent_session = lt.session() self.torrent_session.listen_on(6881, 6891) # Allocation settings (these should be default but make sure they are correct) settings = lt.session_settings() settings.close_redundant_connections = False # This keeps peers connected settings.disk_io_write_mode = lt.io_buffer_mode_t.enable_os_cache settings.disk_io_read_mode = lt.io_buffer_mode_t.enable_os_cache self.torrent_session.set_settings(settings) e = None torrentinfo = None if magnet: params = {"save_path": self.download_directory,"allocation": lt.storage_mode_t.storage_mode_sparse,} self.torrenthandle = lt.add_magnet_uri(self.torrent_session, str(magnet), params) torrentinfo = self.torrenthandle.get_torrent_info() else: e = lt.bdecode(open(path, 'rb').read()) torrentinfo = lt.torrent_info(e) self.torrenthandle = self.torrent_session.add_torrent({'ti': torrentinfo, 'save_path': self.download_directory, 'storage_mode': lt.storage_mode_t.storage_mode_sparse}) self.video_file = self._find_video_file(torrentinfo.files()) # Disable all files, we do not want to download yet. Download starts when torrent.startTorrent() is called # for n in range(0, torrentInfo.num_files()): num_files_skipped = [0] * torrentinfo.num_files() self.torrenthandle.prioritize_files(num_files_skipped) # Print some torrent stats print 'Torrent piece size:', torrentinfo.piece_size(0) / 1024, 'kB' print 'Torrent total pieces:', torrentinfo.num_pieces() print 'Torrent total files:', torrentinfo.num_files() print 'Video file offset pieces:', self.num_video_offset_pieces self.num_total_pieces = torrentinfo.num_pieces() self._create_torrent() # start alert thread thread_alert = threading.Thread(target=self._thread_alert) thread_alert.daemon = True thread_alert.start() return self.download_directory + self.video_file.path
def __init__(self, trsession, ignore_singleton=False): if not ignore_singleton: if LibtorrentMgr.__single: raise RuntimeError("LibtorrentMgr is singleton") LibtorrentMgr.__single = self self.trsession = trsession settings = lt.session_settings() settings.user_agent = 'Tribler/' + version_id # Elric: Strip out the -rcX, -beta, -whatever tail on the version string. fingerprint = ['TL'] + map(int, version_id.split('-')[0].split('.')) + [0] # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369) self.ltsession = lt.session(lt.fingerprint(*fingerprint), flags=1) self.ltsession.set_settings(settings) self.ltsession.set_alert_mask(lt.alert.category_t.stats_notification | lt.alert.category_t.error_notification | lt.alert.category_t.status_notification | lt.alert.category_t.storage_notification | lt.alert.category_t.performance_warning | lt.alert.category_t.debug_notification) self.ltsession.listen_on(self.trsession.get_listen_port(), self.trsession.get_listen_port() + 10) self.set_upload_rate_limit(-1) self.set_download_rate_limit(-1) self.upnp_mapper = self.ltsession.start_upnp() print >> sys.stderr, "LibtorrentMgr: listening on %d" % self.ltsession.listen_port() # Start DHT self.dht_ready = False try: dht_state = open(os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME)).read() self.ltsession.start_dht(lt.bdecode(dht_state)) except: print >> sys.stderr, "LibtorrentMgr: could not restore dht state, starting from scratch" self.ltsession.start_dht(None) self.ltsession.add_dht_router('router.bittorrent.com', 6881) self.ltsession.add_dht_router('router.utorrent.com', 6881) self.ltsession.add_dht_router('router.bitcomet.com', 6881) self.torlock = NoDispersyRLock() self.torrents = {} self.metainfo_requests = {} self.metainfo_lock = threading.RLock() self.metainfo_cache = {} self.trsession.lm.rawserver.add_task(self.process_alerts, 1) self.trsession.lm.rawserver.add_task(self.reachability_check, 1) self.trsession.lm.rawserver.add_task(self.monitor_dht, 5) self.upnp_mappings = {}
def __init__(self, ratio=2.0, ports=[6881, 6891]): # create session self.session = lt.session() self.session.add_dht_router("router.bittorrent.com", 6881) self.session.start_dht() self.session.listen_on(*ports) self.session.start_upnp() self.session.start_natpmp() self.session.start_lsd() self.downloads = {} self.files = {} self.upload_total = {} self.download_total = {} self.folder = config["settings"]["downloads"] # begin thread to watch downloads thread = threading.Thread(target=self._watch_thread) thread.daemon = True thread.start() # set session options self.ratio = ratio settings = lt.session_settings() settings.share_ratio_limit = ratio self.session.set_settings(settings) self.session.set_alert_mask( lt.alert.category_t.error_notification | lt.alert.category_t.status_notification ) self._progress_ticker = 0 # reload persistent data try: with file(libtorrent_file) as f: data = yaml.load(f) for torrent in data['torrents']: self._add_torrent(torrent['torrent']) self.upload_total[torrent['torrent']] = torrent['up'] self.download_total[torrent['torrent']] = torrent['down'] self.session.load_state(data['state']) except IOError: self.log.debug("libtorrent.yaml not found") else: self.log.debug("Loaded from libtorrent.yaml") self.session.resume()
def __init__(self): super(TorrentDownloader, self).__init__() prefs = podpyclient.settings.Settings() self.ports = prefs.torrent_port_start self.port_range = prefs.torrent_port_range self.max_download_rate = prefs.torrent_download_rate self.max_upload_rate = prefs.torrent_download_rate self.save_path = prefs.savePath self.handles = {} torrent_settings = lt.session_settings() self.ses = lt.session() self.ses.set_download_rate_limit(int(self.max_download_rate)) self.ses.set_upload_rate_limit(int(self.max_upload_rate)) self.ses.listen_on(self.ports, self.ports + self.port_range) self.ses.set_settings(torrent_settings) self.ses.set_alert_mask(0xfffffff)
def __init__(self): self.handles = [] self.ses = lt.session() self.ses.listen_on(6881, 6891) """ Settings - Find a full list here: https://github.com/arvidn/libtorrent/blob/master/bindings/python/src/session_settings.cpp self.settings.<some_setting> = <some_setting_value> """ self.settings = lt.session_settings() self.settings.file_checks_delay_per_block = 1 self.ses.set_settings(self.settings) self.ses.start_dht() self.params = { 'save_path': '/home/pi/downloads/', 'storage_mode': lt.storage_mode_t(2), 'paused': False, 'auto_managed': True, 'duplicate_is_error': True }
def __init__(self, cache_path): super(TorrentController, self).__init__() settings = libtorrent.session_settings() settings.user_agent = 'torrent_browser/' + libtorrent.version settings.use_dht_as_fallback = False settings.volatile_read_cache = False ses = libtorrent.session() ses.set_download_rate_limit(0) ses.set_upload_rate_limit(0) ses.listen_on(6881, 6891) ses.set_alert_mask(0xfffffff) ses.set_settings(settings) #start local source discovery ses.start_lsd() #start distributed hash table ses.start_dht() #start router port forwarding ses.start_upnp() ses.start_natpmp() #add bootstrapping nodes ses.add_dht_router("router.bittorrent.com", 6881) ses.add_dht_router("router.utorrent.com", 6881) ses.add_dht_router("router.bitcomet.com", 6881) self.session = ses self.downloads = list() self.alive = True self.cache_path = cache_path print("(I) Cache directory: {}".format(cache_path)) if not os.path.exists(self.cache_path): os.makedirs(self.cache_path) self.timer = QTimer() self.timer.timeout.connect(self.sendUpdates) self.timer.start(500)
def run(self): if self.session is None: self.session = lt.session() self.session.set_alert_mask(lt.alert.category_t.all_categories) #self.session.set_dht_settings() for i in self._subsystems: getattr(self.session, "start_%s" % i)() for i in self._extensions: self.session.add_extension(getattr(lt, "create_%s_plugin" % i)) settings = lt.session_settings() settings.use_parole_mode = True settings.prioritize_partial_pieces = True settings.prefer_udp_trackers = True settings.user_agent = '%s/%s libtorrent/%d.%d' % ( self.appname, self.appversion, lt.version_major, lt.version_minor) # settings.share_ratio_limit = float(self.manager.get_setting('upload_ratio', 0)) settings.use_dht_as_fallback = False # Use DHT for tracker torrent too settings.ignore_resume_timestamps = True # Allow resume settings.announce_to_all_trackers = True # Announce to all trackers by tier settings.announce_to_all_tiers = True # like uTorrent self.session.set_settings(settings) self.config.on("max_downspeed", lambda k, v: self._set_max_downspeed(v)) self.config.on("max_upspeed", lambda k, v: self._set_max_upspeed(v)) self.config.on("max_active_downloads", lambda k, v: self._set_max_active_downloads(v)) self.config.on("max_connections", lambda k, v: self._set_max_connections(v)) self.config.on("max_half_open_connections", lambda k, v: self._set_max_half_open_connections(v)) self.config.on("port_%s_0" % self.name, lambda k, v: self.set_port(0, v)) self.torrent_resume_data = [] self.num_flushes = 0 self.set_port(0, self._port)
def __init__(self, trsession, utility): if LibtorrentMgr.__single: raise RuntimeError, "LibtorrentMgr is singleton" LibtorrentMgr.__single = self self.utility = utility self.trsession = trsession settings = lt.session_settings() settings.user_agent = 'Tribler/' + version_id fingerprint = ['TL'] + map(int, version_id.split('.')) + [0] # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369) self.ltsession = lt.session(lt.fingerprint(*fingerprint), flags=1) self.ltsession.set_settings(settings) self.ltsession.set_alert_mask( lt.alert.category_t.stats_notification | lt.alert.category_t.error_notification | lt.alert.category_t.status_notification | lt.alert.category_t.storage_notification | lt.alert.category_t.performance_warning) self.ltsession.listen_on(self.trsession.get_listen_port(), self.trsession.get_listen_port() + 10) self.set_upload_rate_limit(-1) self.set_download_rate_limit(-1) self.ltsession.start_upnp() # Start DHT try: dht_state = open( os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME)).read() self.ltsession.start_dht(lt.bdecode(dht_state)) except: print >> sys.stderr, "LibtorrentMgr: could not restore dht state, starting from scratch" self.ltsession.start_dht(None) self.ltsession.add_dht_router('router.bittorrent.com', 6881) self.ltsession.add_dht_router('router.utorrent.com', 6881) self.ltsession.add_dht_router('router.bitcomet.com', 6881) self.torlock = NoDispersyRLock() self.torrents = {} self.trsession.lm.rawserver.add_task(self.process_alerts, 1) self.trsession.lm.rawserver.add_task(self.reachability_check, 1)
def download_files(torrents, save_path='.'): """Download the torrents using libtorrent""" def all_seeding(handles): """Are all torrents added to the session currently seeding.""" for handle in handles: status = handle.status() if not status.is_seeding: return False return True # This is optional so we're importing libtorrent here so it's not mandatory import libtorrent as lt session = lt.session() # Don't be nice with the bandwidth. We are a hardcore seedbox after all... settings = lt.session_settings() settings.mixed_mode_algorithm = 0 session.set_settings(settings) handles = [] for torrent in torrents: info = lt.torrent_info(torrent) handle = session.add_torrent({'ti': info, 'save_path': save_path}) status = handle.status() print('starting', status) handles.append(handle) while not all_seeding(handles): status = session.status() # Print alerts, just in case alerts = session.pop_alerts() for a in alerts: if a.category() & lt.alert.category_t.error_notification: print(a) print('Download speed: {0:.2f}MB/s'.format( status.payload_download_rate / (1024 * 1024))) time.sleep(1)
def ltstart(self, irc, msg, args): """Start the libtorrent session and schedules""" if hasattr(self, 'ltses'): irc.error('Already running') return settings = lt.session_settings() settings.user_agent = 'ShareBot/1.3' settings.rate_limit_ip_overhead = False self.ltses = lt.session(flags=0) self.ltses.add_extension('ut_metadata') self.ltses.add_extension('metadata_transfer') self.ltses.set_settings(settings) self.ltses.listen_on(27622, 27632) self.ltses.set_alert_mask(lt.alert.category_t.status_notification) schedule.addPeriodicEvent(self._checkmetadata, 2, 'checkmetadata', False, args=[irc]) schedule.addPeriodicEvent(self._expiretorrents, 300, 'expiretorrents', False, args=[irc]) irc.replySuccess()
def download_torrent(torrent_file, download_throttle, upload_throttle): global torrent_handle settings = libtorrent.session_settings() settings.user_agent = 'Torrent_launcher_simple_client (libtorrent/{})'.format( libtorrent.version) session = libtorrent.session() session.listen_on(6881, 6891) session.set_settings(settings) if upload_throttle is not None: session.set_upload_rate_limit(int(upload_throttle) * 1024) if download_throttle is not None: session.set_download_rate_limit(int(download_throttle) * 1024) with open(torrent_file, 'rb') as file_handle: file_contents = file_handle.read() torrent_metadata = libtorrent.bdecode(file_contents) torrent_info = libtorrent.torrent_info(torrent_metadata) params = { 'save_path': os.getcwd(), 'storage_mode': libtorrent.storage_mode_t. storage_mode_allocate, # Reduce fragmentation on disk 'ti': torrent_info } torrent_handle = session.add_torrent(params) while True: s = torrent_handle.status() print "{} | {:.2%}\tUpload: {}KB\tDownload: {}KB".format( s.state, s.progress, s.upload_rate / 1024, s.download_rate / 1024) print_session_logs(session) time.sleep(1)
def apply_settings(self, btn, widgets, session): s = lt.session_settings() for k, w in widgets.iteritems(): if k == "disk_cache_algorithm": v = lt.disk_cache_algo_t(w.value) elif isinstance(w, Spinner): v = int(w.value) elif isinstance(w, Slider): v = w.value elif isinstance(w, Entry): v = w.entry.encode("utf-8") elif isinstance(w, Check): v = bool(w.state) else: v = None setattr(s, k, v) session.set_settings(s) Notify.Information(self, "Session settings saved.")
def __init__(self, trsession, utility): if LibtorrentMgr.__single: raise RuntimeError, "LibtorrentMgr is singleton" LibtorrentMgr.__single = self self.utility = utility self.trsession = trsession settings = lt.session_settings() settings.user_agent = 'Tribler/' + version_id fingerprint = ['TL'] + map(int, version_id.split('.')) + [0] # Workaround for libtorrent 0.16.3 segfault (see https://code.google.com/p/libtorrent/issues/detail?id=369) self.ltsession = lt.session(lt.fingerprint(*fingerprint), flags = 1) self.ltsession.set_settings(settings) self.ltsession.set_alert_mask(lt.alert.category_t.stats_notification | lt.alert.category_t.error_notification | lt.alert.category_t.status_notification | lt.alert.category_t.storage_notification | lt.alert.category_t.performance_warning ) self.ltsession.listen_on(self.trsession.get_listen_port(), self.trsession.get_listen_port()+10) self.set_upload_rate_limit(-1) self.set_download_rate_limit(-1) self.ltsession.start_upnp() # Start DHT try: dht_state = open(os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME)).read() self.ltsession.start_dht(lt.bdecode(dht_state)) except: print >> sys.stderr, "LibtorrentMgr: could not restore dht state, starting from scratch" self.ltsession.start_dht(None) self.ltsession.add_dht_router('router.bittorrent.com', 6881) self.ltsession.add_dht_router('router.utorrent.com', 6881) self.ltsession.add_dht_router('router.bitcomet.com', 6881) self.torlock = NoDispersyRLock() self.torrents = {} self.trsession.lm.rawserver.add_task(self.process_alerts, 1) self.trsession.lm.rawserver.add_task(self.reachability_check, 1)
def session_handles(): args = format_arguments() settings = lt.session_settings() settings.user_agent = 'python_client/' + lt.version session = lt.session() session.set_settings(settings) session.set_download_rate_limit(int(args.max_download_rate)) session.set_upload_rate_limit(int(args.max_upload_rate)) session.listen_on(args.port, args.port + 10) session.set_alert_mask(0xfffffff) if args.proxy_host != '': session.set_proxy(proxy_setup(args.proxy_host)) handles = [] for torrent in args.torrents: handle = session.add_torrent(session_torrents(torrent, args.save_path)) handle.set_max_connections(60) handle.set_max_uploads(-1) handles.append(handle) return session, handles
def __init__(self, trsession, utility): if LibtorrentMgr.__single: raise RuntimeError, "LibtorrentMgr is singleton" LibtorrentMgr.__single = self self.utility = utility self.trsession = trsession settings = lt.session_settings() settings.user_agent = 'Tribler/' + version_id self.ltsession = lt.session() self.ltsession.set_settings(settings) self.ltsession.add_extension(lt.create_ut_metadata_plugin) self.ltsession.add_extension(lt.create_ut_pex_plugin) self.ltsession.add_extension(lt.create_smart_ban_plugin) self.ltsession.set_alert_mask(lt.alert.category_t.stats_notification | lt.alert.category_t.error_notification | lt.alert.category_t.status_notification | lt.alert.category_t.performance_warning ) self.ltsession.listen_on(self.trsession.get_listen_port(), self.trsession.get_listen_port()+10) self.set_upload_rate_limit(-1) self.set_download_rate_limit(-1) self.ltsession.start_upnp() # Start DHT try: dht_state = open(os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME)).read() self.ltsession.start_dht(lt.bdecode(dht_state)) except: print >> sys.stderr, "LibtorrentMgr: could not restore dht state, starting from scratch" self.ltsession.start_dht(None) self.ltsession.add_dht_router('router.bittorrent.com', 6881) self.ltsession.add_dht_router('router.utorrent.com', 6881) self.ltsession.add_dht_router('router.bitcomet.com', 6881) self.torlock = NoDispersyRLock() self.torrents = {} self.trsession.lm.rawserver.add_task(self.process_alerts, 1)
def __init__(self, path_to_store, args=None, state_file="~/.btclient_state", **kwargs): super(BTClient,self).__init__(path_to_store, args=args) self._torrent_params={'save_path':path_to_store, 'storage_mode':lt.storage_mode_t.storage_mode_sparse } self._state_file=os.path.expanduser(state_file) self._ses=lt.session() if os.path.exists(self._state_file): with open(self._state_file) as f: state=pickle.load(f) self._ses.load_state(state) #self._ses.set_alert_mask(lt.alert.category_t.progress_notification) self._choose_file_flag = False if args: s=lt.session_settings() s.download_rate_limit=int(round(args.bt_download_limit*1024)) s.upload_rate_limit=int(round(args.bt_upload_limit*1024)) self._ses.set_settings(s) self._ses.listen_on(args.listen_port_min, args.listen_port_max) self._choose_file_flag = args.choose_file else: self._ses.listen_on(6881, 6891) self._start_services() self._th=None self._monitor.add_listener(self._check_ready) self._dispatcher=BTClient.Dispatcher(self) self._dispatcher.add_listener(self._update_ready_pieces) self._hash = None self._url=None if args and args.debug_log and args.trace: self.add_monitor_listener(self.debug_download_queue) self.add_dispatcher_listener(self.debug_alerts)
def serve_torrent(magnet=None,hash_file=None): options = {} main_log((True,None)) #~ th = term_handler() options["save-path"]="/home/tv/" if magnet is not None: options["magnet"]=magnet elif hash_file is not None: options["hash-file"]=hash_file logger = logging.getLogger("root") def f(): logger.debug("shutting down without a resume") #~ th.set_handler("save_resume", f) main_default(options) if "resume" in options: resume = options["resume"] else: resume = None resume_data = main_resume(resume) torrent_session = libtorrent.session() sp = libtorrent.session_settings() sp.choking_algorithm=3 sp.request_timeout /= 5 # configuration sp.piece_timeout /= 5 # configuration sp.peer_timeout /= 10 # configuration sp.inactivity_timeout /= 10 # configuration torrent_session.set_settings(sp) torrent_session.listen_on(6881, 6891) torrent_descr = { "save_path": options["save-path"] } if "hash-file" in options: if hash_file.startswith("http"): import urllib2 response = urllib2.urlopen(hash_file) f = response.read() else: f=io.open(options["hash-file"], 'rb').read() e = libtorrent.bdecode(f) torrent_info = libtorrent.torrent_info(e) torrent_descr["ti"] = torrent_info torrent_handle= torrent_session.add_torrent(torrent_descr) if "magnet" in options: torrent_handle=libtorrent.add_magnet_uri(torrent_session, str(options["magnet"]), torrent_descr) #~ torrent_descr["info_hash"] = libtorrent.big_number(options["info-hash"]) #~ torrent_descr["trackers"] = options["info-hash-tracker"] if not (resume_data is None): torrent_descr["resume_data"] = resume_data logger.debug("the torrent description: "+str(torrent_descr)) torrent_handle.set_sequential_download(True) logger.debug("the torrent handle "+str(torrent_handle)+" is created man") if "magnet" in options: if torrent_handle.status().state==libtorrent.torrent_status.downloading_metadata: metadata_received = False torrent_session.set_alert_mask(libtorrent.alert.category_t.status_notification) while(not metadata_received): torrent_session.wait_for_alert(1000) a = torrent_session.pop_alert() logger.debug("the alert "+str(a)+" is received") logger.debug("the torrent handle state is "+str(torrent_handle.status().state)) metadata_received = type(a) == libtorrent.metadata_received_alert logger.debug("the torrent metadata are received from your network") torrent_info = torrent_handle.get_torrent_info() #~ size=0 #~ j=0 #~ for i in torrent_info.files(): #~ if i.size>=size: #~ size=i.size #~ video=i.path #~ file_id=j #~ j=j+1 #~ f=info.files()[file_id] #~ video=f.path #~ torrent_handle.set_ratio(0.5) # configuration. upload_speed/download_speed piece_par_ref0 = reference() piece_par_ref0.data = options["piece-par"] piece_server0 = piece_server() piece_server0.torrent_handle = torrent_handle piece_server0.torrent_info = torrent_info piece_server0.piece_par = piece_par_ref0 piece_server0.init() alert_client0 = alert_client() alert_client0.torrent_session = torrent_session alert_client0.piece_server = piece_server0 if "resume" in options: ra = resume_alert() ra.torrent_handle = torrent_handle rs = resume_save() rs.file_name = options["resume"] rs.resume_alert = ra rt = resume_timer() rt.resume_save = rs rt.start() alert_client0.resume_alert = ra else: alert_client0.resume_alert = None alert_client0.start() r = torrent_read_bt2p() r.torrent_handle = torrent_handle r.torrent_info = torrent_info r.init() http_server = http_server_bt2p((options["domain-name"], options["port"]), http_responder_bt2p) http_server.daemon_threads = True http_server.torrent = r http_server.piece_par = piece_par_ref0 http_server.piece_server = piece_server0 if "resume" in options: def f(): logger.debug("shutting down with a resume") torrent_session.pause() rs.save(True) #~ th.set_handler("save_resume", f) try: http_server.serve_forever() except KeyboardInterrupt: print "key interrupt"
help='Upload limit in KiB/sec.') parser.add_argument('torrent', metavar='TORRENT', help='Torrent files') options = parser.parse_args() return options if __name__ == '__main__': options = _parse_opts() if not os.path.exists(options.torrent): sys.stderr.write("File '%s' does not exists!\n" % options.torrent) sys.exit(1) # Initialize Session session = libtorrent.session() settings = libtorrent.session_settings() settings.min_announce_interval = 15 if options.download_limit > 0 or options.upload_limit > 0: if options.download_limit > 0: session.set_download_rate_limit(options.download_limit << 10) if options.upload_limit > 0: session.set_upload_rate_limit(options.upload_limit << 10) settings.ignore_limits_on_local_network = False # Force Encryption if options.encryption: pe_settings = libtorrent.pe_settings() pe_settings.out_enc_policy = libtorrent.enc_policy.forced pe_settings.in_enc_policy = libtorrent.enc_policy.forced
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option('-p', '--port', type='int', help='set listening port') parser.add_option( '-d', '--max-download-rate', type='float', help='the maximum download rate given in kB/s. 0 means infinite.') parser.add_option( '-u', '--max-upload-rate', type='float', help='the maximum upload rate given in kB/s. 0 means infinite.') parser.add_option( '-s', '--save-path', type='string', help='the path where the downloaded file/folder should be placed.') parser.add_option( '-r', '--proxy-host', type='string', help='sets HTTP proxy host and port (separated by \':\')') parser.set_defaults(port=6881, max_download_rate=0, max_upload_rate=0, save_path='.', proxy_host='') (options, args) = parser.parse_args() if options.port < 0 or options.port > 65525: options.port = 6881 options.max_upload_rate *= 1000 options.max_download_rate *= 1000 if options.max_upload_rate <= 0: options.max_upload_rate = -1 if options.max_download_rate <= 0: options.max_download_rate = -1 settings = lt.session_settings() settings.user_agent = 'python_client/' + lt.version ses = lt.session() ses.set_download_rate_limit(int(options.max_download_rate)) ses.set_upload_rate_limit(int(options.max_upload_rate)) ses.listen_on(options.port, options.port + 10) ses.set_settings(settings) ses.set_alert_mask(0xfffffff) if options.proxy_host != '': ps = lt.proxy_settings() ps.type = lt.proxy_type.http ps.hostname = options.proxy_host.split(':')[0] ps.port = int(options.proxy_host.split(':')[1]) ses.set_proxy(ps) handles = [] alerts = [] for f in args: atp = {} atp["save_path"] = options.save_path atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse atp["paused"] = False atp["auto_managed"] = True atp["duplicate_is_error"] = True if f.startswith('magnet:') or f.startswith('http://') or f.startswith( 'https://'): atp["url"] = f else: info = lt.torrent_info(f) print('Adding \'%s\'...' % info.name()) try: atp["resume_data"] = open( os.path.join(options.save_path, info.name() + '.fastresume'), 'rb').read() except: pass atp["ti"] = info h = ses.add_torrent(atp) handles.append(h) h.set_max_connections(60) h.set_max_uploads(-1) if os.name == 'nt': console = WindowsConsole() else: console = UnixConsole() alive = True while alive: console.clear() out = '' for h in handles: if h.has_metadata(): name = h.get_torrent_info().name()[:40] else: name = '-' out += 'name: %-40s\n' % name s = h.status() if s.state != lt.torrent_status.seeding: state_str = ['queued', 'checking', 'downloading metadata', \ 'downloading', 'finished', 'seeding', \ 'allocating', 'checking fastresume'] out += state_str[s.state] + ' ' out += '%5.4f%% ' % (s.progress * 100) out += progress_bar(s.progress, 49) out += '\n' out += 'total downloaded: %d Bytes\n' % s.total_done out += 'peers: %d seeds: %d distributed copies: %d\n' % \ (s.num_peers, s.num_seeds, s.distributed_copies) out += '\n' out += 'download: %s/s (%s) ' \ % (add_suffix(s.download_rate), add_suffix(s.total_download)) out += 'upload: %s/s (%s) ' \ % (add_suffix(s.upload_rate), add_suffix(s.total_upload)) if s.state != lt.torrent_status.seeding: out += 'info-hash: %s\n' % h.info_hash() out += 'next announce: %s\n' % s.next_announce out += 'tracker: %s\n' % s.current_tracker write_line(console, out) print_peer_info(console, h.get_peer_info()) print_download_queue(console, h.get_download_queue()) if s.state != lt.torrent_status.seeding: try: out = '\n' fp = h.file_progress() ti = h.get_torrent_info() for f, p in zip(ti.files(), fp): out += progress_bar(p / float(f.size), 20) out += ' ' + f.path + '\n' write_line(console, out) except: pass write_line(console, 76 * '-' + '\n') write_line(console, '(q)uit), (p)ause), (u)npause), (r)eannounce\n') write_line(console, 76 * '-' + '\n') while 1: a = ses.pop_alert() if not a: break alerts.append(a) if len(alerts) > 8: del alerts[:len(alerts) - 8] for a in alerts: if type(a) == str: write_line(console, a + '\n') else: write_line(console, a.message() + '\n') c = console.sleep_and_input(0.5) if not c: continue if c == 'r': for h in handles: h.force_reannounce() elif c == 'q': alive = False elif c == 'p': for h in handles: h.pause() elif c == 'u': for h in handles: h.resume() ses.pause() for h in handles: if not h.is_valid() or not h.has_metadata(): continue data = lt.bencode(h.write_resume_data()) open( os.path.join(options.save_path, h.get_torrent_info().name() + '.fastresume'), 'wb').write(data)
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option('-p', '--port', type='int', help='set listening port') parser.add_option('-d', '--max-download-rate', type='float', help='the maximum download rate given in kB/s. 0 means infinite.') parser.add_option('-u', '--max-upload-rate', type='float', help='the maximum upload rate given in kB/s. 0 means infinite.') parser.add_option('-s', '--save-path', type='string', help='the path where the downloaded file/folder should be placed.') parser.add_option('-a', '--allocation-mode', type='string', help='sets mode used for allocating the downloaded files on disk. Possible options are [full | compact]') parser.add_option('-r', '--proxy-host', type='string', help='sets HTTP proxy host and port (separated by \':\')') parser.set_defaults( port=6881 , max_download_rate=0 , max_upload_rate=0 , save_path='./' , allocation_mode='compact' , proxy_host='' ) (options, args) = parser.parse_args() if options.port < 0 or options.port > 65525: options.port = 6881 options.max_upload_rate *= 1000 options.max_download_rate *= 1000 if options.max_upload_rate <= 0: options.max_upload_rate = -1 if options.max_download_rate <= 0: options.max_download_rate = -1 compact_allocation = options.allocation_mode == 'compact' settings = lt.session_settings() settings.user_agent = 'python_client/' + lt.version ses = lt.session() ses.set_download_rate_limit(int(options.max_download_rate)) ses.set_upload_rate_limit(int(options.max_upload_rate)) ses.listen_on(options.port, options.port + 10) ses.set_settings(settings) ses.set_alert_mask(0xfffffff) if options.proxy_host != '': ps = lt.proxy_settings() ps.type = lt.proxy_type.http ps.hostname = options.proxy_host.split(':')[0] ps.port = int(options.proxy_host.split(':')[1]) ses.set_proxy(ps) handles = [] alerts = [] for f in args: atp = {} atp["save_path"] = options.save_path atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse atp["paused"] = False atp["auto_managed"] = True atp["duplicate_is_error"] = True if f.startswith('magnet:') or f.startswith('http://') or f.startswith('https://'): atp["url"] = f else: e = lt.bdecode(open(f, 'rb').read()) info = lt.torrent_info(e) print('Adding \'%s\'...' % info.name()) try: atp["resume_data"] = open(os.path.join(options.save_path, info.name() + '.fastresume'), 'rb').read() except: pass atp["ti"] = info h = ses.add_torrent(atp) handles.append(h) h.set_max_connections(60) h.set_max_uploads(-1) if os.name == 'nt': console = WindowsConsole() else: console = UnixConsole() alive = True while alive: console.clear() out = '' for h in handles: if h.has_metadata(): name = h.get_torrent_info().name()[:40] else: name = '-' out += 'name: %-40s\n' % name s = h.status() if s.state != lt.torrent_status.seeding: state_str = ['queued', 'checking', 'downloading metadata', \ 'downloading', 'finished', 'seeding', \ 'allocating', 'checking fastresume'] out += state_str[s.state] + ' ' out += '%5.4f%% ' % (s.progress*100) out += progress_bar(s.progress, 49) out += '\n' out += 'total downloaded: %d Bytes\n' % s.total_done out += 'peers: %d seeds: %d distributed copies: %d\n' % \ (s.num_peers, s.num_seeds, s.distributed_copies) out += '\n' out += 'download: %s/s (%s) ' \ % (add_suffix(s.download_rate), add_suffix(s.total_download)) out += 'upload: %s/s (%s) ' \ % (add_suffix(s.upload_rate), add_suffix(s.total_upload)) if s.state != lt.torrent_status.seeding: out += 'info-hash: %s\n' % h.info_hash() out += 'next announce: %s\n' % s.next_announce out += 'tracker: %s\n' % s.current_tracker write_line(console, out) print_peer_info(console, h.get_peer_info()) print_download_queue(console, h.get_download_queue()) if s.state != lt.torrent_status.seeding: try: out = '\n' fp = h.file_progress() fp = 0 ti = h.get_torrent_info() for f,p in zip(ti.files(), fp): out += progress_bar(p / f.size, 20) out += ' ' + f.path + '\n' write_line(console, out) except: pass write_line(console, 76 * '-' + '\n') write_line(console, '(q)uit), (p)ause), (u)npause), (r)eannounce\n') write_line(console, 76 * '-' + '\n') while 1: a = ses.pop_alert() if not a: break alerts.append(a) if len(alerts) > 8: del alerts[:len(alerts) - 8] for a in alerts: if type(a) == str: write_line(console, a + '\n') else: write_line(console, a.message() + '\n') c = console.sleep_and_input(0.5) if not c: continue if c == 'r': for h in handles: h.force_reannounce() elif c == 'q': alive = False elif c == 'p': for h in handles: h.pause() elif c == 'u': for h in handles: h.resume() ses.pause() for h in handles: if not h.is_valid() or not h.has_metadata(): continue data = lt.bencode(h.write_resume_data()) open(os.path.join(options.save_path, h.get_torrent_info().name() + '.fastresume'), 'wb').write(data)
def fsinit(self): try: if not hasattr(self, "torrent_handle"): if "resume" in self.options: resume = self.options["resume"] else: resume = None resume_data = main_resume(resume) if "resume" in self.options: def f(): self.logger.debug("shutting down with a resume") torrent_session.pause() rs.save(True) self.torrent_session = libtorrent.session() self.torrent_session.add_dht_router("router.utorrent.com", 6881) sp = libtorrent.session_settings() sp.request_timeout /= 10 # configuration sp.piece_timeout /= 10 # configuration sp.peer_timeout /= 20 # configuration sp.inactivity_timeout /= 20 # configuration self.torrent_session.set_settings(sp) self.torrent_session.listen_on(self.options["port"], self.options["port"] + 100) torrent_descr = { "storage_mode": libtorrent.storage_mode_t.storage_mode_allocate, "save_path": self.options["save-path"] } if not (resume_data is None): torrent_descr["resume_data"] = resume_data if "hash-file" in self.options: e = libtorrent.bdecode( io.open(self.options["hash-file"], 'rb').read()) self.torrent_info = libtorrent.torrent_info(e) torrent_descr["ti"] = self.torrent_info if not (resume_data is None): torrent_descr["resume_data"] = resume_data self.logger.debug("the torrent description: " + str(torrent_descr)) self.torrent_handle = self.torrent_session.add_torrent( torrent_descr) self.logger.debug("the torrent handle " + str(self.torrent_handle) + " is created") if "magnet" in self.options: try: self.torrent_handle = libtorrent.add_magnet_uri( self.torrent_session, self.options["magnet"], torrent_descr) if not self.torrent_handle.is_valid(): raise self.torrent_info = 0 self.logger.debug("the torrent handle " + str(self.torrent_handle) + " is created") self.logger.debug("the magnet %s added" % self.options["magnet"]) return except: self.logger.critical("error parsing magnet link.") raise self.torrent_info = self.torrent_handle.get_torrent_info() piece_par_ref0 = reference() piece_par_ref0.data = self.options["piece-par"] piece_server0 = piece_server() piece_server0.torrent_handle = self.torrent_handle piece_server0.torrent_info = self.torrent_info piece_server0.piece_par = piece_par_ref0 piece_server0.defprio = self.options["defprio"] piece_server0.init() alert_client0 = alert_client() alert_client0.torrent_session = self.torrent_session alert_client0.piece_server = piece_server0 if "resume" in self.options: ra = resume_alert() ra.torrent_handle = self.torrent_handle rs = resume_save() rs.file_name = self.options["resume"] rs.resume_alert = ra rt = resume_timer() rt.resume_save = rs rt.start() alert_client0.resume_alert = ra else: alert_client0.resume_alert = None alert_client0.start() r = torrent_read_bt2p() r.torrent_handle = self.torrent_handle r.torrent_info = self.torrent_info r.piece_server = piece_server0 r.init() self.torrent = r self.piece_server = piece_server0 self.parsebttree() self.torrent_handle.force_recheck() except SystemExit: raise except: logging.critical( str("".join(traceback.format_exception(*sys.exc_info()))))
def settings(self): return libtorrent.session_settings()
def download(torrent, path, upload_rate_limit=0, seedtime=100): try: session = lt.session() session.set_alert_queue_size_limit(1024 * 1024) sts = lt.session_settings() sts.ssl_listen = False sts.user_agent = "Thunder deploy system" sts.tracker_completion_timeout = 5 sts.tracker_receive_timeout = 5 sts.stop_tracker_timeout = 5 sts.active_downloads = -1 sts.active_seeds = -1 sts.active_limit = -1 sts.auto_scrape_min_interval = 5 sts.udp_tracker_token_expiry = 120 sts.min_announce_interval = 1 sts.inactivity_timeout = 60 sts.connection_speed = 10 sts.allow_multiple_connections_per_ip = True sts.max_out_request_queue = 128 sts.request_queue_size = 3 sts.use_read_cache = False session.set_settings(sts) session.set_alert_mask(lt.alert.category_t.tracker_notification | lt.alert.category_t.status_notification) session.set_alert_mask(lt.alert.category_t.status_notification) ipaddr = get_wan_ip_address() #print ipaddr if ipaddr == "": session.listen_on(6881, 6881) else: session.listen_on(6881, 6881, ipaddr) limit = int(upload_rate_limit) if limit >= 100: session.set_upload_rate_limit(limit * 1024) session.set_local_upload_rate_limit(limit * 1024) print session.upload_rate_limit() torrent_info = lt.torrent_info(torrent) add_params = { 'save_path': path, 'storage_mode': lt.storage_mode_t.storage_mode_sparse, 'paused': False, 'auto_managed': True, 'ti': torrent_info, } handle = session.add_torrent(add_params) read_alerts(session) st = time.time() dl_status(handle) et = time.time() - st print '\nall file download in %.2f\nstart to seeding\n' % et sys.stdout.write('\n') handle.super_seeding() seed_status(handle, seedtime) remove_torrents(handle, session) assert len(session.get_torrents()) == 0 finally: print 'download finished'
def get_torrent_info(v1,v2,v3,session,u): global handle,ses,info,cnt,cnt_limit,file_name,ui,torrent_download_path ui = u if not session: sett = lt.session_settings() sett.user_agent = 'qBittorrent v3.3.5' sett.always_send_user_agent = True fingerprint = lt.fingerprint('qB',3,3,5,0) ses = lt.session(fingerprint) ses.listen_on(40000, 50000) ses.set_settings(sett) else: ses = session #print(ses.get_settings()) if v1.startswith('magnet:'): handle = lt.add_magnet_uri(ses,v1,{'save_path':v3}) i = 0 while (not handle.has_metadata()): time.sleep(1) i = i+1 if i > 60: print('No Metadata Available') break info = handle.get_torrent_info() else: info = lt.torrent_info(v1) #print(info) #sett = ses.get_settings() #print(sett) #print(sett['user_agent'],sett['upload_rate_limit']) handle = ses.add_torrent({'ti': info, 'save_path': v3}) handle.set_sequential_download(True) print(handle.trackers()) i=0 fileIndex = int(v2) for f in info.files(): if fileIndex == i: fileStr = f handle.file_priority(i,7) else: handle.file_priority(i,0) i += 1 print (fileStr.path) file_name = v3+'/'+fileStr.path torrent_download_path = v3 file_arr =[] for f in info.files(): file_arr.append(f.path) i += 1 for i in file_arr: print(i) content_length = fileStr.size print(content_length,'content-length') pr = info.map_file(fileIndex,0,fileStr.size) print(pr.length,info.piece_length(),info.num_pieces()) n_pieces = pr.length / info.piece_length() + 1 print(n_pieces) n_pieces = int(n_pieces) for i in range(info.num_pieces()): if i in range(pr.piece,pr.piece+n_pieces): if i == pr.piece: handle.piece_priority(i,7) elif i == pr.piece+1: handle.piece_priority(i,7) elif i == pr.piece+2: handle.piece_priority(i,1) elif i == pr.piece+n_pieces-1: handle.piece_priority(i,7) else: handle.piece_priority(i,1) else: handle.piece_priority(i,0) print ('starting', handle.name()) handle.set_sequential_download(True) cnt = pr.piece cnt_limit = pr.piece+n_pieces cnt1 = cnt print('\n',cnt,cnt_limit,file_name,'---get--torrent--info\n') if ses.is_paused(): ses.resume() handle.resume() return handle,ses,info,cnt,cnt_limit,file_name
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option('-p', '--port', type='int', help='set listening port') parser.add_option('-r', '--ratio', type='float', help='set the preferred upload/download ratio. 0 means infinite. Values smaller than 1 are clamped to 1') parser.add_option('-d', '--max-download-rate', type='float', help='the maximum download rate given in kB/s. 0 means infinite.') parser.add_option('-u', '--max-upload-rate', type='float', help='the maximum upload rate given in kB/s. 0 means infinite.') parser.add_option('-s', '--save-path', type='string', help='the path where the downloaded file/folder should be placed.') parser.add_option('-a', '--allocation-mode', type='string', help='sets mode used for allocating the downloaded files on disk. Possible options are [full | compact]') parser.set_defaults( port=6881 , ratio=0 , max_download_rate=0 , max_upload_rate=0 , save_path='./' , allocation_mode='compact' ) (options, args) = parser.parse_args() if options.port < 0 or options.port > 65525: options.port = 6881 options.max_upload_rate *= 1000 options.max_download_rate *= 1000 if options.max_upload_rate <= 0: options.max_upload_rate = -1 if options.max_download_rate <= 0: options.max_download_rate = -1 compact_allocation = options.allocation_mode == 'compact' settings = lt.session_settings() settings.user_agent = 'python_client/' + lt.version ses = lt.session() ses.set_download_rate_limit(int(options.max_download_rate)) ses.set_upload_rate_limit(int(options.max_upload_rate)) ses.listen_on(options.port, options.port + 10) ses.set_settings(settings) ses.set_severity_level(lt.alert.severity_levels.info) handles = [] alerts = [] # Extensions # ses.add_extension(lambda x: PythonExtension(alerts)) for f in args: e = lt.bdecode(open(f, 'rb').read()) info = lt.torrent_info(e) print 'Adding \'%s\'...' % info.name() try: resume_data = lt.bdecode(open( os.path.join(options.save_path, info.name() + '.fastresume'), 'rb').read()) except: resume_data = None h = ses.add_torrent(info, options.save_path, resume_data=resume_data, compact_mode=compact_allocation) handles.append(h) h.set_max_connections(60) h.set_max_uploads(-1) h.set_ratio(options.ratio) h.set_sequenced_download_threshold(15) if os.name == 'nt': console = WindowsConsole() else: console = UnixConsole() alive = True while alive: console.clear() out = '' for h in handles: if h.has_metadata(): name = h.torrent_info().name()[:40] else: name = '-' out += 'name: %-40s\n' % name s = h.status() if s.state != lt.torrent_status.seeding: state_str = ['queued', 'checking', 'connecting', 'downloading metadata', \ 'downloading', 'finished', 'seeding', 'allocating'] out += state_str[s.state] + ' ' out += '%5.4f%% ' % (s.progress*100) out += progress_bar(s.progress, 49) out += '\n' out += 'total downloaded: %d Bytes\n' % s.total_done out += 'peers: %d seeds: %d distributed copies: %d\n' % \ (s.num_peers, s.num_seeds, s.distributed_copies) out += '\n' out += 'download: %s/s (%s) ' \ % (add_suffix(s.download_rate), add_suffix(s.total_download)) out += 'upload: %s/s (%s) ' \ % (add_suffix(s.upload_rate), add_suffix(s.total_upload)) out += 'ratio: %s\n' % '0' if s.state != lt.torrent_status.seeding: out += 'info-hash: %s\n' % h.info_hash() out += 'next announce: %s\n' % s.next_announce out += 'tracker: %s\n' % s.current_tracker write_line(console, out) peers = h.get_peer_info() print_peer_info(console, peers) if True and s.state != lt.torrent_status.seeding: out = '\n' fp = h.file_progress() ti = h.torrent_info() for f,p in zip(ti.files(), fp): out += progress_bar(p, 20) out += ' ' + f.path + '\n' write_line(console, out) write_line(console, 76 * '-' + '\n') write_line(console, '(q)uit), (p)ause), (u)npause), (r)eannounce\n') write_line(console, 76 * '-' + '\n') while 1: a = ses.pop_alert() if not a: break alerts.append(a) if len(alerts) > 8: del alerts[:len(alerts) - 8] for a in alerts: if type(a) == str: write_line(console, a + '\n') else: write_line(console, a.msg() + '\n') c = console.sleep_and_input(0.5) if not c: continue if c == 'r': for h in handles: h.force_reannounce() elif c == 'q': alive = False elif c == 'p': for h in handles: h.pause() elif c == 'u': for h in handles: h.resume() for h in handles: if not h.is_valid() or not h.has_metadata(): continue h.pause() data = lt.bencode(h.write_resume_data()) open(os.path.join(options.save_path, h.torrent_info().name() + '.fastresume'), 'wb').write(data)
def share_torrent(torrent, save_dir, name): settings = lt.session_settings() settings.user_agent = 'python_client/' + lt.version settings.ignore_limits_on_local_network = False settings.disable_hash_checks = True ses.set_settings(settings) ses.set_alert_mask(0xfffffff) # ses.set_alert_mask(lt.alert.category_t.status_notification | lt.alert.category_t.storage_notification) atp = {} atp["save_path"] = save_dir atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse # atp["storage_mode"] = lt.storage_mode_t.storage_mode_compact # atp["storage_mode"] = lt.storage_mode_t.storage_mode_allocate atp["paused"] = False atp["auto_managed"] = True atp["duplicate_is_error"] = True # atp["flags"] = lt.add_torrent_params_flags_t.flag_seed_mode atp["name"] = "helloworld_%d" % threading.currentThread().ident temp = "/opt/work/code/Python/bt/temp/" + name + '.fastresume' if os.path.exists(temp): print("read torrent", temp) atp["resume_data"] = open(temp, 'rb').read() info = None try: fd = open(torrent, 'rb') torrent_data = lt.bdecode(fd.read()) info = lt.torrent_info(torrent_data) fd.close() except Exception as ex: print("torrent info error: %s\nstatck msg:%s" % (torrent, ex)) basename = info.name() print("download add: %s..." % basename) atp["ti"] = info handle = ses.add_torrent(atp) handle.set_max_connections(60) handle.set_max_uploads(-1) handle.set_download_limit(-1) handle.set_upload_limit(200000) g_handle_queue.put(handle) alive = True while alive: alerts = [] while 1: a = ses.pop_alert() if not a: break alerts.append(a) # print("alert:", len(alerts)) for a in alerts: rtn = handle_alert(a) if rtn == 1: alive = False break ses.post_torrent_updates() if handle and handle.need_save_resume_data(): print("-- handle save resume data") handle.save_resume_data() time.sleep(0.2)
def get_torrent_info(v1, v2, v3, session, u, p_bar, tmp_dir, key=None, client=None): global handle, ses, info, cnt, cnt_limit, file_name, ui, torrent_download_path global progress, total_size_content, tmp_dir_folder, content_length global media_server_key, client_auth_arr media_server_key = key client_auth_arr = client content_length = 0 ui = u progress = p_bar tmp_dir_folder = tmp_dir progress.setValue(0) progress.show() if not session: sett = lt.session_settings() sett.user_agent = 'qBittorrent v3.3.5' sett.always_send_user_agent = True fingerprint = lt.fingerprint('qB', 3, 3, 5, 0) ses = lt.session(fingerprint) ses.listen_on(40000, 50000) ses.set_settings(sett) else: ses = session #print(ses.get_settings()) if v1.startswith('magnet:'): handle = lt.add_magnet_uri(ses, v1, {'save_path': v3}) i = 0 while (not handle.has_metadata()): time.sleep(1) i = i + 1 if i > 60: print('No Metadata Available') break info = handle.get_torrent_info() else: info = lt.torrent_info(v1) #print(info) #sett = ses.get_settings() #print(sett) #print(sett['user_agent'], sett['upload_rate_limit']) handle = ses.add_torrent({'ti': info, 'save_path': v3}) handle.set_sequential_download(True) print(handle.trackers()) print(ses.get_torrents(), 'torrents_list') torr_arr = ses.get_torrents() for i in torr_arr: print(i.name()) i = 0 fileIndex = int(v2) file_found = False for f in info.files(): file_exists = False new_path = os.path.join(v3, f.path) new_size = f.size if os.path.exists(new_path) and os.stat(new_path).st_size == new_size: file_exists = True if fileIndex == i: fileStr = f handle.file_priority(i, 7) if file_exists: file_found = True else: if file_exists: handle.file_priority(i, 1) else: handle.file_priority(i, 0) i = i + 1 print(fileStr.path) file_name = os.path.join(v3, fileStr.path) torrent_download_path = v3 file_arr = [] for f in info.files(): file_arr.append(f.path) i += 1 for i in file_arr: print(i) content_length = fileStr.size print(content_length, 'content-length') total_size_content = str(int(content_length / (1024 * 1024))) + 'M' pr = info.map_file(fileIndex, 0, fileStr.size) print(pr.length, info.piece_length(), info.num_pieces()) n_pieces = pr.length / info.piece_length() + 1 print(n_pieces) n_pieces = int(n_pieces) for i in range(info.num_pieces()): if i in range(pr.piece, pr.piece + n_pieces): if i in range(pr.piece, pr.piece + 10): if i == pr.piece: handle.piece_priority(i, 7) else: handle.piece_priority(i, 6) elif i == pr.piece + n_pieces - 1: handle.piece_priority(i, 7) else: handle.piece_priority(i, 1) tmp = '' for i in range(info.num_pieces()): tmp = tmp + ':' + str(handle.piece_priority(i)) print(tmp) print('starting', handle.name()) handle.set_sequential_download(True) cnt = pr.piece cnt_limit = pr.piece + n_pieces cnt1 = cnt print('\n', cnt, cnt_limit, file_name, '---get--torrent--info\n') if ses.is_paused(): ses.resume() handle.resume() return handle, ses, info, cnt, cnt_limit, file_name
def start(): # Queue of info hashs to get metadata for throught DHT/peers Metadata._queue = { "cache": [], "peers": [] } Metadata.max_handles = 50 Metadata.metadata_timeout = 300 Metadata._state_file = "libtorrent.state" Metadata._state = None if os.path.exists(Metadata._state_file): try: Metadata._state = pickle.load(open(Metadata._state_file, "rb")) except Exception as e: Metadata.logger.critical("Error loading state") Metadata._session = lt.session() if Metadata._state is not None and "session" in Metadata._state: Metadata._session.load_state(Metadata._state["session"]) Metadata.logger.info("Session state loaded") alert_mask = lt.alert.category_t.all_categories Metadata._session.set_alert_mask(alert_mask) Metadata._session.add_extension(lt.create_metadata_plugin) Metadata._session.add_extension(lt.create_ut_metadata_plugin) Metadata._session.add_extension(lt.create_ut_pex_plugin) Metadata._session.add_extension(lt.create_smart_ban_plugin) Metadata._session.listen_on(6881, 6891, None) # Add DHT routers to bootstrap DHT Metadata._session.add_dht_router("router.bittorrent.com", 6881) Metadata._session.add_dht_router("router.bitcomet.com", 6881) Metadata._session.add_dht_router("router.utorrent.com", 6881) Metadata._session.add_dht_router("dht.transmissionbt.com", 6881) Metadata._session.start_dht() Metadata._session.start_lsd() Metadata._session.start_upnp() Metadata._session.start_natpmp() Metadata._settings = lt.session_settings() Metadata._settings.num_want = 50 Metadata._settings.max_peerlist_size = 0 Metadata._settings.active_downloads = Metadata.max_handles + 10 Metadata._settings.active_limit = Metadata.max_handles + 10 Metadata._settings.dht_upload_rate_limit = 4000*15 Metadata._session.set_settings(Metadata._settings) dht_node_id = Metadata._session.dht_state()["node-id"] dht_node_id = binascii.hexlify(dht_node_id).lower() Metadata.logger.info("DHT Node ID: %s" % dht_node_id) Metadata.logger.info("Started") # Start alert loop t = Thread(target=Metadata._alert_loop) t.daemon = True t.start() # Start queue loop t = Thread(target=Metadata._cache_queue_loop) t.daemon = True t.start() # Start queue loop t = Thread(target=Metadata._peer_queue_loop) t.daemon = True t.start()
def configure(self, anonymous_mode, user_agent=None): settings = lt.session_settings() settings.anonymous_mode = anonymous_mode if user_agent: settings.user_agent = user_agent self.session.set_settings(settings)
def _download_torrent(self, torrentpath): logging.info('download to cache dir: ' + self.cache_dir) if self.torrenthandle is not None: raise TorrentInProgressException( 'Another torrent is already in progress') self.torrent_session = lt.session() self.torrent_session.listen_on(6881, 6891) self.torrent_session.stop_dht() settings = lt.session_settings() settings.max_pex_peers = 0 self.torrent_session.set_settings(settings) logging.info('started torrent session') e = lt.bdecode(open(torrentpath, 'rb').read()) torrentinfo = lt.torrent_info(e) self.torrenthandle = self.torrent_session.add_torrent({ 'ti': torrentinfo, 'save_path': self.cache_dir, 'storage_mode': lt.storage_mode_t.storage_mode_sparse }) # Split the title into term s track_terms = self._wanted_track_title_terms # Remove all terms that are non-alphanumeric, such as '&' for t in track_terms: if re.match(r'^\W?$', t, re.UNICODE): track_terms.remove(t) logging.info('created track search terms: ' + repr(track_terms)) # Count amount of terms found, the track with most common terms is the one we, hopefully, want terms_found = 0 track_found = None track_index = None # Find the file we want filelist = torrentinfo.files() filtered_filelist = [ ] # List of files, without common terms (sometimes the artist name is in the filename) for n in range(0, len(filelist)): # Filter the names torrent_content = filelist[n].path.split('\\') track_name = torrent_content[-1].lower( ) # take last part of the path, remove folders split_extension = track_name.split('.') extension = split_extension[-1].lower() track_name = track_name.replace( '.' + extension, '') # remove extension from trackname track_name = re.sub( r'([^\s\w]|_)+', '', track_name ) # remove non-alphanumeric (except space), since this also happens to the search string filtered_filelist.append({ 'track': track_name, 'extension': extension }) common_substring = longest_common_substring( filtered_filelist[0]['track'], filtered_filelist[1]['track']) # Find common string in the first 2 for n in range(0, len(filtered_filelist)): if filtered_filelist[n]['extension'] == 'mp3': new_substring = longest_common_substring( common_substring, filtered_filelist[n]['track']) # print new_substring if len(new_substring) < len(common_substring): common_substring = new_substring logging.info('removing common substring in torrent filenames: ' + common_substring) # Now check if the common substring is also in the rest, if so, remove the substring from all filenames common_substring_in_all_filenames = True for n in range(0, len(filtered_filelist)): if filtered_filelist[n][ 'extension'] == 'mp3' and common_substring not in filtered_filelist[ n]['track']: common_substring_in_all_filenames = False break # No need to continue if common_substring_in_all_filenames: for n in range(0, len(filtered_filelist)): filtered_filelist[n]['track'] = filtered_filelist[n][ 'track'].replace(common_substring, ' ') if self._DEBUG: print filtered_filelist for n in range(0, len(filtered_filelist)): track_name = filtered_filelist[n]['track'] extension = filtered_filelist[n]['extension'] if extension == 'mp3': # Only allows mp3 files if not self._allow_radio_edit: if 'radio' in track_name: # Ignore radio edits continue unwanted_term = False for t in track_terms: # Check if special terms (such as 'remix') are in the search, if not, the special terms are unwanted if 'mix' in t.lower( ): # Checking for mix because this would catch 'remix' too unwanted_term = True if not unwanted_term: if 'mix' in track_name: continue terms_this_track = 0 for t in track_terms: if t.lower() in track_name: terms_this_track += 1 if terms_this_track > terms_found: terms_found = terms_this_track track_found = track_name track_index = n if track_index is None: raise IncorrectTorrentException('Track not found in torrent') filesize_mb = float(filelist[track_index].size) / 1024 / 1024 filesize_string = '%.1f' % filesize_mb + 'MB' if self._wanted_encoding == encoding.C320: estimated_duration = ( (filelist[track_index].size / 1024) * 8) / 320 minutes, seconds = divmod(estimated_duration, 60) filesize_string += ' (~%s:%02d)' % (minutes, seconds) self.emit(self.signal_filesize, filesize_string) logging.info('track in torrent file is size (MB): ' + filesize_string) logging.info('closest match in album: ' + track_found + ' at torrent index: ' + str(track_index)) self._wanted_track_filename = filelist[track_index].path self.emit(self.signal_filename, self._wanted_track_filename.split('\\') [-1]) # track name without path self._check_track_exists() # Set wanted file to normal priority num_files_skipped = [0] * torrentinfo.num_files() num_files_skipped[track_index] = 1 self.torrenthandle.prioritize_files(num_files_skipped) # start thread that displays torrent info self.thread_progress = TorrentProgressThread(self.torrenthandle) self.parent.connect_progress_bar() self.connect(self.thread_progress, self.thread_progress.signal_complete, self._torrent_complete) self.thread_progress.start()
def get_torrent_info(v1,v2,v3,session,u,p_bar,tmp_dir,key=None,client=None): global handle,ses,info,cnt,cnt_limit,file_name,ui,torrent_download_path global progress,total_size_content,tmp_dir_folder,content_length global media_server_key,client_auth_arr media_server_key = key client_auth_arr = client content_length = 0 ui = u progress = p_bar tmp_dir_folder = tmp_dir progress.setValue(0) progress.show() if not session: sett = lt.session_settings() sett.user_agent = 'qBittorrent v3.3.5' sett.always_send_user_agent = True fingerprint = lt.fingerprint('qB',3,3,5,0) ses = lt.session(fingerprint) ses.listen_on(40000, 50000) ses.set_settings(sett) else: ses = session #print(ses.get_settings()) if v1.startswith('magnet:'): handle = lt.add_magnet_uri(ses,v1,{'save_path':v3}) i = 0 while (not handle.has_metadata()): time.sleep(1) i = i+1 if i > 60: print('No Metadata Available') break info = handle.get_torrent_info() else: info = lt.torrent_info(v1) #print(info) #sett = ses.get_settings() #print(sett) #print(sett['user_agent'],sett['upload_rate_limit']) handle = ses.add_torrent({'ti': info, 'save_path': v3}) handle.set_sequential_download(True) print(handle.trackers()) print(ses.get_torrents(),'torrents_list') torr_arr = ses.get_torrents() for i in torr_arr: print(i.name()) i=0 fileIndex = int(v2) file_found = False for f in info.files(): file_exists = False new_path = os.path.join(v3,f.path) new_size = f.size if os.path.exists(new_path) and os.stat(new_path).st_size == new_size: file_exists = True if fileIndex == i: fileStr = f handle.file_priority(i,7) if file_exists: file_found = True else: if file_exists: handle.file_priority(i,1) else: handle.file_priority(i,0) i = i+1 print (fileStr.path) file_name = os.path.join(v3,fileStr.path) torrent_download_path = v3 file_arr =[] for f in info.files(): file_arr.append(f.path) i += 1 for i in file_arr: print(i) content_length = fileStr.size print(content_length,'content-length') total_size_content = str(int(content_length/(1024*1024)))+'M' pr = info.map_file(fileIndex,0,fileStr.size) print(pr.length,info.piece_length(),info.num_pieces()) n_pieces = pr.length / info.piece_length() + 1 print(n_pieces) n_pieces = int(n_pieces) for i in range(info.num_pieces()): if i in range(pr.piece,pr.piece+n_pieces): if i in range(pr.piece,pr.piece+10): if i == pr.piece: handle.piece_priority(i,7) else: handle.piece_priority(i,6) elif i == pr.piece+n_pieces-1: handle.piece_priority(i,7) else: handle.piece_priority(i,1) tmp = '' for i in range(info.num_pieces()): tmp = tmp+':'+str(handle.piece_priority(i)) print(tmp) print ('starting', handle.name()) handle.set_sequential_download(True) cnt = pr.piece cnt_limit = pr.piece+n_pieces cnt1 = cnt print('\n',cnt,cnt_limit,file_name,'---get--torrent--info\n') if ses.is_paused(): ses.resume() handle.resume() return handle,ses,info,cnt,cnt_limit,file_name
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option('-p', '--port', type='int', help='set listening port') parser.add_option( '-r', '--ratio', type='float', help= 'set the preferred upload/download ratio. 0 means infinite. Values smaller than 1 are clamped to 1' ) parser.add_option( '-d', '--max-download-rate', type='float', help='the maximum download rate given in kB/s. 0 means infinite.') parser.add_option( '-u', '--max-upload-rate', type='float', help='the maximum upload rate given in kB/s. 0 means infinite.') parser.add_option( '-s', '--save-path', type='string', help='the path where the downloaded file/folder should be placed.') parser.add_option( '-a', '--allocation-mode', type='string', help= 'sets mode used for allocating the downloaded files on disk. Possible options are [full | compact]' ) parser.set_defaults(port=6881, ratio=0, max_download_rate=0, max_upload_rate=0, save_path='./', allocation_mode='compact') (options, args) = parser.parse_args() if options.port < 0 or options.port > 65525: options.port = 6881 options.max_upload_rate *= 1000 options.max_download_rate *= 1000 if options.max_upload_rate <= 0: options.max_upload_rate = -1 if options.max_download_rate <= 0: options.max_download_rate = -1 compact_allocation = options.allocation_mode == 'compact' settings = lt.session_settings() settings.user_agent = 'python_client/' + lt.version ses = lt.session() ses.set_download_rate_limit(int(options.max_download_rate)) ses.set_upload_rate_limit(int(options.max_upload_rate)) ses.listen_on(options.port, options.port + 10) ses.set_settings(settings) ses.set_severity_level(lt.alert.severity_levels.info) ses.add_extension(lt.create_ut_pex_plugin) ses.add_extension(lt.create_ut_metadata_plugin) ses.add_extension(lt.create_metadata_plugin) handles = [] alerts = [] # Extensions # ses.add_extension(lambda x: PythonExtension(alerts)) for f in args: e = lt.bdecode(open(f, 'rb').read()) info = lt.torrent_info(e) print 'Adding \'%s\'...' % info.name() atp = {} try: atp["resume_data"] = open( os.path.join(options.save_path, info.name() + '.fastresume'), 'rb').read() except: pass atp["ti"] = info atp["save_path"] = options.save_path atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse atp["paused"] = False atp["auto_managed"] = True atp["duplicate_is_error"] = True h = ses.add_torrent(atp) handles.append(h) h.set_max_connections(60) h.set_max_uploads(-1) h.set_ratio(options.ratio) if os.name == 'nt': console = WindowsConsole() else: console = UnixConsole() alive = True while alive: console.clear() out = '' for h in handles: if h.has_metadata(): name = h.get_torrent_info().name()[:40] else: name = '-' out += 'name: %-40s\n' % name s = h.status() if s.state != lt.torrent_status.seeding: state_str = ['queued', 'checking', 'downloading metadata', \ 'downloading', 'finished', 'seeding', \ 'allocating', 'checking fastresume'] out += state_str[s.state] + ' ' out += '%5.4f%% ' % (s.progress * 100) out += progress_bar(s.progress, 49) out += '\n' out += 'total downloaded: %d Bytes\n' % s.total_done out += 'peers: %d seeds: %d distributed copies: %d\n' % \ (s.num_peers, s.num_seeds, s.distributed_copies) out += '\n' out += 'download: %s/s (%s) ' \ % (add_suffix(s.download_rate), add_suffix(s.total_download)) out += 'upload: %s/s (%s) ' \ % (add_suffix(s.upload_rate), add_suffix(s.total_upload)) out += 'ratio: %s\n' % '0' if s.state != lt.torrent_status.seeding: out += 'info-hash: %s\n' % h.info_hash() out += 'next announce: %s\n' % s.next_announce out += 'tracker: %s\n' % s.current_tracker write_line(console, out) print_peer_info(console, h.get_peer_info()) print_download_queue(console, h.get_download_queue()) if True and s.state != lt.torrent_status.seeding: out = '\n' fp = h.file_progress() ti = h.get_torrent_info() for f, p in zip(ti.files(), fp): out += progress_bar(p / f.size, 20) out += ' ' + f.path + '\n' write_line(console, out) write_line(console, 76 * '-' + '\n') write_line(console, '(q)uit), (p)ause), (u)npause), (r)eannounce\n') write_line(console, 76 * '-' + '\n') while 1: a = ses.pop_alert() if not a: break alerts.append(a) if len(alerts) > 8: del alerts[:len(alerts) - 8] for a in alerts: if type(a) == str: write_line(console, a + '\n') else: write_line(console, a.message() + '\n') c = console.sleep_and_input(0.5) if not c: continue if c == 'r': for h in handles: h.force_reannounce() elif c == 'q': alive = False elif c == 'p': for h in handles: h.pause() elif c == 'u': for h in handles: h.resume() ses.pause() for h in handles: if not h.is_valid() or not h.has_metadata(): continue data = lt.bencode(h.write_resume_data()) open( os.path.join(options.save_path, h.get_torrent_info().name() + '.fastresume'), 'wb').write(data)
def serve_torrent(magnet=None, hash_file=None): options = {} main_log((True, None)) #~ th = term_handler() options["save-path"] = "/home/tv/" if magnet is not None: options["magnet"] = magnet elif hash_file is not None: options["hash-file"] = hash_file logger = logging.getLogger("root") def f(): logger.debug("shutting down without a resume") #~ th.set_handler("save_resume", f) main_default(options) if "resume" in options: resume = options["resume"] else: resume = None resume_data = main_resume(resume) torrent_session = libtorrent.session() sp = libtorrent.session_settings() sp.choking_algorithm = 3 sp.request_timeout /= 5 # configuration sp.piece_timeout /= 5 # configuration sp.peer_timeout /= 10 # configuration sp.inactivity_timeout /= 10 # configuration torrent_session.set_settings(sp) torrent_session.listen_on(6881, 6891) torrent_descr = {"save_path": options["save-path"]} if "hash-file" in options: if hash_file.startswith("http"): import urllib2 response = urllib2.urlopen(hash_file) f = response.read() else: f = io.open(options["hash-file"], 'rb').read() e = libtorrent.bdecode(f) torrent_info = libtorrent.torrent_info(e) torrent_descr["ti"] = torrent_info torrent_handle = torrent_session.add_torrent(torrent_descr) if "magnet" in options: torrent_handle = libtorrent.add_magnet_uri(torrent_session, str(options["magnet"]), torrent_descr) #~ torrent_descr["info_hash"] = libtorrent.big_number(options["info-hash"]) #~ torrent_descr["trackers"] = options["info-hash-tracker"] if not (resume_data is None): torrent_descr["resume_data"] = resume_data logger.debug("the torrent description: " + str(torrent_descr)) torrent_handle.set_sequential_download(True) logger.debug("the torrent handle " + str(torrent_handle) + " is created man") if "magnet" in options: if torrent_handle.status( ).state == libtorrent.torrent_status.downloading_metadata: metadata_received = False torrent_session.set_alert_mask( libtorrent.alert.category_t.status_notification) while (not metadata_received): torrent_session.wait_for_alert(1000) a = torrent_session.pop_alert() logger.debug("the alert " + str(a) + " is received") logger.debug("the torrent handle state is " + str(torrent_handle.status().state)) metadata_received = type( a) == libtorrent.metadata_received_alert logger.debug("the torrent metadata are received from your network") torrent_info = torrent_handle.get_torrent_info() #~ size=0 #~ j=0 #~ for i in torrent_info.files(): #~ if i.size>=size: #~ size=i.size #~ video=i.path #~ file_id=j #~ j=j+1 #~ f=info.files()[file_id] #~ video=f.path #~ torrent_handle.set_ratio(0.5) # configuration. upload_speed/download_speed piece_par_ref0 = reference() piece_par_ref0.data = options["piece-par"] piece_server0 = piece_server() piece_server0.torrent_handle = torrent_handle piece_server0.torrent_info = torrent_info piece_server0.piece_par = piece_par_ref0 piece_server0.init() alert_client0 = alert_client() alert_client0.torrent_session = torrent_session alert_client0.piece_server = piece_server0 if "resume" in options: ra = resume_alert() ra.torrent_handle = torrent_handle rs = resume_save() rs.file_name = options["resume"] rs.resume_alert = ra rt = resume_timer() rt.resume_save = rs rt.start() alert_client0.resume_alert = ra else: alert_client0.resume_alert = None alert_client0.start() r = torrent_read_bt2p() r.torrent_handle = torrent_handle r.torrent_info = torrent_info r.init() http_server = http_server_bt2p((options["domain-name"], options["port"]), http_responder_bt2p) http_server.daemon_threads = True http_server.torrent = r http_server.piece_par = piece_par_ref0 http_server.piece_server = piece_server0 if "resume" in options: def f(): logger.debug("shutting down with a resume") torrent_session.pause() rs.save(True) #~ th.set_handler("save_resume", f) try: http_server.serve_forever() except KeyboardInterrupt: print "key interrupt"
def _download_torrent(self, torrentpath): logging.info('download to cache dir: ' + self.cache_dir) if self.torrenthandle is not None: raise TorrentInProgressException('Another torrent is already in progress') self.torrent_session = lt.session() self.torrent_session.listen_on(6881, 6891) self.torrent_session.stop_dht() settings = lt.session_settings() settings.max_pex_peers = 0 self.torrent_session.set_settings(settings) logging.info('started torrent session') e = lt.bdecode(open(torrentpath, 'rb').read()) torrentinfo = lt.torrent_info(e) self.torrenthandle = self.torrent_session.add_torrent({'ti': torrentinfo, 'save_path': self.cache_dir, 'storage_mode': lt.storage_mode_t.storage_mode_sparse}) # Split the title into term s track_terms = self._wanted_track_title_terms # Remove all terms that are non-alphanumeric, such as '&' for t in track_terms: if re.match(r'^\W?$', t, re.UNICODE): track_terms.remove(t) logging.info('created track search terms: ' + repr(track_terms)) # Count amount of terms found, the track with most common terms is the one we, hopefully, want terms_found = 0 track_found = None track_index = None # Find the file we want filelist = torrentinfo.files() filtered_filelist = [] # List of files, without common terms (sometimes the artist name is in the filename) for n in range(0, len(filelist)): # Filter the names torrent_content = filelist[n].path.split('\\') track_name = torrent_content[-1].lower() # take last part of the path, remove folders split_extension = track_name.split('.') extension = split_extension[-1].lower() track_name = track_name.replace('.' + extension, '') # remove extension from trackname track_name = re.sub(r'([^\s\w]|_)+', '', track_name) # remove non-alphanumeric (except space), since this also happens to the search string filtered_filelist.append({'track': track_name, 'extension': extension}) common_substring = longest_common_substring(filtered_filelist[0]['track'], filtered_filelist[1]['track']) # Find common string in the first 2 for n in range(0, len(filtered_filelist)): if filtered_filelist[n]['extension'] == 'mp3': new_substring = longest_common_substring(common_substring, filtered_filelist[n]['track']) # print new_substring if len(new_substring) < len(common_substring): common_substring = new_substring logging.info('removing common substring in torrent filenames: ' + common_substring) # Now check if the common substring is also in the rest, if so, remove the substring from all filenames common_substring_in_all_filenames = True for n in range(0, len(filtered_filelist)): if filtered_filelist[n]['extension'] == 'mp3' and common_substring not in filtered_filelist[n]['track']: common_substring_in_all_filenames = False break # No need to continue if common_substring_in_all_filenames: for n in range(0, len(filtered_filelist)): filtered_filelist[n]['track'] = filtered_filelist[n]['track'].replace(common_substring, ' ') if self._DEBUG: print filtered_filelist for n in range(0, len(filtered_filelist)): track_name = filtered_filelist[n]['track'] extension = filtered_filelist[n]['extension'] if extension == 'mp3': # Only allows mp3 files if not self._allow_radio_edit: if 'radio' in track_name: # Ignore radio edits continue unwanted_term = False for t in track_terms: # Check if special terms (such as 'remix') are in the search, if not, the special terms are unwanted if 'mix' in t.lower(): # Checking for mix because this would catch 'remix' too unwanted_term = True if not unwanted_term: if 'mix' in track_name: continue terms_this_track = 0 for t in track_terms: if t.lower() in track_name: terms_this_track += 1 if terms_this_track > terms_found: terms_found = terms_this_track track_found = track_name track_index = n if track_index is None: raise IncorrectTorrentException('Track not found in torrent') filesize_mb = float(filelist[track_index].size) / 1024 / 1024 filesize_string = '%.1f' % filesize_mb + 'MB' if self._wanted_encoding == encoding.C320: estimated_duration = ((filelist[track_index].size / 1024) * 8) / 320 minutes, seconds = divmod(estimated_duration, 60) filesize_string += ' (~%s:%02d)' % (minutes, seconds) self.emit(self.signal_filesize, filesize_string ) logging.info('track in torrent file is size (MB): ' + filesize_string) logging.info('closest match in album: ' + track_found + ' at torrent index: ' + str(track_index)) self._wanted_track_filename = filelist[track_index].path self.emit(self.signal_filename, self._wanted_track_filename.split('\\')[-1]) # track name without path self._check_track_exists() # Set wanted file to normal priority num_files_skipped = [0] * torrentinfo.num_files() num_files_skipped[track_index] = 1 self.torrenthandle.prioritize_files(num_files_skipped) # start thread that displays torrent info self.thread_progress = TorrentProgressThread(self.torrenthandle) self.parent.connect_progress_bar() self.connect(self.thread_progress, self.thread_progress.signal_complete, self._torrent_complete) self.thread_progress.start()
def _get_basic_settings(): set = lt.session_settings() set.user_agent = 'terasaur/' set.seeding_outgoing_connections = False return set
def __init__(self, port_min=6881, port_max=6891, max_download_rate=0, max_upload_rate=0, save_path='.', allocation_mode='compact', proxy_host='', alert_mask=0xfffffff, verbose=False, status_update_interval=0.25, bootstrap_node='router.bittorrent.com', bootstrap_port=6881): """Initialize libtorrent session with supplied parameters. :param port: Listening port. :type port: int :param max_download_rate: The maximum download rate given in kB/s. A value of 0 means the download rate is unbounded. :type max_download_rate: int or float :param max_upload_rate: The maximum upload rate given in kB/s. A value of 0 means the upload rate is unbounded. :type max_upload_rate: int or float :param save_path: The path where the downloaded file/folder should be placed. :type save_path: str :param allocation_mode: Set the mode used for allocating the downloaded files on disk. Possible options include 'full' and 'compact'. :type allocation_mode: str :param proxy_host: Sets a HTTP proxy host and port, separate by a colon. :type proxy_host: str :param alert_mask: Changes the mask of which alerts to receive. By default only errors are reported. It is a bitmask where each bit represents a category of alerts. :type alert_mask: int (bitmask) :param verbose: Indicate if actions should be made verbosely or not. :type verbose: bool :param status_update_interval: The interval at which the session should update the status dictionary with current information about the torrents it is managing. :type status_update_interval: int or float :param bootstrap_node: Boostrap DHT router to connect to. :type boostrap_node: str :param bootstrap_port: Port of boostrap DHT router to connect to. :type bootstrap_port: int """ if port_min < 0 or port_min > 65525 or not isinstance(port_min, int): raise StorjTorrentError( 'port_min must be an integer value between 0 and 65525.') if port_max < port_min or port_max > 65525 or not isinstance(port_max, int): raise StorjTorrentError( ('port_max must be an integer value greater than port_min and' 'less than 65525.')) if max_download_rate <= 0: self.max_download_rate = -1 else: self.max_download_rate = 1000 * max_download_rate if max_upload_rate <= 0: self.max_upload_rate = -1 else: self.max_upload_rate = 1000 * max_upload_rate self.status_update_interval = status_update_interval self.save_path = os.path.abspath(save_path) self.verbose = verbose self.compact_allocation = allocation_mode == 'compact' self.settings = lt.session_settings() self.settings.user_agent = 'Storj/' + __version__ self.session = lt.session() self.session.set_download_rate_limit(self.max_download_rate) self.session.set_upload_rate_limit(self.max_upload_rate) self.session.listen_on(port_min, port_max) self.session.set_settings = self.settings self.session.set_alert_mask(alert_mask) self.session.add_dht_router(bootstrap_node, bootstrap_port) if proxy_host is not '': proxy_settings = lt.proxy_settings() proxy_settings.type = lt.proxy_type.http proxy_settings.hostname = proxy_host.split(':')[0] proxy_settings.port = int(proxy_host.split(':')[1]) self.session.set_proxy(proxy_settings) self.handles = [] self._status = {'torrents': {}, 'alerts': {}} self.alive = True self.subthread = IntervalTimer(status_update_interval, self._watch_torrents) self.subthread.start()
def get_torrent_info(torrent_file, file_index, file_dest, session, u, p_bar, tmp_dir, key=None, client=None, torrent_handle=None): global handle, ses, info, count, count_limit, file_name, ui global progress, total_size_content, tmp_dir_folder, content_length global media_server_key, client_auth_arr, torrent_download_path media_server_key = key client_auth_arr = client content_length = 0 ui = u progress = p_bar tmp_dir_folder = tmp_dir progress.setValue(0) progress.show() if not session: sett = lt.session_settings() sett.user_agent = 'qBittorrent v3.3.5' sett.always_send_user_agent = True fingerprint = lt.fingerprint('qB', 3, 3, 5, 0) ses = lt.session(fingerprint) ses.listen_on(40000, 50000) ses.set_settings(sett) else: ses = session if torrent_file.startswith('magnet:'): handle = lt.add_magnet_uri(ses, torrent_file, {'save_path': file_dest}) i = 0 while (not handle.has_metadata()): time.sleep(1) i = i + 1 if i > 60: print('No Metadata Available') break info = handle.get_torrent_info() elif torrent_handle and torrent_handle.is_valid(): info = lt.torrent_info(torrent_file) handle = torrent_handle else: info = lt.torrent_info(torrent_file) handle = ses.add_torrent({'ti': info, 'save_path': file_dest}) fileIndex = int(file_index) for i, f in enumerate(info.files()): file_exists = False new_path = os.path.join(file_dest, f.path) new_size = f.size if os.path.exists(new_path) and os.stat(new_path).st_size: file_exists = True if fileIndex == i: fileStr = f handle.file_priority(i, 1) elif file_exists: handle.file_priority(i, 1) else: handle.file_priority(i, 0) print(fileStr.path) file_name = os.path.join(file_dest, fileStr.path) torrent_download_path = file_dest content_length = fileStr.size print(content_length, 'content-length') total_size_content = str(int(content_length / (1024 * 1024))) + 'M' pr = info.map_file(fileIndex, 0, fileStr.size) print(pr.length, info.piece_length(), info.num_pieces()) n_pieces = pr.length / info.piece_length() + 1 print(n_pieces) n_pieces = int(n_pieces) count = pr.piece count_limit = pr.piece + n_pieces - 1 assign_piece_priority(handle, info, count, count_limit) print('starting', handle.name()) handle.set_sequential_download(True) if ses.is_paused(): ses.resume() if handle.status().paused: handle.resume() return handle, ses, info, count, count_limit, file_name