-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathYouTube_playlist_downloader.py
156 lines (123 loc) · 4.22 KB
/
YouTube_playlist_downloader.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
import requests
import urllib.request
from bs4 import BeautifulSoup
import progressbar
from time import sleep
def keepvid_download_link(start=1 , end=300):
fr = open('links.txt', 'r')
links = fr.read()
links = links.split("\n")
length=len(links)
print("Number of videos in the playlist "+str(length-1))
fkeep = open('keepvid.txt', 'w')
n=1
print("Getting download links...")
sleep(0.1)
bar = progressbar.ProgressBar(maxval=length-1, \
widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
for link in links:
if n>=start and n<=end:
if link != "":
keepvid_url = 'http://keepvid.com/?url=' + link
href = download_link(keepvid_url)
bar.update(n)
fkeep.write(href + "\n")
n+=1
if n>end:
break
bar.finish()
sleep(1)
fkeep.close()
fr.close()
def downloading( num, user_input = 0):
print('Downloading Videos....')
fr_title = open('vid_title.txt', 'r')
titles = fr_title.read()
titles = titles.split("\n")
fr_link = open('keepvid.txt', 'r')
links = fr_link.read()
links = links.split("\n")
max_index = len(links)-1
if user_input == 0:
user_input = num
for link in links:
if num < (max_index+1):
if link[num-1] is not "no video found":
download_vid(links[num - 1], titles[user_input - 1])
print(titles[user_input-1] + " -- ["+str(num)+"/"+str(max_index)+"] Downloaded")
else:
print(titles[user_input-1] + " --Download Failed")
num += 1
user_input+=1
fr_link.close()
fr_title.close()
print("Download Complete.")
def download_vid(link, title):
name =str(title) + ".mp4"
urllib.request.urlretrieve(link,name)
def download_link(url):
source = requests.get(url)
plain_text = source.text
soup = BeautifulSoup(plain_text, "lxml")
a = 0
c=0
k=0
for row in soup.findAll('td'):
qulity = str(row.string)
a+=1;
if qulity == '(Max 720p)':
c=((a-1)/4)+1
for link in soup.findAll('a', {'class': 'btn-outline'}):
k+=1
if k == c:
href = link.get('href')
return href
break
elif qulity == '480p':
c = ((a - 1) / 4) + 1
for link in soup.findAll('a', {'class': 'btn-outline'}):
k += 1
# print(k)
if k == c:
href = link.get('href')
return href
break
if k > 0 :
break
return 'no video found'
def download_link_youtube(url):
source = requests.get(url)
plain_text = source.text
soup = BeautifulSoup(plain_text, "lxml")
fw = open('links.txt', 'w')
for link in soup.findAll('a', {'class': 'playlist-video'}):
href = 'https://www.youtube.com' + link.get('href')
fw.write(href+"\n")
fw.close()
fw1 = open('vid_title.txt', 'w')
index=1
for title in soup.findAll('h4', {'class': 'yt-ui-ellipsis'}):
title_vid = title.string
title_vid=str(index)+"."+title_vid.strip()
fw1.write(title_vid + "\n")
index+=1
fw1.close()
print("Select your option : \nEnter 1 : To download playlist (Complete) form url\nEnter 2 : To resume your download.\nEnter 3 : For custom download")
option = input('Enter your option : ')
if option is '1':
url = input('Enter URL of any video of Youtube Playlist : ')
download_link_youtube(url)
keepvid_download_link()
starting_index = 1
downloading(starting_index)
if option is '2':
starting_index =int( input('Enter the video number to start download from : '))
downloading(starting_index)
if option is '3':
url = input('Enter URL of any video of Youtube Playlist : ')
starting_index = int(input('Enter the video number to start download from : '))
ending_index = int(input('Enter the video number to end at : '))
download_link_youtube(url)
keepvid_download_link(starting_index, ending_index)
downloading(1,starting_index)