Search code examples
pythonyoutube-dl

How to pass a list of allowed services to youtube-dl?


I want youtube-dl to be able to download content only from a list of allowed services. Like ['youtube','twitch'].

Also is there any way to set custom error messages?


Solution

  • You can monkeypatch the youtube_dl.extractor._ALL_CLASSES, so youtube_dl will not know any other extractors than you specified.

    import youtube_dl
    
    def get_list_of_extractors(extractor_modules):
        extractors = []
        for module in extractor_modules:
            list_of_extractors = [
                getattr(module, name) for name in dir(module) 
                if name.endswith('IE') and name != 'GenericIE' and name.find("Base") == -1
            ]
            extractors = extractors + list_of_extractors
        return extractors
    
    list_of_extractors = get_list_of_extractors([youtube_dl.extractor.youtube, youtube_dl.extractor.tiktok])
    youtube_dl.extractor._ALL_CLASSES = list_of_extractors
    
    ydl = youtube_dl.YoutubeDL({})
    
    ydl.extract_info(
        'https://www.youtube.com/watch?v=BaW_jenozKc',
        download=False # We do not need to download
    )
    
    # causes youtube_dl.utils.DownloadError, because instagram extractor is not in the list
    ydl.extract_info(
        'https://instagram.com/p/aye83DjauH',
        download=False # We do not need to download
    )