ytdl-org / youtube-dl

Command-line program to download videos from YouTube.com and other video sites
http://ytdl-org.github.io/youtube-dl/
The Unlicense
132.31k stars 10.03k forks source link

How can i check if the url is a valid url before downloading ? #11649

Closed deangelo200 closed 7 years ago

deangelo200 commented 7 years ago

How can i check if the url is a valid url before downloading ?

yan12125 commented 7 years ago

Use self._is_valid_url()

deangelo200 commented 7 years ago

Where would i have to use ? completely lost

yan12125 commented 7 years ago

Are you writing extractors or just using youtube-dl as a CLI program?

deangelo200 commented 7 years ago

Writing an extractor ` with youtube_dl.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(url, download=False) video_id = info_dict.get("id", None) video_title = info_dict.get('title', None) video_thumb = info_dict.get('thumbnail',None) context = { 'form': form, 'title':video_title, 'id': video_id, 'thumbnails':video_thumb

                }`
yan12125 commented 7 years ago

Well, at this level _is_valid_url is not available. Need other approaches. What's your scenario?

deangelo200 commented 7 years ago

I want to add a feature to my website to let persons download songs but if the person entered an invalid url i want to run some functions

yan12125 commented 7 years ago

I guess I've got you. Just catch ExtractorError and analyze it

deangelo200 commented 7 years ago

How to do this ? Can you give me a short example please

yan12125 commented 7 years ago

Like this. Do whatever you want to e.

from youtube_dl.utils import ExtractorError

try:
    info_dict = ydl.extract_info(url, download=False)
except ExtractorError as e:
    print(e)
deangelo200 commented 7 years ago

thanks. if you dont mind can you hit me up a quick email deangeloalmeus@gmail.com

deangelo200 commented 7 years ago

image Hey am getting this error instead of getting this error i want to send the user to a page

Hrxn commented 7 years ago

How about using Regular Expressions to check? Pretty much the standard way to validate form input.

deangelo200 commented 7 years ago

I was thinking the same thing but i want to send the users to an error page if their is an error in the url.

yan12125 commented 7 years ago

Oops I forgot DownloadError wraps ExtractorError. Replace ExtractorError in the previous code with DownloadError

deangelo200 commented 7 years ago

image Are you familiar with Django

deangelo200 commented 7 years ago

Dont worry i got it to work thanks guys :)