-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcuts_ytdl.py
75 lines (61 loc) · 1.57 KB
/
shortcuts_ytdl.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# coding: utf-8
from __future__ import unicode_literals
import webbrowser
import ui
from os import remove
from os.path import splitext
from zipfile import ZipFile
import youtube_dl
import clipboard
import sys
from base64 import b64encode
files = []
def main(argv):
if len(argv) > 1:
url = argv[1]
mode = argv[2]
# Should we download videos or just audio?
if mode == 'audio':
format = 'bestaudio[ext!*=webm]/best'
else: # video
format = 'best'
# https://youtu.be/f0uHpasSpq0
if url:
ydl_opts = {
'progress_hooks': [postdownload],
# 'writethumbnail': True,
'format': format,
'ignoreerrors': True
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# Make zip file with video(s)
with ZipFile('yt.zip', 'x') as z:
for file in files:
z.write(file)
remove(file)
# Save ZIP file to clipboard
with open('yt.zip', 'rb') as f:
f64 = b64encode(f.read())
clipboard.set(f64.decode())
# Remove the ZIP, as it's on the clipboard
remove('yt.zip')
# Call Shortcuts
url = "shortcuts://run-shortcut?name=yt_save3&input=clipboard"
webbrowser.open(url)
def postdownload(d):
if d['status'] == 'finished':
# Get filename of json info saved by yt-dl
base_filename = splitext(d['filename'])[0]
#json_filename = "{}.info.json".format(base_filename)
#thumbnail = "{}.jpg".format(base_filename)
# Add to list
files.append(d['filename'])
# class download:
# def __init__(self):
# self.d = ""
#
# def postd(self, d):
# self.d = d
if __name__ == '__main__':
main(sys.argv)