Skip to content

Instantly share code, notes, and snippets.

@zeankundev
Last active February 10, 2022 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeankundev/12dc3d5395d07444840c50e6bf6ecfa3 to your computer and use it in GitHub Desktop.
Save zeankundev/12dc3d5395d07444840c50e6bf6ecfa3 to your computer and use it in GitHub Desktop.
CLDownloader source code
from pytube import YouTube
import os
import json
import itertools
import threading
import time
import sys
from pytube.request import filesize
# define some methods
finished = False
previousprogress = 0
def load():
for c in itertools.cycle(['/', '|', '\\', '-', '/', '|', '\\', '-']):
if finished:
break
sys.stdout.write('\rFetching some metadata, please wait. ' + c)
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\rDone! \n')
def progress_function(chunk, file_handle, bytes_remaining):
global filesz
current = ((filesz - bytes_remaining)/filesz)
percent = ('{0:.1f}').format(current*100)
progress = int(50*current)
status = '█' * progress + '-' * (50 - progress)
sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar=status, percent=percent))
sys.stdout.flush()
print('''
________ ____ __ __
/ ____/ / / __ \____ _ ______ / /___ ____ _____/ /__ _____
/ / / / / / / / __ \ | /| / / __ \/ / __ \/ __ `/ __ / _ \/ ___/
/ /___/ /___/ /_/ / /_/ / |/ |/ / / / / / /_/ / /_/ / /_/ / __/ /
\____/_____/_____/\____/|__/|__/_/ /_/_/\____/\__,_/\__,_/\___/_/
''')
print('CLDownloader. The most convenient YT downloader for devs on a CLI')
print('(c) 2022 zeankun.dev')
print('Opening downloadconfig.json, please wait...')
try:
with open(f"downloadconfig.json", encoding='utf8') as data:
config = json.load(data)
dir = config["saveDir"]
print('Opened file downloadconfig.json')
except FileNotFoundError:
dir = input('Please input full address e.g C:\\Users\\USER\\. Your video will be saved on the CLDownloader folder (Leave this blank for the default directory. : ')
if dir !='':
newdir = dir + 'CLDownloader'
dir = newdir
else:
newdir = os.path.join(os.path.expandvars("%userprofile%"),"CLDownloader")
dir = newdir
config = {
"saveDir": newdir
}
with open('downloadconfig.json', 'w') as data:
json.dump(config, data, indent=2)
print('Successfully created config.json.')
while True:
vLink = input('Target YouTube link to download: ')
t = threading.Thread(target=load)
t.start()
yt = YouTube(vLink, on_progress_callback=progress_function)
time.sleep(2)
finished = True
print('\nMetadata fetched!')
print('Title: ', yt.title)
print('Views: ', yt.views)
print('Length: ', yt.length)
print('Rating: ', yt.rating)
print('Getting high resolution possible...')
try:
ys = yt.streams.get_highest_resolution()
except AttributeError:
print('CLDownloader encountered an internal error! Sorry for the inconvenience! The process will now be terminated.')
quit()
time.sleep(1)
filesz = ys.filesize
print(f'Video has a high resolution and will be saved to {dir}, so please look for it after download!')
conf = input('Are you sure to download this? This will take around ' + str(round(ys.filesize / (1024 * 1024 ))) + 'MiB. [y/n]: ')
if conf == 'y':
print('Found highest resolution, downloading...')
ys.download(dir)
print('Success! Be sure to check your directory ' + dir)
retry = input('Do you want to download more videos?')
if retry == 'y':
continue
else:
print('See you next time!!!!!!!')
time.sleep(2)
quit()
@zeankundev
Copy link
Author

Please note!!!!!!! Some of the libraries on the Python script are required to be installed to be working.

  • PyTube
  • IterTools

@zeankundev
Copy link
Author

Changelog:

  • Added downloadconfig json file for easier directory storage without having to use the default directory but kept the CLDownloader folder

@bpsagar
Copy link

bpsagar commented Dec 2, 2021

You could add the dependent libraries to the gist in a requirements.txt file 🙂

@zeankundev
Copy link
Author

Ok

@zeankundev
Copy link
Author

Changelog:

  • Added an error message when CLDownloader fails to fetch the video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment