Allow using video URL as arg to download command

This commit is contained in:
Ivan Habunek 2019-02-09 11:52:15 +01:00
parent da60560b63
commit 7d152f6424
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 26 additions and 2 deletions

View File

@ -10,6 +10,7 @@ from functools import partial
from twitchdl import twitch from twitchdl import twitch
from twitchdl.download import download_file from twitchdl.download import download_file
from twitchdl.exceptions import ConsoleError
from twitchdl.output import print_out from twitchdl.output import print_out
from twitchdl.utils import slugify from twitchdl.utils import slugify
@ -157,7 +158,21 @@ def _video_target_filename(video, format):
return name + "." + 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): def download(video_id, max_workers, format='mkv', **kwargs):
video_id = parse_video_id(video_id)
print("Looking up video...") print("Looking up video...")
video = twitch.get_video(video_id) video = twitch.get_video(video_id)

View File

@ -3,6 +3,8 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from collections import namedtuple from collections import namedtuple
from twitchdl.exceptions import ConsoleError
from twitchdl.output import print_err
from . import commands from . import commands
@ -27,7 +29,7 @@ COMMANDS = [
arguments=[ arguments=[
(["video_id"], { (["video_id"], {
"help": "video ID", "help": "video ID",
"type": int, "type": str,
}), }),
(["-w", "--max_workers"], { (["-w", "--max_workers"], {
"help": "maximal number of threads for downloading vods concurrently (default 5)", "help": "maximal number of threads for downloading vods concurrently (default 5)",
@ -78,4 +80,7 @@ def main():
parser.print_help() parser.print_help()
return return
try:
args.func(**args.__dict__) args.func(**args.__dict__)
except ConsoleError as e:
print_err(e)

4
twitchdl/exceptions.py Normal file
View File

@ -0,0 +1,4 @@
class ConsoleError(Exception):
"""Raised when an error occurs and script exectuion should halt."""
pass