diff --git a/twitchdl/commands.py b/twitchdl/commands.py
index 665db52..be06c89 100644
--- a/twitchdl/commands.py
+++ b/twitchdl/commands.py
@@ -45,12 +45,32 @@ def _get_game_ids(names):
return game_ids
+def _clips_json(args):
+ clips = twitch.get_channel_clips(args.channel_name, args.period, args.limit)
+ nodes = list(edge["node"] for edge in clips["edges"])
+ print_json(nodes)
+
+
+def _clips_download(args):
+ generator = twitch.channel_clips_generator(args.channel_name, args.period, 100)
+ for clips, _ in generator:
+ for clip in clips["edges"]:
+ clip = clip["node"]
+ url = clip["videoQualities"][0]["sourceURL"]
+ target = _clip_target_filename(clip)
+ if path.exists(target):
+ print_out("Already downloaded: {}".format(target))
+ else:
+ print_out("Downloading: {}".format(target))
+ download_file(url, target)
+
+
def clips(args):
if args.json:
- clips = twitch.get_channel_clips(args.channel_name, args.period, args.limit)
- nodes = list(edge["node"] for edge in clips["edges"])
- print_json(nodes)
- return
+ return _clips_json(args)
+
+ if args.download:
+ return _clips_download(args)
print_out("Loading clips...")
generator = twitch.channel_clips_generator(args.channel_name, args.period, args.limit)
@@ -183,6 +203,24 @@ def _video_target_filename(video, format):
return name + "." + format
+def _clip_target_filename(clip):
+ 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"],
+ clip["broadcaster"]["channel"]["name"],
+ utils.slugify(clip["title"]),
+ ])
+
+ return "{}.{}".format(name, ext)
+
+
def _get_vod_paths(playlist, start, end):
"""Extract unique VOD paths for download from playlist."""
files = []
@@ -284,18 +322,12 @@ def _download_clip(slug, args):
url = _get_clip_url(clip, args)
print_out("Selected URL: {}".format(url))
- url_path = urlparse(url).path
- extension = Path(url_path).suffix
- filename = "{}_{}{}".format(
- clip["broadcaster"]["login"],
- utils.slugify(clip["title"]),
- extension
- )
+ target = _clip_target_filename(clip)
print_out("Downloading clip...")
- download_file(url, filename)
+ download_file(url, target)
- print_out("Downloaded: {}".format(filename))
+ print_out("Downloaded: {}".format(target))
def _download_video(video_id, args):
diff --git a/twitchdl/console.py b/twitchdl/console.py
index 2a58e06..b6637d0 100644
--- a/twitchdl/console.py
+++ b/twitchdl/console.py
@@ -112,7 +112,12 @@ COMMANDS = [
"help": "If there are more results than LIMIT, ask to show next page",
"action": "store_true",
"default": False,
- })
+ }),
+ (["-d", "--download"], {
+ "help": "Download all videos in given period (in source quality)",
+ "action": "store_true",
+ "default": False,
+ }),
],
),
Command(
diff --git a/twitchdl/twitch.py b/twitchdl/twitch.py
index 971d68f..80a25c4 100644
--- a/twitchdl/twitch.py
+++ b/twitchdl/twitch.py
@@ -109,7 +109,7 @@ def get_channel_clips(channel_id, period, limit, after=None):
query = """
{{
user(login: "{channel_id}") {{
- clips(first: {limit}, after: "{after}", criteria: {{ period: {period} }}) {{
+ clips(first: {limit}, after: "{after}", criteria: {{ period: {period}, sort: VIEWS_DESC }}) {{
pageInfo {{
hasNextPage
hasPreviousPage
@@ -135,6 +135,7 @@ def get_channel_clips(channel_id, period, limit, after=None):
}}
broadcaster {{
channel {{
+ name
displayName
}}
}}