-
Notifications
You must be signed in to change notification settings - Fork 3
/
wu.jsm
95 lines (78 loc) · 2.97 KB
/
wu.jsm
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
// -*- Mode: JavaScript; tab-width: 4 -*- vim:tabstop=4 syntax=javascript:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*global module: false */
// Please don't use the included API key in other applications.
// The default API key is for personal/non-commercial use only.
module.version = 0.8;
module.API_KEY = "bb7ba883f6b6aea5";
module.BASE_URL = "http://api.wunderground.com/api/" + module.API_KEY;
module.COND_BASE_URL = module.BASE_URL + "/conditions/q/";
// forecast doesn't return the location
module.FORECAST_BASE_URL = module.BASE_URL + "/geolookup/forecast/q/";
module.cmd_weather = module.cmd_wu = module.cmd_wunderground = function cmd_wu(e) {
var args = e.args;
if (!args) {
e.reply(this.cmd_wu.help);
return true;
}
var data;
try {
data = e.bot.getJSON(this.COND_BASE_URL + encodeURI(args) + ".json", "wu", this.version);
} catch (ex) {}
if (!data) {
e.reply("Weather Underground returned no data.");
return true;
}
var error = data.response.error;
if (error) {
e.nreply(error.type + ":", error.description);
return true;
}
var results = data.response.results;
if (results) {
var names = results.map(function (loc) "{city}, {state}, {country}".format(loc));
e.nreply(results.length, "locations matched your query:", names.join(" - "));
return true;
}
var curr = data.current_observation, res = [curr.weather];
res.push("Temp: " + curr.temperature_string);
res.push("Humidity: " + curr.relative_humidity);
res.push("Wind: " + curr.wind_string);
res.push("Rainfall today: " + curr.precip_today_string);
res.push("UV: " + curr.UV);
e.nreply("Current weather for", curr.display_location.full, "from Weather Underground (" + curr.observation_time + "):", res.join(" - "));
return true;
};
module.cmd_wu.help = "Get current weather conditions from Weather Underground.";
module.cmd_forecast = module.cmd_wu_forecast = function cmd_wu_forecast(e) {
var args = e.args;
if (!args) {
e.reply(this.cmd_wu_forecast.help);
return true;
}
var data;
try {
data = e.bot.getJSON(this.FORECAST_BASE_URL + encodeURI(args) + ".json", "wu", this.version);
} catch (ex) {}
if (!data) {
e.reply("Weather Underground returned no data.");
return true;
}
var error = data.response.error;
if (error) {
e.nreply(error.type + ":", error.description);
return true;
}
var results = data.response.results;
if (results) {
var names = results.map(function (loc) "{city}, {state}, {country}".format(loc));
e.nreply(results.length, "locations matched your query:", names.join(" - "));
return true;
}
var loc = data.location, f0 = data.forecast.txt_forecast.forecastday[0];
e.nreply(f0.title, "weather forecast for", "{city}, {state}, {country}".format(loc), "from Weather Underground:", f0.fcttext_metric);
return true;
};
module.cmd_wu_forecast.help = "Get weather forecast from Weather Underground.";