2018-01-25 10:09:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-08-12 11:47:48 +00:00
|
|
|
import sys
|
|
|
|
|
2019-06-06 09:06:33 +00:00
|
|
|
from argparse import ArgumentParser, ArgumentTypeError
|
2018-01-25 10:09:20 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2019-02-09 10:52:15 +00:00
|
|
|
from twitchdl.exceptions import ConsoleError
|
|
|
|
from twitchdl.output import print_err
|
2019-07-05 11:14:22 +00:00
|
|
|
from . import commands, __version__
|
2018-01-25 10:09:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
Command = namedtuple("Command", ["name", "description", "arguments"])
|
|
|
|
|
|
|
|
CLIENT_WEBSITE = 'https://github.com/ihabunek/twitch-dl'
|
|
|
|
|
2019-06-06 09:06:33 +00:00
|
|
|
|
|
|
|
def time(value):
|
|
|
|
"""Parse a time string (hh:mm or hh:mm:ss) to number of seconds."""
|
|
|
|
parts = [int(p) for p in value.split(":")]
|
|
|
|
|
|
|
|
if not 2 <= len(parts) <= 3:
|
|
|
|
raise ArgumentTypeError()
|
|
|
|
|
|
|
|
hours = parts[0]
|
|
|
|
minutes = parts[1]
|
|
|
|
seconds = parts[2] if len(parts) > 2 else 0
|
|
|
|
|
|
|
|
if hours < 0 or not (0 <= minutes <= 59) or not (0 <= seconds <= 59):
|
|
|
|
raise ArgumentTypeError()
|
|
|
|
|
|
|
|
return hours * 3600 + minutes * 60 + seconds
|
|
|
|
|
|
|
|
|
2018-01-25 10:09:20 +00:00
|
|
|
COMMANDS = [
|
|
|
|
Command(
|
|
|
|
name="videos",
|
|
|
|
description="List videos from a channel",
|
|
|
|
arguments=[
|
|
|
|
(["channel_name"], {
|
|
|
|
"help": "channel name",
|
|
|
|
"type": str,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Command(
|
|
|
|
name="download",
|
|
|
|
description="Download a video",
|
|
|
|
arguments=[
|
|
|
|
(["video_id"], {
|
|
|
|
"help": "video ID",
|
2019-02-09 10:52:15 +00:00
|
|
|
"type": str,
|
2018-01-25 10:09:20 +00:00
|
|
|
}),
|
|
|
|
(["-w", "--max_workers"], {
|
2019-07-05 10:59:58 +00:00
|
|
|
"help": "maximal number of threads for downloading vods "
|
|
|
|
"concurrently (default 5)",
|
2018-01-25 10:09:20 +00:00
|
|
|
"type": int,
|
2019-04-30 11:36:49 +00:00
|
|
|
"default": 20,
|
2018-01-25 10:09:20 +00:00
|
|
|
}),
|
2019-06-06 09:06:33 +00:00
|
|
|
(["-s", "--start"], {
|
|
|
|
"help": "Download video from this time (hh:mm or hh:mm:ss)",
|
|
|
|
"type": time,
|
|
|
|
"default": None,
|
|
|
|
}),
|
|
|
|
(["-e", "--end"], {
|
|
|
|
"help": "Download video up to this time (hh:mm or hh:mm:ss)",
|
|
|
|
"type": time,
|
|
|
|
"default": None,
|
|
|
|
}),
|
2019-07-05 10:59:58 +00:00
|
|
|
(["-f", "--format"], {
|
|
|
|
"help": "Video format to convert into, passed to ffmpeg as the "
|
|
|
|
"target file extension (default: mkv)",
|
|
|
|
"type": str,
|
|
|
|
"default": "mkv",
|
|
|
|
}),
|
2018-01-25 10:09:20 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
COMMON_ARGUMENTS = [
|
|
|
|
(["--debug"], {
|
|
|
|
"help": "show debug log in console",
|
|
|
|
"action": 'store_true',
|
|
|
|
"default": False,
|
|
|
|
}),
|
|
|
|
(["--no-color"], {
|
|
|
|
"help": "disable ANSI colors in output",
|
|
|
|
"action": 'store_true',
|
|
|
|
"default": False,
|
|
|
|
})
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_parser():
|
|
|
|
description = "A script for downloading videos from Twitch"
|
|
|
|
|
|
|
|
parser = ArgumentParser(prog='twitch-dl', description=description, epilog=CLIENT_WEBSITE)
|
2019-07-05 11:14:22 +00:00
|
|
|
parser.add_argument("--version", help="show version number", action='store_true')
|
|
|
|
|
2018-01-25 10:09:20 +00:00
|
|
|
subparsers = parser.add_subparsers(title="commands")
|
|
|
|
|
|
|
|
for command in COMMANDS:
|
|
|
|
sub = subparsers.add_parser(command.name, help=command.description)
|
|
|
|
|
|
|
|
# Set the function to call to the function of same name in the "commands" package
|
|
|
|
sub.set_defaults(func=commands.__dict__.get(command.name))
|
|
|
|
|
|
|
|
for args, kwargs in command.arguments + COMMON_ARGUMENTS:
|
|
|
|
sub.add_argument(*args, **kwargs)
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = get_parser()
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-07-05 11:14:22 +00:00
|
|
|
if args.version:
|
|
|
|
print("twitch-dl v{}".format(__version__))
|
|
|
|
return
|
|
|
|
|
2018-01-25 10:09:20 +00:00
|
|
|
if "func" not in args:
|
|
|
|
parser.print_help()
|
|
|
|
return
|
|
|
|
|
2019-02-09 10:52:15 +00:00
|
|
|
try:
|
|
|
|
args.func(**args.__dict__)
|
|
|
|
except ConsoleError as e:
|
|
|
|
print_err(e)
|
2019-08-12 11:47:48 +00:00
|
|
|
sys.exit(1)
|