Async function to download video

This commit is contained in:
janic 2022-06-09 00:45:53 +02:00
parent ea28ac1c37
commit c03745007a

View File

@ -1,7 +1,12 @@
// https://www.youtube.com/watch?v=HzqnQThFSX8&list=RDHzqnQThFSX8&start_radio=1 // https://www.youtube.com/watch?v=HzqnQThFSX8&list=RDHzqnQThFSX8&start_radio=1
function ytdl() { function ytdl() {
var ytid = "";
var url = document.getElementById("url").value; var url = document.getElementById("url").value;
const ytid = get_ytid(url);
add_db(ytid);
return;
}
function get_ytid(url) {
if (url === "") { if (url === "") {
alert("Please enter a URL"); alert("Please enter a URL");
return; return;
@ -23,29 +28,24 @@ function ytdl() {
else { else {
alert("URL is not compliant with this website - I am kinda sorry(Just kidding)"); alert("URL is not compliant with this website - I am kinda sorry(Just kidding)");
} }
add_db(ytid); return ytid;
return;
} }
function add_db(ytid) { function add_db(ytid) {
var base_url = "http://localhost:8000/api/get/";
var url = base_url.concat(ytid); var url = base_url.concat(ytid);
console.log(url); console.log(url);
send_get(url); let send_response = add_song(url);
//fetch(url).then(res => res.json()).then(data => console.log(data)); console.log("Send response:", send_response)
//.catch(res => console.log("Failed: " + res))
//return;
} }
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;
} }