Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yt-dlp/yt-dlp into ytdlp
Browse files Browse the repository at this point in the history
* 'master' of https://github.com/yt-dlp/yt-dlp:
  [extractor/crunchyroll] Rework with support for movies, music and artists (#6237)
  [core] Implement `--color` flag (#6904)
  [jsinterp] Do not compile regex
  [misc] Add automatic duplicate issue detection
  Update to ytdl-commit-d1c6c5
  [devscripts/cli_to_api] Add script
  • Loading branch information
Lesmiscore committed May 25, 2023
2 parents d3a1fcf + 032de83 commit 288050f
Show file tree
Hide file tree
Showing 9 changed files with 553 additions and 193 deletions.
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -533,8 +533,12 @@ If you fork the project on GitHub, you can run your fork's [build workflow](.git
--no-wait-for-video Do not wait for scheduled streams (default)
--mark-watched Mark videos watched (even with --simulate)
--no-mark-watched Do not mark videos watched (default)
--no-colors Do not emit color codes in output (Alias:
--no-colours)
--color [STREAM:]POLICY Whether to emit color codes in output,
optionally prefixed by the STREAM (stdout or
stderr) to apply the setting to. Can be one
of "always", "auto" (default), "never", or
"no_color" (use non color terminal
sequences). Can be used multiple times
--compat-options OPTS Options that can help keep compatibility
with youtube-dl or youtube-dlc
configurations by reverting some of the
Expand Down Expand Up @@ -2266,6 +2270,7 @@ While these options are redundant, they are still expected to be used due to the
--playlist-end NUMBER -I :NUMBER
--playlist-reverse -I ::-1
--no-playlist-reverse Default
--no-colors --color no_color


#### Not recommended
Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.py
Expand Up @@ -2020,7 +2020,7 @@ def test_variadic(self):
self.assertEqual(variadic(None), (None, ))
self.assertEqual(variadic('spam'), ('spam', ))
self.assertEqual(variadic('spam', allowed_types=dict), 'spam')
with warnings.catch_warnings(category=DeprecationWarning):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
self.assertEqual(variadic('spam', allowed_types=[dict]), 'spam')

Expand Down
36 changes: 31 additions & 5 deletions yt_dlp/YoutubeDL.py
Expand Up @@ -452,7 +452,12 @@ class YoutubeDL:
- Raise utils.DownloadCancelled(msg) to abort remaining
downloads when a video is rejected.
match_filter_func in utils.py is one example for this.
no_color: Do not emit color codes in output.
color: A Dictionary with output stream names as keys
and their respective color policy as values.
Can also just be a single color policy,
in which case it applies to all outputs.
Valid stream names are 'stdout' and 'stderr'.
Valid color policies are one of 'always', 'auto', 'no_color' or 'never'.
geo_bypass: Bypass geographic restriction via faking X-Forwarded-For
HTTP header
geo_bypass_country:
Expand Down Expand Up @@ -591,6 +596,7 @@ class YoutubeDL:
data will be downloaded and processed by extractor.
You can reduce network I/O by disabling it if you don't
care about HLS. (only for youtube)
no_color: Same as `color='no_color'`
"""

_NUMERIC_FIELDS = {
Expand Down Expand Up @@ -658,9 +664,24 @@ def __init__(self, params=None, auto_init=True):
except Exception as e:
self.write_debug(f'Failed to enable VT mode: {e}')

if self.params.get('no_color'):
if self.params.get('color') is not None:
self.report_warning('Overwriting params from "color" with "no_color"')
self.params['color'] = 'no_color'

term_allow_color = os.environ.get('TERM', '').lower() != 'dumb'

def process_color_policy(stream):
stream_name = {sys.stdout: 'stdout', sys.stderr: 'stderr'}[stream]
policy = traverse_obj(self.params, ('color', (stream_name, None), {str}), get_all=False)
if policy in ('auto', None):
return term_allow_color and supports_terminal_sequences(stream)
assert policy in ('always', 'never', 'no_color')
return {'always': True, 'never': False}.get(policy, policy)

self._allow_colors = Namespace(**{
type_: not self.params.get('no_color') and supports_terminal_sequences(stream)
for type_, stream in self._out_files.items_ if type_ != 'console'
name: process_color_policy(stream)
for name, stream in self._out_files.items_ if name != 'console'
})

# The code is left like this to be reused for future deprecations
Expand Down Expand Up @@ -1032,7 +1053,7 @@ def _format_text(self, handle, allow_colors, text, f, fallback=None, *, test_enc
text = text.encode(encoding, 'ignore').decode(encoding)
if fallback is not None and text != original_text:
text = fallback
return format_text(text, f) if allow_colors else text if fallback is None else fallback
return format_text(text, f) if allow_colors is True else text if fallback is None else fallback

def _format_out(self, *args, **kwargs):
return self._format_text(self._out_files.out, self._allow_colors.out, *args, **kwargs)
Expand Down Expand Up @@ -4071,9 +4092,14 @@ def print_debug_header(self):

def get_encoding(stream):
ret = str(getattr(stream, 'encoding', 'missing (%s)' % type(stream).__name__))
additional_info = []
if os.environ.get('TERM', '').lower() == 'dumb':
additional_info.append('dumb')
if not supports_terminal_sequences(stream):
from .utils import WINDOWS_VT_MODE # Must be imported locally
ret += ' (No VT)' if WINDOWS_VT_MODE is False else ' (No ANSI)'
additional_info.append('No VT' if WINDOWS_VT_MODE is False else 'No ANSI')
if additional_info:
ret = f'{ret} ({",".join(additional_info)})'
return ret

encoding_str = 'Encodings: locale %s, fs %s, pref %s, %s' % (
Expand Down
6 changes: 5 additions & 1 deletion yt_dlp/__init__.py
Expand Up @@ -448,6 +448,10 @@ def metadataparser_actions(f):
elif ed and proto == 'default':
default_downloader = ed.get_basename()

for policy in opts.color.values():
if policy not in ('always', 'auto', 'no_color', 'never'):
raise ValueError(f'"{policy}" is not a valid color policy')

warnings, deprecation_warnings = [], []

# Common mistake: -f best
Expand Down Expand Up @@ -915,7 +919,7 @@ def parse_options(argv=None, ignore_config_files='if_override'):
'playlist_items': opts.playlist_items,
'xattr_set_filesize': opts.xattr_set_filesize,
'match_filter': opts.match_filter,
'no_color': opts.no_color,
'color': opts.color,
'ffmpeg_location': opts.ffmpeg_location,
'hls_prefer_native': opts.hls_prefer_native,
'hls_use_mpegts': opts.hls_use_mpegts,
Expand Down
2 changes: 2 additions & 0 deletions yt_dlp/extractor/_extractors.py
Expand Up @@ -409,6 +409,8 @@
from .crunchyroll import (
CrunchyrollBetaIE,
CrunchyrollBetaShowIE,
CrunchyrollMusicIE,
CrunchyrollArtistIE,
)
from .cspan import CSpanIE, CSpanCongressIE
from .ctsnews import CtsNewsIE
Expand Down

0 comments on commit 288050f

Please sign in to comment.