Reputation: 49
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.
Upvotes: 1
Views: 3703
Reputation: 1
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.
Upvotes: 0
Reputation: 188
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
Upvotes: 3