This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transcript.js
65 lines (58 loc) · 1.84 KB
/
transcript.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
const fetch = require('node-fetch');
require('dotenv').config();
async function createTranscript(recordingUrl, fileName) {
const options = {
file_url: recordingUrl,
language: 'en',
name: fileName,
keywords: 'NAS, Pensacola, galley, menu'
};
return await fetch('https://api.sonix.ai/v1/media', {
method: 'POST',
body: JSON.stringify(options),
headers: {
'Authorization': `Bearer ${process.env.SONIX_API_KEY}`,
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(json => json.id);
}
// Loops continuously every 7.5 seconds until transcript is completed
async function transcriptReady(transcript_id) {
let sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
await checkStatus();
async function checkStatus() {
let status;
await fetch(`https://api.sonix.ai/v1/media/${transcript_id}`, {
method: 'GET',
cache: 'no-store',
headers: {
'Authorization': `Bearer ${process.env.SONIX_API_KEY}`,
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(json => status = json.status);
if (status !== "completed") {
await sleep(7500);
await checkStatus();
}
}
}
// Turns the audio file into a transcription
async function getTranscript(transcript_id) {
return await fetch(`https://api.sonix.ai/v1/media/${transcript_id}/transcript`, {
method: 'GET',
headers: { 'Authorization': `Bearer ${process.env.SONIX_API_KEY}` }
})
.then(res => res.text())
// Format the transcription down to just the text
.then(text => text.split("\n").slice(2).join("\n"))
// Line break after every sentence for readability
.then(text => text.split(". ").join(".\n"))
.catch(err => console.error(err));
}
exports.create = createTranscript;
exports.isReady = transcriptReady;
exports.get = getTranscript;