-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.js
191 lines (171 loc) · 5.85 KB
/
functions.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
181
182
183
184
185
186
187
188
189
190
191
var jobID;
//var furl = "https://api.havenondemand.com/1/api/async/recognizespeech/v2?url=https%3A%2F%2Fwww.havenondemand.com%2Fsample-content%2Fvideos%2Fhpnext.mp4&language_model=en-us&apikey=690c5e3e-6517-45b2-b340-9eaa372d63e7";
// TEST EN LOCAL: 'https://api.havenondemand.com/1/api/async/recognizespeech/v2?file=@MacronGJ&language_model=en-us&apikey=690c5e3e-6517-45b2-b340-9eaa372d63e7'';
//'https://api.havenondemand.com/1/api/async/recognizespeech/v2?url=@MacronGJ&language_model=en-us&apikey=690c5e3e-6517-45b2-b340-9eaa372d63e7';
var APIKEY = "4bf6fe7e-fd9b-47c6-bdc6-38e3b70da60e";
var button = document.getElementById("submit");
var loadingGif = "<p id='status'>Loading....</p><img id='loading' src='loading.gif'>";
var jsonString = "";
var JSONFILE;
//tableau de mots
var mots = [];
function getData(){
var loc = document.getElementById("locale").value;
if (document.getElementById("url").value == ""){
getJobFile(loc);
}else {
var url = document.getElementById("url").value;
getJobIdUrl(url, loc);
}
}
// Recuperation du JobID -> URL
function getJobIdUrl(url, loc) {
document.getElementById("zone").innerHTML = loadingGif;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
jobID = this.responseText;
jobID = jobID.slice(16,jobID.length - 3);
console.log(jobID);
getJson();
}
};
xhttp.open("POST", "https://api.havenondemand.com/1/api/async/recognizespeech/v2?url="+url+"&language_model="+loc+"&apikey="+APIKEY, true);
xhttp.send();
}
// Recuperation du JSON
function getJson() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
reloadResponse(this.responseText);
document.getElementById("status").value = "Done!";
document.getElementById("loading").visibility = "hidden";
document.getElementById("zone").innerHTML = "";
console.log(this.responseText);
responseText(this.responseText);
JSONFILE = this.responseText;
}
};
var link = "https://api.havenondemand.com/1/job/result/" + jobID + "?apikey="+APIKEY;
xhttp.open("GET", link, true);
xhttp.send();
}
function reloadResponse(dumb){
console.log(dumb);
var i = 0;
//disparition et appararition des components
var myNode = document.getElementById("firstI");
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
document.getElementById("secondI").style.display = "inline";
//parsage JSON
var obj = JSON.parse(dumb, function (key, value) {
if (key == "text") {
jsonString = jsonString + value + " ";
mots[i] = value;
i = i + 1;
}
});
console.log(jsonString);
document.getElementById("message").value = jsonString;
}
function getJobFile(locale){
document.getElementById("zone").innerHTML = loadingGif;
var file_data = $('#fichier').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: 'https://api.havenondemand.com/1/api/async/recognizespeech/v2?apikey='+APIKEY+'&language_model='+locale,
type: 'POST',
data: form_data,
contentType: false,
processData: false,
success: function(responseText){
getJsonQ(responseText);
}
});
}
function getJsonQ(job){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("status").value = "Done!";
document.getElementById("loading").visibility = "hidden";
document.getElementById("zone").innerHTML = "";
console.log(this.responseText);
reloadResponse(this.responseText);
JSONFILE = this.responseText;
}
};
var link = "https://api.havenondemand.com/1/job/result/" + job["jobID"] + "?apikey="+APIKEY;
xhttp.open("GET", link, true);
xhttp.send();
}
function conditionalInput(){
if(document.getElementById("url").value==""){
document.getElementById("fichier").disabled=false;
}else{
document.getElementById("fichier").disabled=true;
}
if(document.getElementById("fichier").value==""){
document.getElementById("url").disabled=false;
}else{
document.getElementById("url").disabled=true;
}
}
function searchTimeS(outId, word){
var out = "";
var json = JSON.parse(JSONFILE);
for (var data in json.actions[0].result.items){
if(json.actions[0].result.items[data].text.toLowerCase() == word.toLowerCase()){
console.log(data);
var current = json.actions[0].result.items[data];
var precedent = "[Begin of File]";
if(data > 1){
precedent = json.actions[0].result.items[data-1];
}
var next = "[End of File]";
if(parseInt(data, 10) < json.actions[0].result.items.length){
next = json.actions[0].result.items[parseInt(data, 10) + 1];
}
var line = "<p onclick='jump("+current.start_time_offset+")'>"+precedent.text+" <b>"+current.text+ "</b> " + next.text+"</p>";
out += line;
}
}
document.getElementById("areaResults").innerHTML = out;
}
function jump(value){
var player = document.getElementById("video");
player.currentTime = value
}
function jsonToSrt(){
var out = "";
var json = JSON.parse(JSONFILE);
for (var data in json.actions[0].result.items){
var line = json.actions[0].result.items[data];
out += data +"\n";
out += line.start_time_offset + " --> " + line.end_time_offset+"\n";
out += line.text+"\n";
}
console.log(out);
downloadSrt(out);
}
function downloadSrt(srtData){
var file = new Blob([srtData], {type: "text/plain"});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, "captions.srt");
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = "captions.srt";
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}