diff --git a/twitchdl/commands.py b/twitchdl/commands.py index 341881d..8798150 100644 --- a/twitchdl/commands.py +++ b/twitchdl/commands.py @@ -10,6 +10,7 @@ from functools import partial from twitchdl import twitch from twitchdl.download import download_file +from twitchdl.exceptions import ConsoleError from twitchdl.output import print_out from twitchdl.utils import slugify @@ -157,7 +158,21 @@ def _video_target_filename(video, format): return name + "." + format +def parse_video_id(video_id): + """This can be either a integer ID or an URL to the video on twitch.""" + if re.search(r"^\d+$", video_id): + return int(video_id) + + match = re.search(r"^https://www.twitch.tv/videos/(\d+)$", video_id) + if match: + return int(match.group(1)) + + raise ConsoleError("Invalid video ID given, expected integer ID or Twitch URL") + + def download(video_id, max_workers, format='mkv', **kwargs): + video_id = parse_video_id(video_id) + print("Looking up video...") video = twitch.get_video(video_id) diff --git a/twitchdl/console.py b/twitchdl/console.py index 4ba1807..bb00531 100644 --- a/twitchdl/console.py +++ b/twitchdl/console.py @@ -3,6 +3,8 @@ from argparse import ArgumentParser from collections import namedtuple +from twitchdl.exceptions import ConsoleError +from twitchdl.output import print_err from . import commands @@ -27,7 +29,7 @@ COMMANDS = [ arguments=[ (["video_id"], { "help": "video ID", - "type": int, + "type": str, }), (["-w", "--max_workers"], { "help": "maximal number of threads for downloading vods concurrently (default 5)", @@ -78,4 +80,7 @@ def main(): parser.print_help() return - args.func(**args.__dict__) + try: + args.func(**args.__dict__) + except ConsoleError as e: + print_err(e) diff --git a/twitchdl/exceptions.py b/twitchdl/exceptions.py new file mode 100644 index 0000000..d0ff78d --- /dev/null +++ b/twitchdl/exceptions.py @@ -0,0 +1,4 @@ + +class ConsoleError(Exception): + """Raised when an error occurs and script exectuion should halt.""" + pass