mirror of
https://github.com/ihabunek/twitch-dl
synced 2024-08-30 18:32:25 +00:00
Allow using video URL as arg to download command
This commit is contained in:
parent
da60560b63
commit
7d152f6424
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
4
twitchdl/exceptions.py
Normal file
4
twitchdl/exceptions.py
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
class ConsoleError(Exception):
|
||||
"""Raised when an error occurs and script exectuion should halt."""
|
||||
pass
|
Loading…
Reference in New Issue
Block a user