YouTube Mp3 Downloader

FREEMIUM
(Ким) Vijay Prajapati | Оновлено 22 days ago | 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
10 months ago

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 10 months ago

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 10 months ago

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 10 months ago

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).

Приєднуйтесь до обговорення — додайте повідомлення нижче:

Вхід / Реєстрація, щоб публікувати нові повідомлення