Compare commits

...

3 Commits

Author SHA1 Message Date
six 65b95dbfe7
Merge 143f0c9f41 into 64766459e3 2024-04-28 02:45:07 +05:30
Simon Sawicki 64766459e3
[core/windows] Improve shell quoting and tests (#9802)
Authored by: Grub4K
2024-04-27 10:37:26 +02:00
lostfictions 143f0c9f41 add nts.live extractor 2024-04-07 04:06:29 +00:00
4 changed files with 94 additions and 22 deletions

View File

@ -2059,7 +2059,22 @@ Line 1
assert extract_basic_auth('http://user:pass@foo.bar') == ('http://foo.bar', 'Basic dXNlcjpwYXNz')
@unittest.skipUnless(compat_os_name == 'nt', 'Only relevant on Windows')
def test_Popen_windows_escaping(self):
def test_windows_escaping(self):
tests = [
'test"&',
'%CMDCMDLINE:~-1%&',
'a\nb',
'"',
'\\',
'!',
'^!',
'a \\ b',
'a \\" b',
'a \\ b\\',
# We replace \r with \n
('a\r\ra', 'a\n\na'),
]
def run_shell(args):
stdout, stderr, error = Popen.run(
args, text=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@ -2067,15 +2082,18 @@ Line 1
assert not error
return stdout
# Test escaping
assert run_shell(['echo', 'test"&']) == '"test""&"\n'
assert run_shell(['echo', '%CMDCMDLINE:~-1%&']) == '"%CMDCMDLINE:~-1%&"\n'
assert run_shell(['echo', 'a\nb']) == '"a"\n"b"\n'
assert run_shell(['echo', '"']) == '""""\n'
assert run_shell(['echo', '\\']) == '\\\n'
# Test if delayed expansion is disabled
assert run_shell(['echo', '^!']) == '"^!"\n'
assert run_shell('echo "^!"') == '"^!"\n'
for argument in tests:
if isinstance(argument, str):
expected = argument
else:
argument, expected = argument
args = [sys.executable, '-c', 'import sys; print(end=sys.argv[1])', argument, 'end']
assert run_shell(args) == expected
escaped = shell_quote(argument, shell=True)
args = f'{sys.executable} -c "import sys; print(end=sys.argv[1])" {escaped} end'
assert run_shell(args) == expected
if __name__ == '__main__':

View File

@ -1334,6 +1334,7 @@ from .nrk import (
NRKTVSeriesIE,
)
from .nrl import NRLTVIE
from .nts import NTSLiveIE
from .ntvcojp import NTVCoJpCUIE
from .ntvde import NTVDeIE
from .ntvru import NTVRuIE

60
yt_dlp/extractor/nts.py Normal file
View File

@ -0,0 +1,60 @@
from .common import InfoExtractor
from ..utils import extract_attributes
class NTSLiveIE(InfoExtractor):
IE_NAME = 'nts.live'
_VALID_URL = r'https?://(?:www\.)?nts\.live/shows/[^/]+/episodes/(?P<id>[^./?#]+)'
_TESTS = [
{
# embedded soundcloud
'url': 'https://www.nts.live/shows/yu-su/episodes/yu-su-2nd-april-2024',
'md5': 'b5444c04888c869d68758982de1a27d8',
'info_dict': {
'id': '1791563518',
'ext': 'opus',
'uploader_id': '995579326',
'title': 'Pender Street Steppers & YU SU 020424',
'timestamp': 1712143743,
'upload_date': '20240403',
'thumbnail': 'https://i1.sndcdn.com/artworks-qKcNO0z0AQGGbv9s-GljJCw-original.jpg',
'license': 'all-rights-reserved',
'repost_count': int,
'uploader_url': 'https://soundcloud.com/user-643553014',
'uploader': 'NTS Latest',
'description': 'md5:cf9d7d7997ef00a7b6046c9615b72c55',
'duration': 10784.157,
}
},
{
# embedded mixcloud
'url': 'https://www.nts.live/shows/absolute-fiction/episodes/absolute-fiction-23rd-july-2022',
'md5': 'TODO',
'info_dict': {
'id': 'NTSRadio_absolute-fiction-23rd-july-2022',
'ext': 'webm',
'like_count': int,
'title': 'Absolute Fiction - 23rd July 2022',
'comment_count': int,
'uploader_url': 'https://www.mixcloud.com/NTSRadio/',
'description': 'md5:a8cb9d9f040adb6525d33a6604fc759c',
'tags': [],
'duration': 3529,
'timestamp': 1658772398,
'repost_count': int,
'upload_date': '20220725',
'uploader_id': 'NTSRadio',
'thumbnail': 'https://thumbnailer.mixcloud.com/unsafe/1024x1024/extaudio/5/1/a/d/ae3e-1be9-4fd4-983e-9c3294226eac',
'uploader': 'Mixcloud NTS Radio',
}
}
]
def _real_extract(self, url):
video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id)
attrs = extract_attributes(self._search_regex(
r'(<button[^>]+?aria-label="Play"[^>]*?>)', webpage, 'player'))
return self.url_result(attrs['data-src'])

View File

@ -1638,16 +1638,14 @@ def get_filesystem_encoding():
return encoding if encoding is not None else 'utf-8'
_WINDOWS_QUOTE_TRANS = str.maketrans({'"': '\\"', '\\': '\\\\'})
_WINDOWS_QUOTE_TRANS = str.maketrans({'"': R'\"'})
_CMD_QUOTE_TRANS = str.maketrans({
# Keep quotes balanced by replacing them with `""` instead of `\\"`
'"': '""',
# Requires a variable `=` containing `"^\n\n"` (set in `utils.Popen`)
# These require an env-variable `=` containing `"^\n\n"` (set in `utils.Popen`)
# `=` should be unique since variables containing `=` cannot be set using cmd
'\n': '%=%',
# While we are only required to escape backslashes immediately before quotes,
# we instead escape all of 'em anyways to be consistent
'\\': '\\\\',
'\r': '%=%',
# Use zero length variable replacement so `%` doesn't get expanded
# `cd` is always set as long as extensions are enabled (`/E:ON` in `utils.Popen`)
'%': '%%cd:~,%',
@ -1656,19 +1654,14 @@ _CMD_QUOTE_TRANS = str.maketrans({
def shell_quote(args, *, shell=False):
args = list(variadic(args))
if any(isinstance(item, bytes) for item in args):
deprecation_warning('Passing bytes to utils.shell_quote is deprecated')
encoding = get_filesystem_encoding()
for index, item in enumerate(args):
if isinstance(item, bytes):
args[index] = item.decode(encoding)
if compat_os_name != 'nt':
return shlex.join(args)
trans = _CMD_QUOTE_TRANS if shell else _WINDOWS_QUOTE_TRANS
return ' '.join(
s if re.fullmatch(r'[\w#$*\-+./:?@\\]+', s, re.ASCII) else s.translate(trans).join('""')
s if re.fullmatch(r'[\w#$*\-+./:?@\\]+', s, re.ASCII)
else re.sub(r'(\\+)("|$)', r'\1\1\2', s).translate(trans).join('""')
for s in args)