Async function to download video
This commit is contained in:
		@@ -1,7 +1,12 @@
 | 
			
		||||
// https://www.youtube.com/watch?v=HzqnQThFSX8&list=RDHzqnQThFSX8&start_radio=1
 | 
			
		||||
function ytdl() {
 | 
			
		||||
    var ytid = "";
 | 
			
		||||
    var url = document.getElementById("url").value;
 | 
			
		||||
    const ytid = get_ytid(url);
 | 
			
		||||
    add_db(ytid);
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function get_ytid(url) {
 | 
			
		||||
    if (url === "") {
 | 
			
		||||
        alert("Please enter a URL");
 | 
			
		||||
        return;
 | 
			
		||||
@@ -23,29 +28,24 @@ function ytdl() {
 | 
			
		||||
    else {
 | 
			
		||||
        alert("URL is not compliant with this website - I am kinda sorry(Just kidding)");
 | 
			
		||||
    }
 | 
			
		||||
    add_db(ytid);
 | 
			
		||||
    return;
 | 
			
		||||
    return ytid;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function add_db(ytid) {
 | 
			
		||||
    var base_url = "http://localhost:8000/api/get/";
 | 
			
		||||
    var url = base_url.concat(ytid);
 | 
			
		||||
    console.log(url);
 | 
			
		||||
    send_get(url);
 | 
			
		||||
        //fetch(url).then(res => res.json()).then(data => console.log(data));
 | 
			
		||||
        //.catch(res => console.log("Failed: " + res))
 | 
			
		||||
    //return;
 | 
			
		||||
    let send_response = add_song(url);
 | 
			
		||||
    console.log("Send response:", send_response)
 | 
			
		||||
}
 | 
			
		||||
function send_get(url){
 | 
			
		||||
    fetch(url)
 | 
			
		||||
        .then(response =>  {   // indicates whether the response is successful (status code 200-299) or not
 | 
			
		||||
            if (!response.ok) {
 | 
			
		||||
                throw new Error(`Request failed with status ${reponse.status}`)
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                console.log(response.body)
 | 
			
		||||
            }
 | 
			
		||||
        return response.json()
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
async function add_song(url){
 | 
			
		||||
    var requestOptions = {
 | 
			
		||||
        method: 'GET',
 | 
			
		||||
        redirect: 'follow'
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let response = await fetch(url, requestOptions);
 | 
			
		||||
    let data = await response.text();
 | 
			
		||||
    console.log(data)
 | 
			
		||||
    return data;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user