Skip to content

Instantly share code, notes, and snippets.

@twelvedogs
Last active February 6, 2025 20:06
Show Gist options
  • Save twelvedogs/0a4e07139033005ea3e55e61d5735e90 to your computer and use it in GitHub Desktop.
Save twelvedogs/0a4e07139033005ea3e55e61d5735e90 to your computer and use it in GitHub Desktop.
def do_download(url, addedBy):
isPlaylist = url.find('&list=')
if(isPlaylist > -1):
url = url[0:isPlaylist]
# TODO: need to catch malformed url
# TODO: check if folder exists probably
ydl = youtube_dl.YoutubeDL({'outtmpl': os.path.join(cfg.download_path, '%(title)s - %(id)s.%(ext)s'),
'format': 'bestvideo+bestaudio/best',
'getfilename': True,
'keep': True,
'logger': logging
# , 'restrictfilenames': True # makes it too hard to guess file name for now
})
# ydl.add_progress_hook(ydlhook) # add hook function to update with progress
with ydl:
result = ydl.extract_info(
url
# ,download=False # We just want to extract the info
)
if 'entries' in result:
# Can be a playlist or a list of videos
# not doing playlists yet
# video = result['entries'][0]
return {'result': False}
else:
# Just a video
youtubeResponse = result
full_path = ydl.prepare_filename(youtubeResponse)
# full_filename = ntpath.basename(full_path)
path_name, ext = get_extension(full_path)
found_file = False
# test if we had to merge the files into an mkv
try:
logging.info('looking for downloaded video at \"%s\" ', full_path)
if Path(full_path).is_file():
found_file = True
logging.info('found file \"%s\" using youtube-dl filename', full_path)
else:
logging.info('couldn\'t find \"%s\"', full_path)
if Path(path_name + '.mkv').is_file():
found_file = True
logging.info('video found at ' + path_name + '.mkv')
ext = '.mkv'
else:
logging.info('couldn\'t find ' + path_name + '.mkv')
except Exception as err:
logging.error('failed to determine path_name error:\n %s', str(err))
if(not found_file):
print('couldn\'t find file ' + path_name)
return False
print('filename should be: ' + path_name + ext)
# todo: this contains a lot of bullshit like [Official Video] maybe have a list of banned phrases, maybe just regex out everything between () or [] or ""
# possibly get title from one of those lookup tools like is used in kodi
title = youtubeResponse['title']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment