-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
180 lines (151 loc) · 5.75 KB
/
scripts.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Initialize Bootstrap dropdown
var dropdownElementList = [].slice.call(document.querySelectorAll('.dropdown-toggle'));
var dropdownList = dropdownElementList.map(function (dropdownToggleEl) {
return new bootstrap.Dropdown(dropdownToggleEl);
});
// Elements selection
const form = document.getElementById('download-form');
const resultDiv = document.getElementById('result');
const spinner = document.getElementsByClassName('spinner')[0];
const videoDetailsDiv = document.getElementById('video-details');
const videoDetailsTable = document.getElementById('video-details-table');
const downloadButton = document.getElementById('download-btn');
// Handle form submission
form.addEventListener('submit', async (event) => {
event.preventDefault();
const urlInput = document.getElementById('url-input');
const url = urlInput.value.trim();
clearUI(); // Clear the UI for the new request
if (!url) {
resultDiv.innerHTML = '<p>Please enter a valid YouTube URL.</p>';
return;
}
try {
await fetchVideoDetails(url);
const downloadToken = await initiateDownload(url);
await pollDownload(downloadToken);
} catch (error) {
console.error('Error:', error);
displayError(error.message || 'An unexpected error occurred');
}
});
// Fetch video details and update UI
async function fetchVideoDetails(url) {
spinner.style.display = 'flex';
const response = await fetch('/info?url=' + encodeURIComponent(url));
if (!response.ok) throw new Error('Failed to fetch video details');
const videoDetails = await response.json();
displayVideoDetails(videoDetails.title, videoDetails.duration);
}
// Initiate download and return token
async function initiateDownload(url) {
const response = await fetch('/download?url=' + encodeURIComponent(url));
if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
const data = await response.json();
return data.token;
}
// Poll the server for download progress and handle download
async function pollDownload(downloadToken) {
resultDiv.innerHTML = '<p>Downloading...</p>';
async function checkDownload(downloadToken) {
const response = await fetch('/progress?token=' + encodeURIComponent(downloadToken), {
method: 'GET', // or 'POST' depending on your server setup
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
credentials: 'same-origin' // or 'include' if requests are cross-origin
});
if (response.status === 202) {
setTimeout(checkDownload, 5000, downloadToken); // Make sure to pass the downloadToken again
} else if (response.status === 200) {
console.log('Download ready.');
// Further actions here
await downloadFile(downloadToken);
} else {
throw new Error(`Failed to fetch with status: ${response.status}`);
}
}
checkDownload(downloadToken);
}
async function downloadFile(downloadToken) {
const response = await fetch('/downloadFile?token=' + encodeURIComponent(downloadToken));
spinner.style.display = 'none'; // Assuming 'spinner' is a pre-defined element for loading indication
if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
// Read chunks from the response stream
const reader = response.body.getReader();
let chunks = [];
let totalLength = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
totalLength += value.length;
}
} catch (error) {
console.error('Error while reading the stream:', error);
throw new Error('Failed to read the stream.');
}
// Combine the chunks into a single Uint8Array
let uint8Array = new Uint8Array(totalLength);
let position = 0;
for (let chunk of chunks) {
uint8Array.set(chunk, position);
position += chunk.length;
}
// Create a blob from the Uint8Array
const blob = new Blob([uint8Array], { type: 'application/octet-stream' });
createDownloadLink(blob);
}
// Create and handle download link
function createDownloadLink(blob) {
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
console.log(downloadLink.href)
console.log("downloadLink.href = URL.createObjectURL(blob);")
downloadLink.download = 'downloadedFile'+ Date.now()+".mp3"; // Set your desired file name
const downloadButton = document.getElementById('download-btn');
downloadButton.style.display = 'inline-block';
downloadButton.onclick = () => {
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
resultDiv.innerHTML = '<p>Download completed.</p>';
};
resultDiv.innerHTML = '<p>Download is ready.</p>';
spinner.style.display = 'none';
}
// Display video details on the UI
function displayVideoDetails(title, duration) {
videoDetailsDiv.style.display = 'flex';
const newRow = document.createElement('tr');
newRow.innerHTML = `<td>${title}</td><td>${duration}</td>`;
videoDetailsTable.appendChild(newRow);
}
// Display error messages
function displayError(message) {
resultDiv.innerHTML = `<p>Error: ${message}</p>`;
spinner.style.display = 'none';
}
// Clear UI elements
function clearUI() {
spinner.style.display = 'none';
downloadButton.style.display = 'none';
resultDiv.innerHTML = "";
videoDetailsTable.innerHTML = "";
videoDetailsDiv.style.display = 'none';
}
// Read URL parameters and auto-submit form if applicable
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (urlParams.has('v')) {
document.getElementById('url-input').value = `https://www.youtube.com/watch?v=${urlParams.get('v')}`;
form.dispatchEvent(new Event('submit'));
}