forked from greed2411/Flask-YouTube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathydl.py
103 lines (76 loc) · 2.8 KB
/
ydl.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
import subprocess
import requests
from bs4 import BeautifulSoup
from fetcher import id_generator
"""
YouTube Downloader used to download the best quality
audio,
video,
Playlist of audio files,
Playlist of video files
from a URL belonging to https://www.youtube.com/
Started working on : 21-06-2017
"""
def fetch_name(url):
""" To get the title of the YouTube Page """
youtube_page = requests.get(url)
bsObj = BeautifulSoup(youtube_page.text, "html.parser")
return bsObj.title.text
def verify(url):
"""
To check whether `url` belongs to YouTube, if yes returns True
else False
"""
if "www.youtube.com" in url or "youtu.be" in url:
return True
return False
def get_media(url, choice):
"""
Uses `choice`, to download the required content from `url`
"""
id_generated = id_generator()
try:
if url == "":
pass
else:
if choice == 1:
subprocess.call(
'youtube-dl -f 251 -o "/tmp/{id_generated}.%(ext)s" -q --no-playlist --extract-audio --audio-format mp3 --no-warnings "{url}"'.format(
id_generated=id_generated, url=url
),
shell=True,
)
return id_generated
elif choice == 2:
subprocess.call(
'youtube-dl -f 22 -o "/tmp/{id_generated}.%(ext)s" -q --no-playlist --no-warnings "{url}"'.format(
id_generated=id_generated, url=url
),
shell=True,
)
return id_generated
elif choice == 3:
subprocess.call(
'youtube-dl -i -o "/tmp/{id_generated}/%(playlist_index)s.%(title)s.%(ext)s" --yes-playlist --extract-audio --audio-format mp3 --no-warnings "{url}"'.format(
id_generated=id_generated, url=url
),
shell=True,
)
return id_generated
elif choice == 4:
return id_generated
subprocess.call(
'youtube-dl -i -o "/tmp/{id_generated}/%(playlist_index)s.%(title)s.%(ext)s" --yes-playlist --newline --no-warnings "{url}"'.format(
id_generated=id_generated, url=url
),
shell=True,
)
except Exception as e: # for any other unknown errors, used it to get the other exceptions mentioned above, while debugging used to print `e`
return e
if __name__ == "___main__":
print("This is a borrowed script")
else:
url = ""
choice = 1
verify(url)
get_media(url, choice)