1

I am developing a project, where user submits a URL. I need to check if that URL is valid url , to download data from youtube-dl supported sites.

Please help.

Davit Tovmasyan
  • 3,238
  • 2
  • 20
  • 36
My Projects
  • 49
  • 1
  • 8
  • Does this answer your question? [Validating URLs in Python](https://stackoverflow.com/questions/22238090/validating-urls-in-python) – Davit Tovmasyan Apr 27 '20 at 18:52

2 Answers2

3

Try this function:

import youtube-dl

url = 'type your url here'

def is_supported(url):
    extractors = youtube_dl.extractor.gen_extractors()
    for e in extractors:
        if e.suitable(url) and e.IE_NAME != 'generic':
            return True
    return False

print (is_supported(url))

Remember: you need to import youtube_dl

m8factorial
  • 188
  • 6
  • I update my code above. You need to execute this in a python file. Search about how do it. There's lots of tutorials. Ex: https://realpython.com/run-python-scripts/ Good luck! – m8factorial Apr 29 '20 at 07:45
  • As far as I understand I can pass custom list of extactors to limit services? https://stackoverflow.com/q/62203971/7415288 – Marat Mkhitaryan Jun 04 '20 at 21:14
0

Here's an example code in Python using the youtube-dl library to check if a URL is for a video or not:

import youtube_dl

def check_url_video(url):
    ydl = youtube_dl.YoutubeDL({'quiet': True})
    try:
        info = ydl.extract_info(url, download=False)
        return True
    except Exception:
        return False

url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
if check_url_video(url):
    print("The URL is for a video.")
else:
    print("The URL is not for a video.")

or

import youtube_dl

ydl = youtube_dl.YoutubeDL()

try:
    info = ydl.extract_info("https://www.youtube.com/watch?v=dQw4w9WgXcQ", download=False)
    print("The URL is for a video.")
except youtube_dl.DownloadError:
    print("The URL is not for a video.")

This code uses the youtube_dl library to try to extract information from the URL using ydl.extract_info(). If the extraction is successful, the URL is for a video. If it fails, the URL is not for a video.