Add downloading clips

This commit is contained in:
Ivan Habunek 2020-11-10 09:21:37 +01:00
parent 02e4cdaff6
commit 1cb4598a71
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 53 additions and 15 deletions

View File

@ -45,12 +45,32 @@ def _get_game_ids(names):
return game_ids return game_ids
def clips(args): def _clips_json(args):
if args.json:
clips = twitch.get_channel_clips(args.channel_name, args.period, args.limit) clips = twitch.get_channel_clips(args.channel_name, args.period, args.limit)
nodes = list(edge["node"] for edge in clips["edges"]) nodes = list(edge["node"] for edge in clips["edges"])
print_json(nodes) print_json(nodes)
return
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: <green>{}</green>".format(target))
else:
print_out("Downloading: <yellow>{}</yellow>".format(target))
download_file(url, target)
def clips(args):
if args.json:
return _clips_json(args)
if args.download:
return _clips_download(args)
print_out("<dim>Loading clips...</dim>") print_out("<dim>Loading clips...</dim>")
generator = twitch.channel_clips_generator(args.channel_name, args.period, args.limit) 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 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): def _get_vod_paths(playlist, start, end):
"""Extract unique VOD paths for download from playlist.""" """Extract unique VOD paths for download from playlist."""
files = [] files = []
@ -284,18 +322,12 @@ def _download_clip(slug, args):
url = _get_clip_url(clip, args) url = _get_clip_url(clip, args)
print_out("<dim>Selected URL: {}</dim>".format(url)) print_out("<dim>Selected URL: {}</dim>".format(url))
url_path = urlparse(url).path target = _clip_target_filename(clip)
extension = Path(url_path).suffix
filename = "{}_{}{}".format(
clip["broadcaster"]["login"],
utils.slugify(clip["title"]),
extension
)
print_out("Downloading 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): def _download_video(video_id, args):

View File

@ -112,7 +112,12 @@ COMMANDS = [
"help": "If there are more results than LIMIT, ask to show next page", "help": "If there are more results than LIMIT, ask to show next page",
"action": "store_true", "action": "store_true",
"default": False, "default": False,
}) }),
(["-d", "--download"], {
"help": "Download all videos in given period (in source quality)",
"action": "store_true",
"default": False,
}),
], ],
), ),
Command( Command(

View File

@ -109,7 +109,7 @@ def get_channel_clips(channel_id, period, limit, after=None):
query = """ query = """
{{ {{
user(login: "{channel_id}") {{ user(login: "{channel_id}") {{
clips(first: {limit}, after: "{after}", criteria: {{ period: {period} }}) {{ clips(first: {limit}, after: "{after}", criteria: {{ period: {period}, sort: VIEWS_DESC }}) {{
pageInfo {{ pageInfo {{
hasNextPage hasNextPage
hasPreviousPage hasPreviousPage
@ -135,6 +135,7 @@ def get_channel_clips(channel_id, period, limit, after=None):
}} }}
broadcaster {{ broadcaster {{
channel {{ channel {{
name
displayName displayName
}} }}
}} }}