-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (54 loc) · 1.97 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
var express = require('express');
var app = express();
var request = require('request');
var msTranslator = require('mstranslator');
var cheerio = require("cheerio");
var _ = require('underscore');
var fs = require('fs');
$ = cheerio.load(fs.readFileSync('./index.html'));
// Variable containing secrets, should contain at least:
// {
// microsoft: {
// client_id: "replace_with_ms ID",
// client_secret: "replace with ms client secret"
// },
// google: {
// key: "google API key goes here"
// }
// }
require("./APISecrets.js");
require('./streetView.js');
// Translation related Language parameters:
var fromLang = "en",
toLang = "zh";
//set up translator
var client = new msTranslator({
client_id: APISecrets.microsoft.client_id,
client_secret: APISecrets.microsoft.client_secret
}, true);
app.get('/', function(req, res){
//get the parameter for start and end locations:
var start = req.query.start;
var end = req.query.end;
var method = req.query.method || "walking"; //default to 'walking' to target
// Route URL:
var routeURL = "https://maps.googleapis.com/maps/api/directions/json?origin="+encodeURIComponent(start)+
"&destination="+encodeURIComponent(end)+"&mode="+method+"&key="+APISecrets.google.key;
console.log(routeURL);
// Generate HTML
getHTMLContent(routeURL, function (html, instructionStrings) {
$("#step_content").html(html);
// console.log(instructionStrings);
//translate all instructions
// res.send($.html());
client.translateArray({texts: instructionStrings, from: fromLang, to: toLang}, function(err, data) {
var translationEls = $(".translation");
_.each(data, function (val, index) {
// console.log("bla"+$(translationEls[index]).html());
$(translationEls[index]).html("("+val.TranslatedText+")");
});
res.send($.html());
});
});
});
app.listen(3000);