-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediadl.py
87 lines (68 loc) · 1.97 KB
/
mediadl.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
import re, ssl, subprocess
from InquirerPy import inquirer
from pytube import YouTube, Playlist
from utils import *
import youtube
import soundcloud
def is_yt_video(link: str):
return re.match(r"https:\/\/www\.youtube\.com\/watch\?v=[\w_-]{11}(&list=[\w_-]{34}&index=[0-9]+)?", link)
def is_yt_playlist(link: str):
return re.match(r"https:\/\/www\.youtube\.com\/playlist\?list=[\w_-]{34}", link)
def is_soundcloud(link: str):
return link.startswith("https://soundcloud.com/")
def link_validator(link: str):
is_yt = link.startswith("https://www.youtube.com/")
is_sc = is_soundcloud(link)
return is_yt or is_sc
# main
def main():
ssl._create_default_https_context = ssl._create_stdlib_context
print("Media Downloader v1.1 - By Chocomint\n")
# check ffmpeg
having_ffmpeg = True
try:
subprocess.run("ffmpeg", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except:
having_ffmpeg = False
warning("You don't have ffmpeg. You cannot fix header for YouTube media.")
if not inquirer.confirm("Do you want to continue?").execute():
return
print()
link = inquirer.text(
"media link:",
validate=link_validator,
invalid_message="Only support Youtube and SoundCloud",
amark="✓"
).execute()
if is_yt_video(link):
try:
video = YouTube(link)
except:
error("Invalid YouTube video")
return
youtube.single_video_downloader(video, having_ffmpeg)
elif is_yt_playlist(link):
try:
playlist = Playlist(link)
except:
error("Invalid YouTube playlist")
return
youtube.playlist_downloader(playlist, having_ffmpeg)
elif is_soundcloud(link):
block_print_error()
try:
with spinner("Resolving SoundCloud...") as bar:
x = soundcloud.API.resolve(link)
bar.title = "Resolving SoundCloud - Done."
except:
error("Invalid SoundCloud link")
bar.title = "Resolved error."
enable_print_error()
return
enable_print_error()
soundcloud.downloader(x)
else:
error("Invalid link")
return
if __name__ == "__main__":
main()