-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (47 loc) · 2.28 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
var querystring = require('querystring'),
request = require('request');
function getWeather(route, args) {
// API Key for Wunderground.com – no idea who "owns" this key
var apiKey = '83f199a422e382c3';
// Parse location from the args; fall back to Austin if no location was passed
var location = args.length ? querystring.escape(args.map(function (x) { return x.replace(/,/g, ''); })) : 'TX/Austin';
// Set up the URL for the request
// Fun fact: Wunderground supports localization, but NOT for the conditions endpoint. Awesome.
var url = 'http://api.wunderground.com/api/' + apiKey + '/conditions/q/' + location + '.json';
// Make the request
request.get(url, function (err, res, body) {
// Parse the response
var results = JSON.parse(body),
data = results.current_observation;
if (data) {
// If we have weather data, return a formatted version
route.send('?weather_success_response', data.display_location.full, data.weather, data.temp_f, data.feelslike_f, data.wind_string.substring(0, 1).toLowerCase() + data.wind_string.substring(1));
} else if (results.response.results) {
// If Wunderground returns a list of cities, list them
var possibilities = [];
for (var index in results.response.results) {
possibilities.push(' ' + (parseInt(index) + 1) + '. ' + results.response.results[index].city + ', ' + results.response.results[index].state + ' ' + results.response.results[index].country);
}
route.send('?weather_suggestion_response', possibilities.join('\n'));
} else if (results.response.error) {
// If Wunderground returns an error, print it
route.send('?response_error', results.response.error.description);
} else {
// If we get this far, reply with a witty message
route.send('?generic_error');
}
}.bind(this));
}
module.exports = {
displayname : 'Weather',
description : 'Gets the current weather.',
commands : [
{
name : 'Weather',
description : 'Gets the current weather.',
usage : 'weather [search term]',
trigger : /weather/i,
func : getWeather
}
]
};