forked from BeyondVerbal-V3/JavaScript-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.html
186 lines (163 loc) · 6.15 KB
/
file.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>JavaScript Sample Code</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
<style>
label.error { color: red; font-weight: normal; }
</style>
</head>
<body>
<div class="container">
<h1>JavaScript Sample Code</h1>
<hr />
<form id="form" class="form-horizontal">
<div class="form-group">
<label for="input_file" class="col-sm-2 control-label">Input File</label>
<div class="col-sm-10">
<input type="file" class="form-control" id="input_file" required>
</div>
</div>
<div class="form-group">
<label for="api_key" class="col-sm-2 control-label">API Key</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="api_key" placeholder="API Key" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button id="submit" type="submit" class="btn btn-default">Start</button>
</div>
</div>
</form>
<div class="panel panel-default">
<div class="panel-heading">Results</div>
<div class="panel-body">
<ul id="result"></ul>
</div>
</div>
</div>
<script type="text/javascript">
if (window.File && window.FileReader && window.FileList && window.Blob)
{
console.log("This browser confirm window.File && window.FileReader !!!");
}
else
{
alert('The File APIs are not fully supported in this browser.');
}
options = {
url: {
tokenUrl: 'https://token.beyondverbal.com/token',
serverUrl: 'https://apiv3.beyondverbal.com/v3/recording/'
},
apiKey: $("#api_key").val(),
token: ''
};
$(function ()
{
$("#form").validate({
submitHandler: function (form)
{
$("#submit").attr("disabled", true).text("Analyze...");
$('#result').html('');
authenticate()
.error(function (jqXHR, textStatus, errorThrown)
{
Show(JSON.stringify(jqXHR) + errorThrown);
})
.success(function (data)
{
console.log('sucess::' + JSON.stringify(data));
var token = JSON.parse(data);
options.token = token.access_token;
uploadFile(form[0].files[0]);
});
}
});
});
function uploadFile(file)
{
if (typeof FileReader !== "undefined")
{
var reader = new FileReader();
reader.onload = function (e)
{
analyzeFile($("#api_key").val(), e.target.result)
.done(function (res)
{
Show(res);
$("#submit").attr("disabled", false).text("Start");
})
.fail(function (err)
{
Show(err);
$("#submit").attr("disabled", false).text("Start");
});
};
reader.readAsArrayBuffer(file);
}
}
function authenticate()
{
console.log('url token:' + options.url.tokenUrl);
options.apiKey = $("#api_key").val();
return $.ajax({
url: options.url.tokenUrl,
type: "POST",
dataType: 'text',
contentType: 'application/x-www-form-urlencoded',
data: {
grant_type: "client_credentials",
apiKey: options.apiKey
}
});
}
function analyzeFile(apiKey, content, interval)
{
var dfd = $.Deferred();
var startUrl = options.url.serverUrl+'start';
//console.log('url::' + startUrl + ' token:' + options.token);
$.ajax({
url: startUrl,
headers: { 'Authorization': "Bearer " + options.token },
type: "POST",
cache: false,
data: JSON.stringify({ dataFormat: { type: "WAV" } }),
contentType: 'application/x-www-form-urlencoded',
dataType: 'text'
})
.then(function (data)
{
Show(data);
var recID = data.recordingId ? data.recordingId : JSON.parse(data).recordingId;
//console.log('recid::' + recID);
var upStreamUrl = options.url.serverUrl + recID;
//post content for analysis
$.ajax({
url: upStreamUrl,
headers: { 'Authorization': "Bearer " + options.token },
data: content,
contentType: false,
processData: false,
cache: false,
dataType: 'text',
type: "POST"
})
.then(dfd.resolve, dfd.reject);
}, dfd.reject);
return dfd.promise().always(function ()
{
});
}
function Show(json)
{
$('#result').append("<li>" + JSON.stringify(json) + "</li>");
}
</script>
</body>
</html>