forked from mohit23x/youtube-playlist-downloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ytdown.py
147 lines (99 loc) · 3.12 KB
/
ytdown.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
#!python3
# Usage -
# 1. open cmd
# 2. cd to the folder where these files are present
# 3. type - python ytdown.py
# the script will start working
import os
import subprocess
from pytube import YouTube
import random
import requests
import re
import string
#imp functions
def foldertitle(url):
try:
res = requests.get(url)
except:
print('no internet')
return False
plain_text = res.text
if 'list=' in url:
eq = url.rfind('=') + 1
cPL = url[eq:]
else:
print('Incorrect attempt.')
return False
return cPL
def link_snatcher(url):
our_links = []
try:
res = requests.get(url)
except:
print('no internet')
return False
plain_text = res.text
if 'list=' in url:
eq = url.rfind('=') + 1
cPL = url[eq:]
else:
print('Incorrect Playlist.')
return False
tmp_mat = re.compile(r'watch\?v=\S+?list=' + cPL)
mat = re.findall(tmp_mat, plain_text)
for m in mat:
new_m = m.replace('&', '&')
work_m = 'https://youtube.com/' + new_m
# print(work_m)
if work_m not in our_links:
our_links.append(work_m)
return our_links
BASE_DIR = os.getcwd()
print('WELCOME TO PLAYLIST DOWNLOADER DEVELOPED BY - www.github.com/mohit0101')
url = str(input("\nspecify you playlist url\n"))
print('\nCHOOSE ANY ONE - TYPE 360P OR 720P\n')
user_res = str(input()).lower()
print('...You choosed ' + user_res + ' resolution\n.')
our_links = link_snatcher(url)
os.chdir(BASE_DIR)
new_folder_name = foldertitle(url)
print(new_folder_name[:7])
try:
os.mkdir(new_folder_name[:7])
except:
print('folder already exists')
os.chdir(new_folder_name[:7])
SAVEPATH = os.getcwd()
print(f'\n files will be saved to {SAVEPATH}')
x=[]
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
pathh = os.path.join(root, name)
if os.path.getsize(pathh) < 1:
os.remove(pathh)
else:
x.append(str(name))
print('\nconnecting . . .\n')
print()
for link in our_links:
try:
yt = YouTube(link)
main_title = yt.title
main_title = main_title + '.mp4'
main_title = main_title.replace('|', '')
except:
print('connection problem..unable to fetch video info')
break
if main_title not in x:
if user_res == '360p' or user_res == '720p':
vid = yt.streams.filter(progressive=True, file_extension='mp4', res=user_res).first()
print('Downloading. . . ' + vid.default_filename + ' and its file size -> ' + str(round(vid.filesize / (1024 * 1024), 2)) + ' MB.')
vid.download(SAVEPATH)
print('Video Downloaded')
else:
print('something is wrong.. please rerun the script')
else:
print(f'\n skipping "{main_title}" video \n')
print(' downloading finished')
print(f'\n all your videos are saved at --> {SAVEPATH}')