Skip to content

Commit

Permalink
Fix parsing extra arguments for downloader
Browse files Browse the repository at this point in the history
String arguments cannot be passed as-is to the subprocess and need to be
parsed beforehand.
  • Loading branch information
glubsy committed Apr 9, 2023
1 parent 82fec46 commit f0d93f4
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions save_livestream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
import subprocess
import argparse
from shlex import shlex
from time import gmtime, strftime
from random import uniform

Expand Down Expand Up @@ -60,13 +61,14 @@ def download(args, extra=None, quality="best"):
filename = r"{time:%Y%m%d %H-%M-%S} [" + args.author_name + r"] {title} [" + f"{quality}" + r"][{id}].ts"
cmd = [
"streamlink", "--twitch-disable-hosting", "--twitch-disable-ads",
"--hls-live-restart", "--stream-segment-timeout", "30",
"--stream-segment-attempts", "10"
"--hls-live-restart", "--stream-segment-timeout", "30",
"--stream-segment-attempts", "10", "-o", filename
]

if extra:
cmd.extend(extra)

cmd.extend(["-o", filename, args.URI, quality])
cmd.extend([args.URI, quality])

full_output = ""
try:
Expand Down Expand Up @@ -136,16 +138,23 @@ def parse_args():
"URI", metavar="URI",
help="The URI to the channel to monitor OR video to download",
)
return parser.parse_known_args()
args, extra = parser.parse_known_args()

# Pre-parse and sanitize extra positional arguments to pass to downloader
if extra:
pextras = []
for extra_arg in extra:
pextra = shlex(extra_arg)
pextra.whitespace_split = True
for _arg in list(pextra):
pextras.append(_arg.strip("'"))

return args, pextras


def main():
args, extra = parse_args()

# Usage: <author_name> <Twitch_URI
# Example for Twitch: script sovietwomble https://www.twitch.tv/sovietwomble/"
# Example for Twitch: script sovietwomble https://www.twitch.tv/videos/571088399"

if "twitch.tv" not in args.URI:
print("Not a twitch.tv URI. Aborting.")
return 1
Expand All @@ -164,4 +173,7 @@ def main():


if __name__ == '__main__':
# Usage: <author_name> <Twitch_URI
# Example for Twitch: script sovietwomble https://www.twitch.tv/sovietwomble/"
# Example for Twitch: script sovietwomble https://www.twitch.tv/videos/571088399"
exit(main())

0 comments on commit f0d93f4

Please sign in to comment.