This repository was archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyt-dl
More file actions
executable file
·44 lines (35 loc) · 1.71 KB
/
Copy pathyt-dl
File metadata and controls
executable file
·44 lines (35 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
import os
import argparse
parser = argparse.ArgumentParser(description="Download a video using youtube-dl, and then do something with it")
parser.add_argument("--open", action="store_true", help="Open this video after it finishes downloading")
parser.add_argument("--wait", action="store_true", help="After this video finishes downloading, wait for the user to hit enter, and then open it.")
parser.add_argument("--name", help="Change the name from the default youtube-dl file to the passed name")
dest = parser.add_mutually_exclusive_group()
dest.add_argument("--downloads", action="store_true", default=False, help="Change download destination to ~/Downloads")
dest.add_argument("--tmp", action="store_true", help="Download a video to the /tmp directory, it will be deleted on computer restart")
args, unknownargs = parser.parse_known_args()
# give youtube-dl leftover arguments
youtube_dl = " ".join(["youtube-dl"] + unknownargs)
# if we have to wait before opening, we should be opening
if args.wait:
args.open = True
# create absolute path to download directory
dir = os.path.abspath(".")
if args.tmp:
dir = os.path.abspath("/tmp")
elif args.downloads:
dir = os.path.join(os.path.expanduser("~"), "Downloads")
# if given a custom name, use it instead
name = ""
if args.name is not None and args.name.strip():
name = "-o '{}.%(ext)s'".format(args.name)
# use youtube-dl's --exec flag to open the files once downloaded
exec_str = ""
if args.wait:
exec_str = "--exec \"echo 'Press \'enter/return\' to open...'; read; open {}\""
elif args.open:
exec_str = '--exec "open {}"'
youtube_dl = f"cd {dir}; {youtube_dl} {name} {exec_str}"
# call youtube-dl with a system call
os.system(youtube_dl)