-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (142 loc) · 4.11 KB
/
index.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
import {
addDailyRecording,
addDownloadLink,
addUploadedProject,
updateProjectStatus,
} from './dom.js';
window.addEventListener('DOMContentLoaded', () => {
setupUploadForm();
setupRecordingsFetchBtn();
});
const apiURL = 'http://127.0.0.1:5000';
/**
* Configures the manual file upload form
*/
function setupUploadForm() {
const form = document.getElementById('uploadForm');
form.onsubmit = (ev) => {
ev.preventDefault();
const video = document.getElementById('videoFile').files[0];
const formData = new FormData();
formData.append('file', video);
// Upload the selected file to the server. This will begin processing the file
// to remove filler words.
fetch(`${apiURL}/upload`, { method: 'POST', body: formData })
.then((res) => {
if (res.ok === false) {
throw Error(`upload request failed: ${res.status}`);
}
return res.json();
})
.then((data) => {
// Retrieve project ID from server response and begin polling status.
const projectID = data.project_id;
// Update the DOM to show the new project
addUploadedProject(projectID, data.name);
pollStatus(projectID);
})
.catch((e) => {
console.error('Failed to process uploaded video:', e);
});
};
}
/**
* Configures fetching Daily recordings
*/
function setupRecordingsFetchBtn() {
const btn = document.getElementById('fetchRecordings');
btn.onclick = () => {
// Fetch all recordings from the server
fetch(`${apiURL}/recordings`, { method: 'GET' })
.then((res) => {
if (res.ok === false) {
throw Error(`upload request failed: ${res.status}`);
}
return res.json();
})
.then((data) => {
// Add each fetched recording to the recordings table in the DOM
const { recordings } = data;
for (let i = 0; i < recordings.length; i += 1) {
const rec = recordings[i];
addDailyRecording(
rec.id,
rec.room_name,
rec.timestamp,
processDailyRecording,
);
}
})
.catch((e) => {
console.error('Failed to process uploaded video:', e);
});
};
}
/**
* Processes a specified Daily recording to remove filler words
* @param recordingID
*/
function processDailyRecording(recordingID) {
// Begin processing Daily recording to remove filler words
fetch(`${apiURL}/process_recording/${recordingID}`, {
method: 'POST',
})
.then((res) => {
if (res.ok === false) {
throw Error(`Recording processing failed: ${res.status}`);
}
return res.json();
})
.then((data) => {
const projectID = data.project_id;
// Begin polling status
pollStatus(projectID, recordingID);
})
.catch((e) => {
console.error('Failed to process Daily recording:', e);
});
}
/**
* Check the status of a processing project
* @param projectID
* @param isRecording
*/
function pollStatus(projectID, recordingID = null) {
setTimeout(() => {
// Fetch status of the given project from the server
fetch(`${apiURL}/projects/${projectID}`)
.then((res) => {
if (!res.ok) {
throw Error(`status request failed: ${res.status}`);
}
return res.json();
})
.then((data) => {
const { status } = data;
const { info } = data;
// Update status in the DOM
updateProjectStatus(projectID, status, info, recordingID);
switch (status) {
case 'In progress':
pollStatus(projectID, recordingID);
break;
case 'Succeeded':
addDownloadLink(
projectID,
`${apiURL}/projects/${projectID}/download`,
recordingID,
);
break;
case 'Failed':
break;
default:
console.warn('unexpected status:', status);
pollStatus(projectID, recordingID);
}
})
.catch((err) => {
console.error('failed to check project status: ', err);
pollStatus(projectID, recordingID);
});
}, 2000);
}