YouTube Mp3 Downloader

부분 유료
분류별 Vijay Prajapati | 업데이트됨 il y a 22 jours | Video, Images
인기

9.7 / 10

지연 시간

15,679ms

서비스 수준

89%

Health Check

N/A

모든 토론으로 돌아가기

How to handle malformed URLs or other errors during fetch

Rapid account: 325234234
325234234
il y a 10 mois

Hey,

thanks for the API. Works great! Unless it doesn’t work. For example if the input isn’t a proper YouTube url or something else is wrong. Is there any designated way to handle this? Do we need to check the validity of the user input on the client side? Or can we post a request to the API and get a proper error code back in time? Right now it times out after 15 seconds or something.

Cheers,
Tino

Rapid account: 420 Vijay 47
420vijay47 Commented il y a 10 mois

Hi, you can extract the video ID (id) using JS in the frontend (as you explained), and then, make the request to the API like this:

var url = “https://www.youtube.com/watch?v=”+id;

$.ajax({
url: ‘/YTtomp3_api_endpoint/’,
type: ‘get’,
dataType: ‘JSON’,
data: {url: url},
success: function(response){
//action after success
}
}

})
Rapid account: 325234234
325234234 Commented il y a 10 mois

Alright. Any plans to implement some sort of early error code response when the API isn’t able to download the video? Right now I’m using this to check for validity on the client side:

async function videoExists(url) {
      //check if the url contains a valid YouTube video ID
      const regEx = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/

      //if url contains a proper video ID, match returns an array with the ID at position 1
      if(url.match(regEx)) {
        const id = url.match(regEx)[1]

        //check if video exists
        const response = await fetch(`https://www.googleapis.com/youtube/v3/videos?part=id&id=${id}&key=${import.meta.env.VITE_GOOGLE_API_KEY}`)
        const data = await response.json()

        // 0 = no video = falsey, 1 = video = truthy
        return data.items.length  
      } else {        
        return false
      }    
}

The regex comes from this SO answer: https://stackoverflow.com/a/27728417 and supposedly supports 25 different formats of youtube urls. Certainly works, but would of course be nicer if I could outsource all my download related logic to the API 😃

Anyways, thanks for the reply and the api!

Rapid account: 420 Vijay 47
420vijay47 Commented il y a 10 mois

Hi, the API validates the Youtube URL & works for these formats:

https://www.youtube.com/watch?v=VnRsoPOwteI
https://www.youtube.com/watch?v=VnRsoPOwteI&list=RDVnRsoPOwteI&start_radio=1&ab_channel=ShivBhakti
https://youtu.be/eCnaaI8FKH4

Means, it suppports YT short links also. You have make sure that the reauest is made in this formats only. if user inputs a URL that diesn’t align with these or have whitespaces, you have to trim the spaces then make the API request. if the URL is altogether not related to YT, API might not reverse a response & run for indefinite time (for now, the TTL is 180 seconds or 5 Mins).

아래에 의견을 추가하고 토론에 참여하세요.

새 댓글을 게시하려면 로그인 / 가입