initial commit
[JIRC.git] / lib / googleweather.js
CommitLineData
39c8b14f 1/* Modified version of node-googleweather by maxkueng
2 * (Removed the celsius and kmh conversions)
3 * Original library can be found at:
4 * https://github.com/maxkueng/node-googleweather/
5 *
6MIT License
7
8Copyright (c) 2011 Max Kueng (http://maxkueng.com/)
9
10Permission is hereby granted, free of charge, to any person obtaining
11a copy of this software and associated documentation files (the
12"Software"), to deal in the Software without restriction, including
13without limitation the rights to use, copy, modify, merge, publish,
14distribute, sublicense, and/or sell copies of the Software, and to
15permit persons to whom the Software is furnished to do so, subject to
16the following conditions:
17
18The above copyright notice and this permission notice shall be
19included in all copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28*/
29
30var request = require('request');
31var jsdom = require('jsdom');
32
33Date.prototype.pad = function (n) {
34 return (n < 10) ? '0' + n : n;
35};
36
37Date.prototype.toISODate = function () {
38 return this.getFullYear() + '-'
39 + this.pad(this.getMonth() + 1) + '-'
40 + this.pad(this.getDate());
41};
42
43var get = function (callback, location, forecastDate) {
44 var currentConditions = {
45 'date' : null,
46 'condition' : null,
47 'temperature' : null,
48 'humidity' : null,
49 'wind' : {
50 'direction' : null,
51 'speed' : null
52 }
53 };
54
55 var forecast = [];
56
57 var uri = 'http://www.google.com/ig/api?weather='
58 + escape(location);
59
60 request({
61 'method' : 'GET',
62 'uri' : uri,
63 'headers' : {
64 'User-Agent' : 'Nintendo Gameboy/1.0'
65 }
66 }, function (error, response, body) {
67 if (!error && response.statusCode == 200) {
68 jsdom.env(body, [],
69 function(errors, window) {
70 try {
71 var document = window.document;
72 var info = document.getElementsByTagName('forecast_information')[0];
73 var current = document.getElementsByTagName('current_conditions')[0];
74 var forecasts = document.getElementsByTagName('forecast_conditions');
75
76 var baseDateString = info.getElementsByTagName('forecast_date')[0].getAttribute('data');
77 var baseDate = new Date(baseDateString);
78
79 currentConditions.date = baseDateString;
80 currentConditions.condition = current.getElementsByTagName('condition')[0].getAttribute('data');
81 currentConditions.temperature = current.getElementsByTagName('temp_f')[0].getAttribute('data');
82
83 var humidityString = current.getElementsByTagName('humidity')[0].getAttribute('data');
84 currentConditions.humidity = /(\d+)%/.exec(humidityString)[1];
85
86 var windString = current.getElementsByTagName('wind_condition')[0].getAttribute('data');
87 var windMatches = /([NESW]+)\sat\s([0-9]+)\smph/.exec(windString);
88 currentConditions.wind.direction = windMatches[1];
89 currentConditions.wind.speed = windMatches[2];
90
91 for (var i = 0; i < forecasts.length; i++) {
92 var d = new Date(baseDate.getTime() + ( i * 24 * 3600 * 1000 ));
93 var condition = forecasts[i].getElementsByTagName('condition')[0].getAttribute('data');
94 var lowTemp = forecasts[i].getElementsByTagName('low')[0].getAttribute('data');
95 var highTemp = forecasts[i].getElementsByTagName('high')[0].getAttribute('data');
96
97 var forecastDay = {
98 'date' : d.toISODate(),
99 'condition' : condition,
100 'temperature' : {
101 'low' : lowTemp,
102 'high' : highTemp
103 }
104 };
105
106 forecast[d.toISODate()] = forecastDay;
107 }
108
109 callback(currentConditions, forecast[forecastDate]);
110 } catch(err) {
111 callback(null, null);
112 return;
113 }
114 }
115 );
116 }
117 });
118};
119
120exports.get = get;