-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
232 lines (210 loc) · 6.59 KB
/
app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var record = require('./routes/record');
var users = require('./routes/users');
var request = require('request');
var Uber = require('uber-api')({
server_token: 'cepnsST9mZ0VJPuWT4y6h_82P9-xxLLpA4ZmBfIi',
version: 'v1'
});
var fs = require('fs');
var url = require('url');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('port', process.env.PORT || 9000);
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/record', record);
/**
* Get speech recognition from HPE
* https://github.com/danwrong/restler
*/
function getSpeechRecognition(file) {
var apikey = "9f68afe8-cdd7-43b3-a9e6-bfdb9e1d93bb";
var URL = "https://api.havenondemand.com/1/api/async/recognizespeech/v1?&file=" + file + "&apikey=" + apikey;
fs.stat(file, function(err, stats) {
restler.post(URL, {
multipart: true,
data: {
"folder_id": "0",
"filename": restler.file(file, null, stats.size, null, "audio/wav")
}
}).on("complete", function(data) {
console.log("WORKING!");
});
});
}
/**
* Get estimate price from current location to destination with Uber API
*/
function getEstimatePriceUber(start_lat, start_long, end_lat, end_long, cb) {
Uber.getPriceEstimate({
sLat: start_lat,
sLng: start_long,
eLat: end_lat,
eLng: end_long
}, function(err, response) {
if (!err) {
cb(null, response);
}
console.log(err);
});
}
/**
* Request a ride from Uber.
*/
function getEstimateTimeUser(start_lat, start_long, cb) {
Uber.getTimeEstimate({
sLat: start_lat,
sLng: start_long,
}, function(err, response) {
if (!err) {
cb(null, response);
}
console.log(err);
});
}
/**
* Convert lat and long to Address formatted_address.
*/
function latLongToAddress(lat, long, cb) {
var URL = "http://maps.googleapis.com/maps/api/geocode/json?address=" + lat + "," + long + "&sensor=false";
request(URL, function(err, res, body) {
if (!err) {
cb(null, JSON.parse(body).results[0].formatted_address);
}
});
}
/**
* Get list of all nearby restaurants.
* GrubHub API made with love and beast-mode activated by Rohit.
*/
function findRestuarant(lat, long, food, cb) {
console.log("Finding rest.");
request.post({
url: "https://api-gtm.grubhub.com/auth",
json: {
"brand": "GRUBHUB",
"client_id": "beta_UmWlpstzQSFmocLy3h1UieYcVST",
"scope": "anonymous",
"device_id": -901860116
}
}, function(err, res, body) {
console.log(err);
request.get("https://api-gtm.grubhub.com/restaurants/search?orderMethod=delivery&locationMode=DELIVERY&facetSet=umami&pageSize=20&hideHateos=true&queryText=" + food + "&location=POINT(" + long + "%20" + lat + ")&variationId=default-impressionScoreBaseBuffed-20160317&countOmittingTimes=true", {
'auth': {
'bearer': body.session_handle.access_token
}
}, function(err, res, body) {
console.log(err);
cb(null, JSON.parse(body).search_result.results);
});
});
}
/**
* Get all movie based on current location.
*/
function findMovie(lat, long, date, cb) {
var api_key = "37eshrgqbkxxbpqhgmbadt52";
var URL = "http://data.tmsapi.com/v1.1/movies/showings?startDate=" + formatDate(new Date()) + "&lat=" + lat + "&lng=" + long + "&radius=20&units=mi&api_key=" + api_key;
request(URL, function(error, response, body) {
console.log(error);
if (!error && response.statusCode == 200) {
cb(null, JSON.parse(body));
}
});
}
/**
* Convert date to yyyy-mm-dd format
*/
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
/**
* Wingman API to get Uber estimate price, restaurants and movies
* /v1/find - get list of restaurants and movies.
* /v1/uber - get estimate price and time.
*/
app.use('/v1/speech', function(req, res, next) {
});
app.use('/v1/find', function(req, res, next) {
findMovie(req.body.slat, req.body.slon, formatDate(req.body.date), function(err, movies) {
console.log(err);
if (!err) {
findRestuarant(req.body.slat, req.body.slon, req.body.food, function(err, restaurants) {
if (!err) {
res.json({
movies: movies,
restaurants: restaurants
});
}
console.log(err);
});
}
console.log(err);
});
});
app.use('/v1/uber', function(req, res, next) {
var slat = req.body.slat;
var slon = req.body.slon;
var elat = req.body.elat;
var elon = req.body.elon;
getEstimatePriceUber(slat, slon, elat, elon, function(err, price) {
if (!err) {
getEstimateTimeUser(slat, slon, function(err, time) {
if (!err) {
res.json({
price: price,
time: time
});
}
console.log(err);
});
}
console.log(err);
});
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
app.listen(app.get("port"));
console.log("Server is listening at port: " + (process.env.PORT || 9000));
module.exports = app;