Use console.status across dl command

This commit is contained in:
rlaphoenix 2023-02-25 12:22:32 +00:00
parent 62d91a3e77
commit b2bbc808c4
1 changed files with 156 additions and 157 deletions

View File

@ -141,43 +141,41 @@ class dl:
self.service = Services.get_tag(ctx.invoked_subcommand) self.service = Services.get_tag(ctx.invoked_subcommand)
console.log(f"Loading Profile Data for {self.service}") with console.status("Preparing Service and Profile Authentication...", spinner="dots"):
if profile: if profile:
self.profile = profile self.profile = profile
console.log(f" + Profile: {self.profile} (explicit)") console.log(f"Profile: '{self.profile}' from the --profile argument")
else: else:
self.profile = self.get_profile(self.service) self.profile = self.get_profile(self.service)
console.log(f" + Profile: {self.profile} (from config)") console.log(f"Profile: '{self.profile}' from the config")
console.log("Initializing Widevine CDM") service_config_path = Services.get_path(self.service) / config.filenames.config
if service_config_path.is_file():
self.service_config = yaml.safe_load(service_config_path.read_text(encoding="utf8"))
console.log("Service Config loaded")
else:
self.service_config = {}
merge_dict(config.services.get(self.service), self.service_config)
with console.status("Loading Widevine CDM...", spinner="dots"):
try: try:
self.cdm = self.get_cdm(self.service, self.profile) self.cdm = self.get_cdm(self.service, self.profile)
except ValueError as e: except ValueError as e:
self.log.error(f" - {e}") self.log.error(f"Failed to load Widevine CDM, {e}")
sys.exit(1) sys.exit(1)
console.log( console.log(
f" + {self.cdm.__class__.__name__}: {self.cdm.system_id} (L{self.cdm.security_level})" f"Loaded {self.cdm.__class__.__name__} Widevine CDM: {self.cdm.system_id} (L{self.cdm.security_level})"
) )
console.log("Loading Vaults") with console.status("Loading Key Vaults...", spinner="dots"):
self.vaults = Vaults(self.service) self.vaults = Vaults(self.service)
for vault in config.key_vaults: for vault in config.key_vaults:
vault_type = vault["type"] vault_type = vault["type"]
del vault["type"] del vault["type"]
self.vaults.load(vault_type, **vault) self.vaults.load(vault_type, **vault)
console.log(f" + {len(self.vaults)} Vaults") console.log(f"Loaded {len(self.vaults)} Vaults")
console.log("Getting Service Config") with console.status("Loading Proxy Providers...", spinner="dots"):
service_config_path = Services.get_path(self.service) / config.filenames.config
if service_config_path.is_file():
self.service_config = yaml.safe_load(service_config_path.read_text(encoding="utf8"))
console.log(" + Got Service Config")
else:
self.service_config = {}
console.log(" - No Service Config")
merge_dict(config.services.get(self.service), self.service_config)
console.log("Loading Proxy Providers")
self.proxy_providers = [] self.proxy_providers = []
if config.proxy_providers.get("basic"): if config.proxy_providers.get("basic"):
self.proxy_providers.append(Basic(**config.proxy_providers["basic"])) self.proxy_providers.append(Basic(**config.proxy_providers["basic"]))
@ -186,7 +184,7 @@ class dl:
if get_binary_path("hola-proxy"): if get_binary_path("hola-proxy"):
self.proxy_providers.append(Hola()) self.proxy_providers.append(Hola())
for proxy_provider in self.proxy_providers: for proxy_provider in self.proxy_providers:
console.log(f" + {proxy_provider.__class__.__name__}: {repr(proxy_provider)}") console.log(f"Loaded {proxy_provider.__class__.__name__}: {proxy_provider}")
if proxy: if proxy:
requested_provider = None requested_provider = None
@ -195,7 +193,7 @@ class dl:
requested_provider, proxy = proxy.split(":", maxsplit=1) requested_provider, proxy = proxy.split(":", maxsplit=1)
if re.match(r"^[a-z]{2}(?:\d+)?$", proxy, re.IGNORECASE): if re.match(r"^[a-z]{2}(?:\d+)?$", proxy, re.IGNORECASE):
proxy = proxy.lower() proxy = proxy.lower()
console.log(f"Getting a Proxy to '{proxy}'") with console.status(f"Getting a Proxy to {proxy}...", spinner="dots"):
if requested_provider: if requested_provider:
proxy_provider = next(( proxy_provider = next((
x x
@ -210,16 +208,16 @@ class dl:
self.log.error(f"The proxy provider {requested_provider} had no proxy for {proxy}") self.log.error(f"The proxy provider {requested_provider} had no proxy for {proxy}")
sys.exit(1) sys.exit(1)
proxy = ctx.params["proxy"] = proxy_uri proxy = ctx.params["proxy"] = proxy_uri
console.log(f" + {proxy} (from {proxy_provider.__class__.__name__})") console.log(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
else: else:
for proxy_provider in self.proxy_providers: for proxy_provider in self.proxy_providers:
proxy_uri = proxy_provider.get_proxy(proxy) proxy_uri = proxy_provider.get_proxy(proxy)
if proxy_uri: if proxy_uri:
proxy = ctx.params["proxy"] = proxy_uri proxy = ctx.params["proxy"] = proxy_uri
console.log(f" + {proxy} (from {proxy_provider.__class__.__name__})") console.log(f"Using {proxy_provider.__class__.__name__} Proxy: {proxy}")
break break
else: else:
console.log(f"Proxy: {proxy} (from args)") console.log(f"Using explicit Proxy: {proxy}")
ctx.obj = ContextData( ctx.obj = ContextData(
config=self.service_config, config=self.service_config,
@ -269,20 +267,19 @@ class dl:
vaults_only = not cdm_only vaults_only = not cdm_only
if self.profile: if self.profile:
with console.status("Authenticating with Service...", spinner="dots"):
cookies = self.get_cookie_jar(self.service, self.profile) cookies = self.get_cookie_jar(self.service, self.profile)
credential = self.get_credentials(self.service, self.profile) credential = self.get_credentials(self.service, self.profile)
if not cookies and not credential: if not cookies and not credential:
self.log.error(f"The Profile '{self.profile}' has no Cookies or Credentials. Check for typos.") self.log.error(f"The Profile '{self.profile}' has no Cookies or Credentials, Check for typos")
sys.exit(1) sys.exit(1)
console.log(f"Authenticating with Profile '{self.profile}'")
service.authenticate(cookies, credential) service.authenticate(cookies, credential)
console.log(" + Authenticated") console.log("Authenticated with Service")
console.log("Retrieving Titles") with console.status("Fetching Title Metadata...", spinner="dots"):
titles = service.get_titles() titles = service.get_titles()
if not titles: if not titles:
self.log.error(" - No titles returned!") self.log.error("No titles returned, nothing to download...")
sys.exit(1) sys.exit(1)
for line in str(titles).splitlines(keepends=False): for line in str(titles).splitlines(keepends=False):
@ -300,9 +297,10 @@ class dl:
console.log(f"Getting tracks for {title}") console.log(f"Getting tracks for {title}")
if slow and i != 0: if slow and i != 0:
delay = random.randint(60, 120) delay = random.randint(60, 120)
console.log(f" - Delaying by {delay} seconds due to --slow ...") with console.status(f"Delaying by {delay} seconds..."):
time.sleep(delay) time.sleep(delay)
with console.status("Getting tracks...", spinner="dots"):
title.tracks.add(service.get_tracks(title), warn_only=True) title.tracks.add(service.get_tracks(title), warn_only=True)
title.tracks.add(service.get_chapters(title)) title.tracks.add(service.get_chapters(title))
@ -320,6 +318,7 @@ class dl:
non_sdh_sub.OnDownloaded = lambda x: x.strip_hearing_impaired() non_sdh_sub.OnDownloaded = lambda x: x.strip_hearing_impaired()
title.tracks.add(non_sdh_sub) title.tracks.add(non_sdh_sub)
with console.status("Sorting tracks by language and bitrate...", spinner="dots"):
title.tracks.sort_videos(by_language=v_lang or lang) title.tracks.sort_videos(by_language=v_lang or lang)
title.tracks.sort_audio(by_language=lang) title.tracks.sort_audio(by_language=lang)
title.tracks.sort_subtitles(by_language=s_lang) title.tracks.sort_subtitles(by_language=s_lang)
@ -330,6 +329,7 @@ class dl:
console.log("> Selected Tracks:") # log early so errors logs make sense console.log("> Selected Tracks:") # log early so errors logs make sense
with console.status("Selecting tracks...", spinner="dots"):
if isinstance(title, (Movie, Episode)): if isinstance(title, (Movie, Episode)):
# filter video tracks # filter video tracks
title.tracks.select_video(lambda x: x.codec == vcodec) title.tracks.select_video(lambda x: x.codec == vcodec)
@ -378,7 +378,6 @@ class dl:
if not title.tracks.audio: if not title.tracks.audio:
self.log.error(f"There's no {channels} Audio Track...") self.log.error(f"There's no {channels} Audio Track...")
sys.exit(1) sys.exit(1)
if lang and "all" not in lang: if lang and "all" not in lang:
title.tracks.audio = title.tracks.select_per_language(title.tracks.audio, lang) title.tracks.audio = title.tracks.select_per_language(title.tracks.audio, lang)
if not title.tracks.audio: if not title.tracks.audio: