-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherParser.js
395 lines (321 loc) · 12.9 KB
/
weatherParser.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
Copyright _ 2005, Apple Computer, Inc. All rights reserved.
NOTE: Use of this source code is subject to the terms of the Software
License Agreement for Mac OS X, which accompanies the code. Your use
of this source code signifies your agreement to such license terms and
conditions. Except as expressly granted in the Software License Agreement
for Mac OS X, no other copyright, patent, or other intellectual property
license or right is granted, either expressly or by implication, by Apple.
*/
var numRequests = 1;
// Request parameters
var gAppver = "1.1";
var gDevtype = "Apple Desktop Widget v" + gAppver;
var gDeployver = "Apple Desktop Widget v" + gAppver;
var gApp = "YGoAppleWeatherWidget";
var gAPIver = "1.0.0";
var gAPI = "weather";
//code -> weathername -> icon
// -> miniIcon
var yahooWeatherTypes =
[
"lightening", // 0 Tornado
"lightening", // 1 tropical storm
"lightening", // 2 hurricane
"lightening", // 3 severe thunderstorm
"lightening", // 4 thunderstorm
"rain&snow", // 5 mixed rain and snow
"rain&snow", // 6 mixed rain and sleet
"rain&snow", // 7 mixed snow and sleet
"rain&clouds", // 8 freezing drizzle
"rain&clouds", // 9 drizzle
"hail", // 10 freezing rain
"rain", // 11 showers
"rain", // 12 showers
"flurries", // 13 snow flurries
"flurries", // 14 light snow flurries
"snow", // 15 blowing snow
"snow", // 16 snow
"hail", // 17 hail
"hail", // 18 sleet
"sun", // 19 dust "haze"
"fog", // 20 foggy
"sun", // 21 haze "haze"
"sun", // 22 smoky "haze"
"wind", // 23 bustery
"wind", // 24 windy
"ice", // 25 cold
"clouds", // 26 clouds
"clouds", // 27 mostly cloudy (night)
"clouds", // 28 mostly cloudy (day)
["moon", "partlycomboclouds"], // 29 partly cloudy (night) //was suncloud
"partlycloudy", // 30 partly cloudy (day)
"moon", // 31 clear (night) //was sun
"sun", // 32 sunny
["moon", "partlycomboclouds"], // 33 fair (night) //was suncloud
"partlycloudy", // 34 fair (day)
"rain&snow", // 35 mixed rain and hail
"sun", // 36 hot
"lightening", // 37 isolated thunderstorms
"lightening", // 38 scattered thunderstorms
"rain&sun", // 39 scattered thunderstorm (yahoo icon is rain & sun)
"rain", // 40 scattered showers
"snow", // 41 heavy snow
"snow", // 42 scattered snow
"snow", // 43 heavy snow
"partlycloudy", // 44 partly cloudy (yahoo icon is n/a)
"rain&sun", // 45 thundershowers (yahoo icon is rain & sun)
"snow", // 46 snow showers
"lightening", // 47 isolated thundershowers "lightning" //was sun
];
if (window.timerInterval != 300000)
window.timerInterval = 300000; // 5 minutes
function findChild (element, nodeName)
{
var child;
for (child = element.firstChild; child != null; child = child.nextSibling)
{
if (child.nodeName == nodeName)
return child;
}
return null;
}
function applyFunctionToChildrenWhoMatch (element, nodeName, func, data)
{
var child;
for (child = element.firstChild; child != null; child = child.nextSibling)
{
if (child.nodeName == nodeName)
{
func (child, data);
}
}
}
function trimWhiteSpace (string)
{
return string.replace(/^\s*/, '').replace(/\s*$/, '');
}
// returns an anonymous object like so
// object
// error: Boolean false for success
// errorString: failure string
// hi: Fahrenheit
// lo: Fahrenheit
// temp: Fahrenheit
// icon : icon code
// icons: our icons to display
// description: description
// city: City (first caps)
// time: time 24 hours(nn:nn)
// sunset: time 24 hours (nn:nn)
// sunrise: time 24 hours (nn:nn)
// phases: array[7] of integers; -1 means no phase data 1-24
// forcast: array[6] of anonymous objects like so
// object
// hi: Fahrenheit
// lo: Fahrenheit
// icon: icon code
// ouricon: our icon code to display
// description: description
// daycode: (MON/TUE/WED/THU/FRI/SAT/SUN)
function fetchWeatherData (callback, zip)
{
var uid = getUID();
var url = 'http://iphone-wu.apple.com/dgw?imei=' + uid + '&apptype=weather&t=' + numRequests++;
var body = '<?xml version="1.0" encoding="utf-8"?><request devtype="' + gDevtype + '" deployver="' + gDeployver + '" app="' + gApp + '" appver="' + gAppver + '" api="' + gAPI + '" apiver="' + gAPIver + '" acknotification="0000">'
+ '<query id="0" timestamp="'
+ new Date().getTime() + '" type="getforecastbylocationid"><list>'
+ '<id>' + zip + '</id></list><unit>f</unit></query></request>';
var xml_request = new XMLHttpRequest();
xml_request.onload = function(e) {xml_loaded(e, xml_request, callback);}
xml_request.overrideMimeType("text/xml");
xml_request.open("POST", url);
xml_request.setRequestHeader("Content-type", "text/xml");
xml_request.setRequestHeader("X-Client-ID", "IMSI=" + uid);
xml_request.setRequestHeader("Cache-Control", "no-cache");
xml_request.setRequestHeader("Accept-Encoding", "");
xml_request.setRequestHeader("Accept-Language", "");
xml_request.setRequestHeader("Connection", "");
xml_request.send(body);
return xml_request;
}
function getUID() {
if (window.widget) {
return widget.identifier;
} else {
return (Math.random() * new Date().getTime()).toString();
}
}
function constructError (string)
{
return {error:true, errorString:string};
}
var days = ["SUN", "MON", "TUES", "WED", "THU", "FRI", "SAT"];
function parseDayCode (dayCode)
{
daycode = trimWhiteSpace(dayCode).substr (0, 3).toUpperCase();
return days[dayCode-1];
}
function xml_loaded (event, request, callback)
{
var obj = { error:false,
errorString: null,
time: null,
city: null,
temp: null,
description: null,
icon: null,
icons: null,
sunset: null,
sunrise: null,
phases: null,
hi: null,
lo: null,
forecast: null,
link: null
}
if (request.responseXML)
{
var obj = {error:false, errorString:null};
var responseElement = findChild (request.responseXML, "response");
if (responseElement == null) {callback(constructError("no <response>")); return;}
var resultElement = findChild (responseElement, "result");
if (resultElement == null) {callback(constructError("no <result>")); return;}
var listElement = findChild (resultElement, "list");
if (listElement == null) {callback(constructError("no <list>")); return;}
var itemElement = findChild (listElement, "item"); //could be a list, but this gets the first one
if (itemElement == null) {callback(constructError("no <item>")); return;}
var location = findChild (itemElement, "location");
obj.city = location.getAttribute("city");
obj.country = location.getAttribute("countryname");
var condition = findChild(itemElement, "condition");
obj.time = condition.getAttribute("time"); //TODO need to parse differently
obj.temp = condition.getAttribute("temp");
obj.description = condition.getAttribute("text");
var astronomy = findChild(itemElement, "astronomy");
obj.sunrise = astronomy.getAttribute("sunrise");
obj.sunset = astronomy.getAttribute("sunset");
obj.phase = astronomy.getAttribute("moonphase");
var link = findChild(itemElement, "link");
obj.link = link.firstChild.data;
obj.forecast = new Array;
var Forecasts = itemElement.getElementsByTagName("forecast");
if (Forecasts == null || Forecasts.length == 0) {callback(constructError("no Forecasts")); return;}
for(j=0; j < Forecasts.length; j++)
{
var foreElement = Forecasts.item(j);
var foreobj = {description:null, hi:0, lo:0, icon:-1};
foreobj.description = foreElement.getAttribute("text");
foreobj.hi = foreElement.getAttribute("high");
foreobj.lo = foreElement.getAttribute("low");
foreobj.daycode = parseDayCode(foreElement.getAttribute("dayofweek"));
foreobj.icon = foreElement.getAttribute("code");
foreobj.ouricon = yahooWeatherTypes[foreobj.icon];
obj.forecast.push(foreobj);
if(j == 0) //the first day is today
{
obj.hi = foreobj.hi;
obj.lo = foreobj.lo;
if (condition.getAttribute("code") == "3200")
{
condition.setAttribute("code", foreobj.icon);
condition.setAttribute("text", foreobj.description);
}
}
}
obj.icon = condition.getAttribute("code");
if (obj.icon < 0 || obj.icon > 47) {callback(constructError("icon code invalid, out of range (0-47) " + obj.icon)); return;}
obj.icons = yahooWeatherTypes[obj.icon];
callback (obj);
}
else
{
callback ({error:true, errorString:"XML request failed. no responseXML"});
}
}
function printObj (object) {
var string = "{";
for(att in object)
string += att + ": " + object[att] + ", ";
string += "}";
alert(string)
}
// returns an anonymous object like so
// object
// error: Boolean false for success
// errorString: failure string
// cities: array (alphabetical by name)
// object
// name: city name
// zip: postal code
// state: city state
// refine: boolean - true if the search is too generic
function validateWeatherLocation (location, callback)
{
var uid = getUID();
var url = 'http://iphone-wu.apple.com/dgw?imei=' + uid + '&apptype=weather&t=' + numRequests++;
var body = '<?xml version="1.0" encoding="utf-8"?><request devtype="' + gDevtype + '" deployver="' + gDeployver + '" app="' + gApp + '" appver="' + gAppver + '" api="' + gAPI + '" apiver="' + gAPIver + '" acknotification="0000">'
+ '<query id="3" timestamp="'
+ new Date().getTime() + '" type="getlocationid">'
+ '<phrase>' + location+ '</phrase></query></request>';
var xml_request = new XMLHttpRequest();
xml_request.onload = function(e) {xml_validateloaded(e, xml_request, callback);}
xml_request.overrideMimeType("text/xml");
// xml_request.open("GET", url+location);
xml_request.open("POST", url);
xml_request.setRequestHeader("Content-type", "text/xml");
xml_request.setRequestHeader("X-Client-ID", "IMSI=" + uid);
xml_request.setRequestHeader("Cache-Control", "no-cache");
xml_request.send(body);
return xml_request;
}
function xml_validateloaded (event, request, callback)
{
if (request.responseXML)
{
var obj = {error:false, errorString:null, cities:new Array, refine:false};
var responseElement = findChild (request.responseXML, "response");
if (responseElement == null) {callback(constructError("no <response>")); return;}
var resultElement = findChild (responseElement, "result");
if (resultElement == null) {callback(constructError("no <result>")); return;}
var listElement = findChild (resultElement, "list");
if (listElement == null) {callback(constructError("no <list>")); return;}
obj.refine = 0;
var items = listElement.getElementsByTagName("item");
for(var i=0; i < items.length; i++)
{
var itemElement = items.item(i);
var fields = ["city", "region", "regionname", "country", "countryname", "id"];
var currentElement = null;
var city = {};
var city = findChild(itemElement, "city");
city = (city && city.firstChild) ? city.firstChild.data : null; //check for <city />
var region = findChild(itemElement, "region");
region = (region && region.firstChild) ? region.firstChild.data : null;
var regionName = findChild(itemElement, "regionname");
regionName = (regionName && regionName.firstChild) ? regionName.firstChild.data : null;
var country = findChild(itemElement, "country");
country = (country && country.firstChild) ? country.firstChild.data : null;
var countryName = findChild(itemElement, "countryname");
countryName = (countryName && countryName.firstChild) ? countryName.firstChild.data : null;
var zip = findChild(itemElement, "id");
zip = (zip && zip.firstChild) ? zip.firstChild.data : null;
var state = region;
if (country != "US")
{
if (regionName && regionName != "")
state = regionName + " (" + countryName + ")";
else if (countryName && countryName != "")
state = countryName;
else
state = nil;
}
obj.cities.push({name: city, state: state, zip: zip});
}
callback (obj);
}
else
{
callback ({error:true, errorString:"XML request failed. no responseXML"});
}
}