-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (94 loc) · 3.04 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
require('dotenv').load();
var nominatim = require('./nominatim')
var Q = require('q')
var googleMapsClient = require('@google/maps').createClient({
Promise: require('q').Promise
});
var main = function(input_sms) {
var inputArray = parseInput(input_sms)
if (inputArray.error) {
return inputArray.error
} else {
return geocode(inputArray)
.then(function(result) {
return lookupDirections(result, inputArray)
})
.catch(function(error) {
return {
error: error.name + ': ' + error.message
}
})
.then(function(result) {
console.log(result)
if (result.error) {
console.log(result.error)
return result.error
} else {
return sendResults(result)
}
})
}
}
var geocode = function(data) {
console.log('gecoding')
var start = nominatim(data[0])
var end = nominatim(data[1])
return Q.all([start, end])
}
var lookupDirections = function(data, inputArray) {
origin = finalAddress(data[0], inputArray[0])
destination = finalAddress(data[1], inputArray[1])
return googleMapsClient.directions({
origin: origin,
destination: destination,
mode: data[2].toLowerCase().trim().replace('biking', 'bicycling')
})
.asPromise()
}
var finalAddress = function(nominatimInput, rawInput) {
if (nominatimInput) {
return [nominatimInput.lat, nominatimInput.lon]
} else {
console.log('nominatim failed')
return rawInput
}
}
var parseInput = function(input_sms) {
var inputArray = input_sms.split(';')
if (inputArray.length != 3) {
var msg = 'Error! Input should be delimited by semi colons, ' +
'and in the form {start};{end};{mode} where mode is one of {driving, ' +
'bicycling, transit, walking}'
return {
error: Error(msg)
}
} else {
return inputArray
}
}
var sendResults = function(response) {
//console.log('got results from google maps:')
//console.log(response)
if (response.json.status === 'ZERO_RESULTS') {
console.log('zero results')
return 'No results found, try different search terms'
} else {
stepArr = []
var steps = response.json.routes[0].legs[0].steps
for (i = 0; i < steps.length; i++) {
var step = steps[i]
var instruction = step.html_instructions.replace(/<(?:.|\n)*?>/gm, '').replace(/[^\x00-\x7F]/g, "");
var distance = step.distance.text
var msg = instruction + ' (' + distance + ')';
stepArr.push(msg)
}
console.log(stepArr)
return stepArr.join(', ')
}
}
<<<<<<< HEAD
var mystr = '1762 U St NW DC;Comet Ping Pong;Walking'
=======
var mystr = '1762 U St NW DC;Comet DC;Walking'
>>>>>>> 1e5a9286f36d91f56832eb1a5ddf7bb1ef2f2ed9
console.log(main(mystr))