This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiosave.py
executable file
·204 lines (155 loc) · 5.52 KB
/
audiosave.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""
Error Codes:
0 Success
-1 youtube_dl error
-2 file_exists_ no_overwrite
"""
import argparse
import os
import youtube_dl
APP_NAME = 'audiosave'
OUTPUT_EXT = {
'vorbis': '.ogg',
'mp3': '.mp3'
}
if os.path.isfile('/usr/share/icons/gnome/32x32/mimetypes/audio-x-generic.png'):
ICON_SUCCESS = '/usr/share/icons/gnome/32x32/mimetypes/audio-x-generic.png'
else:
ICON_SUCCESS = None
if os.path.isfile('/usr/share/icons/gnome/32x32/status/error.png'):
ICON_FAILURE = '/usr/share/icons/gnome/32x32/status/error.png'
else:
ICON_FAILURE = None
def yesno(msg: str) -> bool:
choice = ''
while choice.lower() not in ['y', 'n', 'yes', 'no']:
choice = input(msg.strip() + ' ')
return True if choice[0].lower() == 'y' else False
def coloredText(message: str, type: str) -> str:
try:
from termcolor import colored
except ModuleNotFoundError:
# TODO Manually construct message with ANSI/VT100 CODES
return message
if type == 'success':
return colored(message, 'green') # type: str
elif type == 'error':
return colored(message, 'red') # type: str
else:
return message
def notify(title, body=None, icon=None):
try:
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
from gi.repository import GdkPixbuf
except:
pass
Notify.init(APP_NAME)
notification = Notify.Notification.new(title, body)
if icon:
image = GdkPixbuf.Pixbuf.new_from_file(icon)
notification.set_icon_from_pixbuf(image)
notification.set_image_from_pixbuf(image)
notification.show()
Notify.uninit()
def create_dir(dir_path: str) -> None:
os.makedirs(dir_path)
def move_file(from_, to):
os.rename(from_, to)
def check_dir_exists(dir_path: str) -> bool:
return os.path.isdir(dir_path)
def check_file_exists(file_path: str) -> bool:
return os.path.isfile(file_path)
def download(url: str, codec: str, bitrate: int, title: str = None, quiet: bool = True, force: bool = False):
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': codec,
'preferredquality': str(bitrate),
}],
'outtmpl': '%(title)s.%(etx)s',
'quiet': quiet
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
if title is None:
title = info['title']
if check_file_exists(title + OUTPUT_EXT[codec]) and not force:
if yesno('"{}{}" already exists, overwrite it? (y/n)'.format(title, OUTPUT_EXT[codec])):
overwrite = True
else:
overwrite = False
else:
overwrite = True
if overwrite:
# youtube-dl doesn't seem to raise exceptions (further testing
# is needed)
try:
print("Downloading...")
status = ydl.download([url])
except:
status = -1
move_file(info.get('title', None) + OUTPUT_EXT[codec],
title + OUTPUT_EXT[codec])
return {'status': status, 'info': info}
else:
return {'status': -2}
def parse_args():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('url', help='URL')
# Primary
arg_parser.add_argument('-d', '--dir', help='Save directory')
arg_parser.add_argument('-t', '--title',
help='Override filename (minus extension) (default\'s to video title)')
# Media
arg_parser.add_argument('--bitrate', default=192, help='Bitrate')
arg_parser.add_argument('--codec', default='mp3',
help='Audio codec (see youtube-dl supported codecs)')
# Extra
arg_parser.add_argument('-f', '--force', action='store_true',
help='Overwrite existing')
arg_parser.add_argument('--notify', action='store_true',
help='Show notification')
arg_parser.add_argument('-v', '--verbose', action='store_true',
help='Show all output')
return arg_parser.parse_args()
def handle_download(result, codec: str, show_notif: bool = True) -> None:
status = result['status']
if status != -2:
title = result['info'].get('title', None)
msg = 'Downloaded "{}{}"'.format(title, OUTPUT_EXT[codec])
else:
msg = 'Unknown error'
if status == 0:
print(coloredText(msg, 'success'))
if show_notif:
notify(msg, icon=ICON_SUCCESS)
return 0
if status == -2:
return 0
else:
msg_title = 'Download failed for "{}{}"'.format(title, OUTPUT_EXT[codec])
msg_body = 'Error {}'.format(status)
print(coloredText(msg_title, 'error'))
print(msg_body)
if show_notif:
notify(msg_title, msg_body, icon=ICON_FAILURE)
return status
def main():
args = parse_args()
if args.dir:
if not check_dir_exists(args.dir):
if yesno(f'{args.dir} does not exist. Create it?'):
create_dir(args.dir)
os.chdir(args.dir)
result = download(args.url, codec=args.codec, bitrate=args.bitrate,
title=args.title, quiet=not(args.verbose), force=args.force)
handle_download(result, args.codec, args.notify)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass