2021-01-07 08:34:14 +00:00
|
|
|
import re
|
2022-02-25 18:10:48 +00:00
|
|
|
import sys
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
from itertools import islice
|
2021-01-07 08:34:14 +00:00
|
|
|
from os import path
|
|
|
|
|
|
|
|
from twitchdl import twitch, utils
|
2021-06-09 13:07:07 +00:00
|
|
|
from twitchdl.commands.download import get_clip_authenticated_url
|
2021-01-07 08:34:14 +00:00
|
|
|
from twitchdl.download import download_file
|
|
|
|
from twitchdl.output import print_out, print_clip, print_json
|
|
|
|
|
|
|
|
|
2021-05-18 12:25:49 +00:00
|
|
|
def clips(args):
|
2022-02-25 18:10:48 +00:00
|
|
|
# Ignore --limit if --pager or --all are given
|
|
|
|
limit = sys.maxsize if args.all or args.pager else args.limit
|
|
|
|
|
|
|
|
generator = twitch.channel_clips_generator(args.channel_name, args.period, limit)
|
|
|
|
|
2021-05-18 12:25:49 +00:00
|
|
|
if args.json:
|
2022-02-25 18:10:48 +00:00
|
|
|
return print_json(list(generator))
|
2021-05-18 12:25:49 +00:00
|
|
|
|
|
|
|
if args.download:
|
2022-02-25 18:10:48 +00:00
|
|
|
return _download_clips(generator)
|
|
|
|
|
|
|
|
if args.pager:
|
|
|
|
print(args)
|
|
|
|
return _print_paged(generator, args.pager)
|
2021-05-18 12:25:49 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
return _print_all(generator, args)
|
2021-05-18 12:25:49 +00:00
|
|
|
|
|
|
|
|
2021-01-07 08:34:14 +00:00
|
|
|
def _continue():
|
2022-02-25 18:10:48 +00:00
|
|
|
print_out("Press <green><b>Enter</green> to continue, <yellow><b>Ctrl+C</yellow> to break.")
|
2021-01-07 08:34:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
input()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
def _target_filename(clip):
|
2021-01-07 08:34:14 +00:00
|
|
|
url = clip["videoQualities"][0]["sourceURL"]
|
|
|
|
_, ext = path.splitext(url)
|
|
|
|
ext = ext.lstrip(".")
|
|
|
|
|
|
|
|
match = re.search(r"^(\d{4})-(\d{2})-(\d{2})T", clip["createdAt"])
|
|
|
|
date = "".join(match.groups())
|
|
|
|
|
|
|
|
name = "_".join([
|
|
|
|
date,
|
|
|
|
clip["id"],
|
2021-01-14 20:38:56 +00:00
|
|
|
clip["broadcaster"]["login"],
|
2021-01-07 08:34:14 +00:00
|
|
|
utils.slugify(clip["title"]),
|
|
|
|
])
|
|
|
|
|
|
|
|
return "{}.{}".format(name, ext)
|
|
|
|
|
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
def _download_clips(generator):
|
|
|
|
for clip in generator:
|
|
|
|
target = _target_filename(clip)
|
2021-05-18 12:23:56 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
if path.exists(target):
|
|
|
|
print_out("Already downloaded: <green>{}</green>".format(target))
|
|
|
|
else:
|
2021-06-09 13:07:07 +00:00
|
|
|
url = get_clip_authenticated_url(clip["slug"], "source")
|
2022-02-25 18:10:48 +00:00
|
|
|
print_out("Downloading: <yellow>{}</yellow>".format(target))
|
|
|
|
download_file(url, target)
|
2021-05-18 12:23:56 +00:00
|
|
|
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
def _print_all(generator, args):
|
|
|
|
for clip in generator:
|
|
|
|
print_out()
|
|
|
|
print_clip(clip)
|
2021-05-18 12:23:56 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
if not args.all:
|
|
|
|
print_out(
|
|
|
|
"\n<dim>There may be more clips. " +
|
|
|
|
"Increase the --limit, use --all or --pager to see the rest.</dim>"
|
|
|
|
)
|
2021-01-07 08:34:14 +00:00
|
|
|
|
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
def _print_paged(generator, page_size):
|
|
|
|
iterator = iter(generator)
|
|
|
|
page = list(islice(iterator, page_size))
|
|
|
|
|
|
|
|
first = 1
|
|
|
|
last = first + len(page) - 1
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
while True:
|
2021-01-07 08:34:14 +00:00
|
|
|
print_out("-" * 80)
|
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
print_out()
|
|
|
|
for clip in page:
|
|
|
|
print_clip(clip)
|
2021-01-14 20:38:56 +00:00
|
|
|
print_out()
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
last = first + len(page) - 1
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
print_out("-" * 80)
|
|
|
|
print_out("<yellow>Clips {}-{}</yellow>".format(first, last))
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-25 18:10:48 +00:00
|
|
|
first = first + len(page)
|
|
|
|
last = first + 1
|
|
|
|
|
|
|
|
page = list(islice(iterator, page_size))
|
|
|
|
if not page or not _continue():
|
|
|
|
break
|