Skip to content

Commit dd165d1

Browse files
Modified as per windows
1 parent f982eb2 commit dd165d1

1 file changed

Lines changed: 36 additions & 19 deletions

File tree

Download playlist from cmd using python/playlist.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ def main():
131131
start = 0
132132
end = 0
133133
path = "playlist"
134+
driver = ""
135+
system = sys.platform
134136

135137
parser = argparse.ArgumentParser()
136138
parser.add_argument('-u', '--url', nargs='?',
@@ -142,7 +144,7 @@ def main():
142144
parser.add_argument('-e', '--end', nargs='?',
143145
help="End no. of playlist", type=int)
144146
parser.add_argument("-d", "--driver", type=str, default="phantomjs",
145-
help="which driver to use [option: phantomjs, firefox, chrome]")
147+
help="which driver to use [option: phantomjs, firefox, chrome]")
146148

147149
args = parser.parse_args()
148150

@@ -161,17 +163,30 @@ def main():
161163
try:
162164

163165
choice = args.driver
164-
driver = ""
165-
if choice == "firefox":
166-
binary = FirefoxBinary('firefox')
167-
driver = webdriver.Firefox(firefox_binary=binary)
168-
elif choice == "chrome":
169-
driver = webdriver.Chrome();
170-
elif choice == "phantomjs":
171-
driver = webdriver.PhantomJS()
172-
else:
173-
print("Invalid Choice");
174-
sys.exit(1);
166+
if system == "linux" or system == "linux2":
167+
if choice == "firefox":
168+
binary = FirefoxBinary('firefox')
169+
driver = webdriver.Firefox(firefox_binary=binary)
170+
elif choice == "chrome":
171+
driver = webdriver.Chrome()
172+
elif choice == "phantomjs":
173+
driver = webdriver.PhantomJS()
174+
else:
175+
print("Invalid Choice")
176+
sys.exit(1)
177+
178+
elif system == "win32":
179+
if choice == "firefox":
180+
binary = FirefoxBinary('firefox.exe')
181+
driver = webdriver.Firefox(firefox_binary=binary)
182+
elif choice == "chrome":
183+
driver = webdriver.Chrome('chromedriver.exe')
184+
elif choice == "phantomjs":
185+
driver = webdriver.PhantomJS(
186+
executable_path=r'phantomjs.exe')
187+
else:
188+
print("Invalid Choice")
189+
sys.exit(1)
175190

176191
driver.set_window_size(1120, 550)
177192

@@ -184,8 +199,8 @@ def main():
184199

185200
soup = BeautifulSoup(driver.page_source, 'html.parser')
186201
path = soup.find(
187-
"a", class_="yt-formatted-string").string
188-
202+
"a", class_="yt-formatted-string").string.replace('|', '-')
203+
print(path)
189204
_urls = soup.find_all(
190205
"ytd-playlist-video-renderer", class_="ytd-playlist-video-list-renderer")
191206

@@ -195,15 +210,17 @@ def main():
195210

196211
print("There are total of " + str(totalVideos) + " Videos in the playlist")
197212

198-
if not os.path.exists(r'"'+path+'"'):
199-
os.system('mkdir %s' % r'"'+path+'"')
213+
if not os.path.exists(r'"' + path + '"'):
214+
os.system('mkdir %s' % r'"' + path + '"')
215+
# path = os.path.abspath(path)
216+
# print(path)
200217

201218
for i in range(start, end):
202219

203220
_url = _urls[i]
204221

205222
_name = _url.find("h3").find("span").string.replace('\n', '').replace(
206-
' ', '')
223+
' ', '').replace('|', '-')
207224
_name += '.mp4'
208225

209226
_url = prefix + _url.find("a").get("href")
@@ -239,7 +256,7 @@ def main():
239256
"a", class_="link link-download subname ga_track_events download-icon")
240257
url_parse = click.get("href")
241258
print(str(i + 1) + "- Downloading " + _name)
242-
with open(path + "//" + _name, 'wb') as out_file:
259+
with open(path + "\\" + _name, 'wb') as out_file:
243260
with urlopen(url_parse) as fp:
244261
info = fp.info()
245262
if 'Content-Length' in info:
@@ -262,9 +279,9 @@ def main():
262279
print("\n")
263280
print("Successfully download " + _name)
264281
sys.stdout.write("\a")
282+
driver.quit()
265283
except Exception as e:
266284
print(e)
267-
driver.quit()
268285

269286

270287
if __name__ == '__main__':

0 commit comments

Comments
 (0)