-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
36 lines (29 loc) · 1008 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export async function downloadYTDLP(url) {
const apiUrl = 'http://localhost:8000';
try {
const response = await fetch(apiUrl + '/download_yt_dlp', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ url })
});
console.log("utils 13", response);
if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
const blob = await response.blob();
const blobUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = blobUrl;
link.setAttribute('download', 'audio.mp3');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(blobUrl);
return blob;
} catch (error) {
console.error('Error:', error);
return 'An error occurred.';
}
}