tubesync/app/sync/youtube.py

40 lines
1.1 KiB
Python
Raw Normal View History

'''
Wrapper for the youtube-dl library. Used so if there are any library interface
updates we only need to udpate them in one place.
'''
from django.conf import settings
import youtube_dl
_defaults = getattr(settings, 'YOUTUBE_DEFAULTS', {})
class YouTubeError(youtube_dl.utils.DownloadError):
'''
Generic wrapped error for all errors that could be raised by youtube-dl.
'''
pass
2020-12-06 01:22:16 +00:00
def get_media_info(url):
'''
Extracts information from a YouTube URL and returns it as a dict. For a channel
or playlist this returns a dict of all the videos on the channel or playlist
as well as associated metadata.
'''
2020-12-06 01:22:16 +00:00
opts = _defaults.update({
'skip_download': True,
'forcejson': True,
'simulate': True,
})
response = {}
with youtube_dl.YoutubeDL(opts) as y:
try:
response = y.extract_info(url, download=False)
except youtube_dl.utils.DownloadError as e:
raise YouTubeError(f'Failed to extract_info for "{url}": {e}') from e
return response