mirror of
https://github.com/ihabunek/twitch-dl
synced 2024-08-30 18:32:25 +00:00
Improve parsing inputs for download command
* Fix overzealous regex which caused video patterns to be identified as clips by testing for videos before clips. * Allow URLs with or without `www.`. * Add tests which verify this actually works. fixes #28
This commit is contained in:
parent
f456d04de6
commit
78295a492c
3
Makefile
3
Makefile
@ -19,3 +19,6 @@ deb:
|
|||||||
|
|
||||||
man:
|
man:
|
||||||
scdoc < twitch-dl.1.scd > twitch-dl.1.man
|
scdoc < twitch-dl.1.scd > twitch-dl.1.man
|
||||||
|
|
||||||
|
test:
|
||||||
|
pytest
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
pytest-cov~=2.4.0
|
pytest
|
||||||
pytest~=3.0.0
|
twine
|
||||||
stdeb~=0.8.5
|
wheel
|
||||||
twine~=1.8.1
|
|
||||||
wheel~=0.29.0
|
|
||||||
|
45
tests/test_download.py
Normal file
45
tests/test_download.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
from twitchdl.commands import download
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
|
Args = namedtuple("args", ["video"])
|
||||||
|
|
||||||
|
|
||||||
|
TEST_VIDEO_PATTERNS = [
|
||||||
|
("702689313", "702689313"),
|
||||||
|
("702689313", "https://twitch.tv/videos/702689313"),
|
||||||
|
("702689313", "https://www.twitch.tv/videos/702689313"),
|
||||||
|
]
|
||||||
|
|
||||||
|
TEST_CLIP_PATTERNS = {
|
||||||
|
("AbrasivePlayfulMangoMau5", "AbrasivePlayfulMangoMau5"),
|
||||||
|
("AbrasivePlayfulMangoMau5", "https://clips.twitch.tv/AbrasivePlayfulMangoMau5"),
|
||||||
|
("AbrasivePlayfulMangoMau5", "https://www.twitch.tv/dracul1nx/clip/AbrasivePlayfulMangoMau5"),
|
||||||
|
("AbrasivePlayfulMangoMau5", "https://twitch.tv/dracul1nx/clip/AbrasivePlayfulMangoMau5"),
|
||||||
|
("HungryProudRadicchioDoggo", "HungryProudRadicchioDoggo"),
|
||||||
|
("HungryProudRadicchioDoggo", "https://clips.twitch.tv/HungryProudRadicchioDoggo"),
|
||||||
|
("HungryProudRadicchioDoggo", "https://www.twitch.tv/bananasaurus_rex/clip/HungryProudRadicchioDoggo?filter=clips&range=7d&sort=time"),
|
||||||
|
("HungryProudRadicchioDoggo", "https://twitch.tv/bananasaurus_rex/clip/HungryProudRadicchioDoggo?filter=clips&range=7d&sort=time"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@patch("twitchdl.commands._download_clip")
|
||||||
|
@patch("twitchdl.commands._download_video")
|
||||||
|
@pytest.mark.parametrize("expected,input", TEST_VIDEO_PATTERNS)
|
||||||
|
def test_video_patterns(video_dl, clip_dl, expected, input):
|
||||||
|
args = Args(video=input)
|
||||||
|
download(args)
|
||||||
|
video_dl.assert_called_once_with(expected, args)
|
||||||
|
clip_dl.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
@patch("twitchdl.commands._download_clip")
|
||||||
|
@patch("twitchdl.commands._download_video")
|
||||||
|
@pytest.mark.parametrize("expected,input", TEST_CLIP_PATTERNS)
|
||||||
|
def test_clip_patterns(video_dl, clip_dl, expected, input):
|
||||||
|
args = Args(video=input)
|
||||||
|
download(args)
|
||||||
|
clip_dl.assert_called_once_with(expected, args)
|
||||||
|
video_dl.assert_not_called()
|
@ -164,29 +164,29 @@ def _crete_temp_dir(base_uri):
|
|||||||
|
|
||||||
VIDEO_PATTERNS = [
|
VIDEO_PATTERNS = [
|
||||||
r"^(?P<id>\d+)?$",
|
r"^(?P<id>\d+)?$",
|
||||||
r"^https://www.twitch.tv/videos/(?P<id>\d+)(\?.+)?$",
|
r"^https://(www.)?twitch.tv/videos/(?P<id>\d+)(\?.+)?$",
|
||||||
]
|
]
|
||||||
|
|
||||||
CLIP_PATTERNS = [
|
CLIP_PATTERNS = [
|
||||||
r"^(?P<slug>[A-Za-z0-9]+)$",
|
r"^(?P<slug>[A-Za-z0-9]+)$",
|
||||||
r"^https://www.twitch.tv/\w+/clip/(?P<slug>[A-Za-z0-9]+)(\?.+)?$",
|
r"^https://(www.)?twitch.tv/\w+/clip/(?P<slug>[A-Za-z0-9]+)(\?.+)?$",
|
||||||
r"^https://clips.twitch.tv/(?P<slug>[A-Za-z0-9]+)(\?.+)?$",
|
r"^https://clips.twitch.tv/(?P<slug>[A-Za-z0-9]+)(\?.+)?$",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def download(args):
|
def download(args):
|
||||||
for pattern in CLIP_PATTERNS:
|
|
||||||
match = re.match(pattern, args.video)
|
|
||||||
if match:
|
|
||||||
clip_slug = match.group('slug')
|
|
||||||
return _download_clip(clip_slug, args)
|
|
||||||
|
|
||||||
for pattern in VIDEO_PATTERNS:
|
for pattern in VIDEO_PATTERNS:
|
||||||
match = re.match(pattern, args.video)
|
match = re.match(pattern, args.video)
|
||||||
if match:
|
if match:
|
||||||
video_id = match.group('id')
|
video_id = match.group('id')
|
||||||
return _download_video(video_id, args)
|
return _download_video(video_id, args)
|
||||||
|
|
||||||
|
for pattern in CLIP_PATTERNS:
|
||||||
|
match = re.match(pattern, args.video)
|
||||||
|
if match:
|
||||||
|
clip_slug = match.group('slug')
|
||||||
|
return _download_clip(clip_slug, args)
|
||||||
|
|
||||||
raise ConsoleError("Invalid video: {}".format(args.video))
|
raise ConsoleError("Invalid video: {}".format(args.video))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user