This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
195 lines (172 loc) · 5.47 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
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
192
193
194
195
'use strict';
const electron = require('electron');
const config = require('./config.js');
const https = require('https');
const request = require("request");
const fs = require("fs");
const exec = require('child_process').exec;
const app = electron.app;
const google = require('googleapis');
const record = require('node-record-lpcm16');
var authClient;
google.auth.getApplicationDefault( (err, ac) => {
if (err) {
console.log(err);
} else if (ac.createScopedRequired && ac.createScopedRequired()) {
authClient = ac.createScoped(['https://www.googleapis.com/auth/cloud-platform']);
}
});
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
// prevent window being garbage collected
let mainWindow;
let lat = '1.352083';
let lng = '103.819836';
function onClosed() {
// dereference the window
// for multiple windows store them in an array
mainWindow = null;
}
function createMainWindow() {
const win = new electron.BrowserWindow({
width: 600,
height: 400
});
win.loadURL(`file://${__dirname}/index.html`);
win.setFullScreen(true);
win.on('closed', onClosed);
return win;
}
electron.ipcMain.on("getAudioInput", (event) => {
setTimeout(() => {
event.sender.send("show-waiting");
exec('rec --encoding signed-integer --bits 16 --channels 1 --rate 16000 out.wav trim 0 3', () => {
fs.readFile("out.wav", (err, data) => {
if (err) {
return console.log(err);
}
var audioString = new Buffer(data).toString('base64');
google.speech('v1beta1').speech.syncrecognize({
"auth": authClient,
"resource": {
"config": {
"encoding": "LINEAR16",
"sampleRate": 16000,
"speechContext": {
"phrases": [ "f01", "f02", "f03", "f04", "f05", "f06", "f07", "f08", "f09"]
}
},
"audio": {
"content": data.toString('base64')
}
}}, (err, response) => {
if (err) {
console.error(err)
}
fs.unlink("out.wav");
console.log(response.results[0].alternatives[0].transcript);
decode(event, response.results[0].alternatives[0].transcript);
});
});
});
},3000)
});
const renderResponse = (event, response, message) => {
try {
if (response.entities.intent !== null){
// there is a valid response
console.log(response.entities);
switch (response.entities.intent[0].value) {
case "weather":
if (response.entities.location !== null){
// get weather in location
https.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${response.entities.location[0].value}`, (res) => {
var buffer = "", data;
res.on("data", (chunk) => {
buffer += chunk;
});
res.on("end", (err) => {
data = JSON.parse(buffer);
lat = data.results[0].geometry.location.lat;
lng = data.results[0].geometry.location.lng;
getWeather(event, response.entities.location[0].value);
});
})
} else {
// get in singapore
getWeather(event, "Singapore");
}
break
case "timetable":
var q = response.entities.local_search_query ? response.entities.local_search_query[0].value : response.entities.search_query[0].value
getTimetable(event, q.toUpperCase());
break
case "video":
var q = response.entities.local_search_query ? response.entities.local_search_query[0].value : response.entities.search_query[0].value
getVideo(event, q);
break
default:
console.log("no value responses.");
}
}
} catch(e){
console.log(e);
event.sender.send('undefined-method', message)
}
}
const getTimetable = (event, group) => {
request.get(
{url: 'http://sutd-timetable.herokuapp.com/group_sections?' + group},
(err, httpResponse, body) => {
var res = JSON.parse(body);
event.sender.send('timetable-reply', res.events);
}
);
}
const getVideo = (event, query) => {
// event.sender.send('play-video', "Ri6wvGjuoOg");
google.youtube('v3').search.list({"q": query, "part": "snippet", "maxResults": 1, "key": config.google.key}, (err, val) =>{
if (val.items[0].id.kind == "youtube#video"){
event.sender.send('play-video', val.items[0].id.videoId);
};
});
}
const getWeather = (event, location) => {
let url = `https://api.forecast.io/forecast/${config.weather.key}/${lat},${lng}?units=${config.weather.units}&exclude=currently,daily`
var request = https.get(url, (res) => {
var buffer = "", data;
res.on("data", (chunk) => {
buffer += chunk;
});
res.on("end", (err) => {
data = JSON.parse(buffer);
event.sender.send('weather-reply', [data, location]);
});
});
}
const decode = (event, message) => {
request.get(
{ headers: { "Authorization": "Bearer " + config.wit.key },
url: 'https://api.wit.ai/message?v=20160902&q='+message
},
(err, httpResponse, body) => {
var response = JSON.parse(body);
if (err) {
return console.error(err);
}
renderResponse(event, response, message);
});
};
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on('ready', () => {
mainWindow = createMainWindow();
});