master
1/*! simpleWeather v3.1.0 - http://simpleweatherjs.com */
2(function($) {
3 'use strict';
4
5 function getAltTemp(unit, temp) {
6 if(unit === 'f') {
7 return Math.round((5.0/9.0)*(temp-32.0));
8 } else {
9 return Math.round((9.0/5.0)*temp+32.0);
10 }
11 }
12
13 $.extend({
14 simpleWeather: function(options){
15 options = $.extend({
16 location: '',
17 woeid: '',
18 unit: 'f',
19 success: function(weather){},
20 error: function(message){}
21 }, options);
22
23 var now = new Date();
24 var weatherUrl = 'https://query.yahooapis.com/v1/public/yql?format=json&rnd=' + now.getFullYear() + now.getMonth() + now.getDay() + now.getHours() + '&diagnostics=true&callback=?&q=';
25
26 if(options.location !== '') {
27 /* If latitude/longitude coordinates, need to format a little different. */
28 var location = '';
29 if(/^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/.test(options.location)) {
30 location = '(' + options.location + ')';
31 } else {
32 location = options.location;
33 }
34
35 weatherUrl += 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="' + location + '") and u="' + options.unit + '"';
36 } else if(options.woeid !== '') {
37 weatherUrl += 'select * from weather.forecast where woeid=' + options.woeid + ' and u="' + options.unit + '"';
38 } else {
39 options.error('Could not retrieve weather due to an invalid location.');
40 return false;
41 }
42
43 $.getJSON(
44 encodeURI(weatherUrl),
45 function(data) {
46 if(data !== null && data.query !== null && data.query.results !== null && data.query.results.channel.description !== 'Yahoo! Weather Error') {
47 var result = data.query.results.channel,
48 weather = {},
49 forecast,
50 compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'],
51 image404 = 'https://s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png';
52
53 weather.title = result.item.title;
54 weather.temp = result.item.condition.temp;
55 weather.code = result.item.condition.code;
56 weather.todayCode = result.item.forecast[0].code;
57 weather.currently = result.item.condition.text;
58 weather.high = result.item.forecast[0].high;
59 weather.low = result.item.forecast[0].low;
60 weather.text = result.item.forecast[0].text;
61 weather.humidity = result.atmosphere.humidity;
62 weather.pressure = result.atmosphere.pressure;
63 weather.rising = result.atmosphere.rising;
64 weather.visibility = result.atmosphere.visibility;
65 weather.sunrise = result.astronomy.sunrise;
66 weather.sunset = result.astronomy.sunset;
67 weather.description = result.item.description;
68 weather.city = result.location.city;
69 weather.country = result.location.country;
70 weather.region = result.location.region;
71 weather.updated = result.item.pubDate;
72 weather.link = result.item.link;
73 weather.units = {temp: result.units.temperature, distance: result.units.distance, pressure: result.units.pressure, speed: result.units.speed};
74 weather.wind = {chill: result.wind.chill, direction: compass[Math.round(result.wind.direction / 22.5)], speed: result.wind.speed};
75
76 if(result.item.condition.temp < 80 && result.atmosphere.humidity < 40) {
77 weather.heatindex = -42.379+2.04901523*result.item.condition.temp+10.14333127*result.atmosphere.humidity-0.22475541*result.item.condition.temp*result.atmosphere.humidity-6.83783*(Math.pow(10, -3))*(Math.pow(result.item.condition.temp, 2))-5.481717*(Math.pow(10, -2))*(Math.pow(result.atmosphere.humidity, 2))+1.22874*(Math.pow(10, -3))*(Math.pow(result.item.condition.temp, 2))*result.atmosphere.humidity+8.5282*(Math.pow(10, -4))*result.item.condition.temp*(Math.pow(result.atmosphere.humidity, 2))-1.99*(Math.pow(10, -6))*(Math.pow(result.item.condition.temp, 2))*(Math.pow(result.atmosphere.humidity,2));
78 } else {
79 weather.heatindex = result.item.condition.temp;
80 }
81
82 if(result.item.condition.code == '3200') {
83 weather.thumbnail = image404;
84 weather.image = image404;
85 } else {
86 weather.thumbnail = 'https://s.yimg.com/zz/combo?a/i/us/nws/weather/gr/' + result.item.condition.code + 'ds.png';
87 weather.image = 'https://s.yimg.com/zz/combo?a/i/us/nws/weather/gr/' + result.item.condition.code + 'd.png';
88 }
89
90 weather.alt = {temp: getAltTemp(options.unit, result.item.condition.temp), high: getAltTemp(options.unit, result.item.forecast[0].high), low: getAltTemp(options.unit, result.item.forecast[0].low)};
91 if(options.unit === 'f') {
92 weather.alt.unit = 'c';
93 } else {
94 weather.alt.unit = 'f';
95 }
96
97 weather.forecast = [];
98 for(var i=0;i<result.item.forecast.length;i++) {
99 forecast = result.item.forecast[i];
100 forecast.alt = {high: getAltTemp(options.unit, result.item.forecast[i].high), low: getAltTemp(options.unit, result.item.forecast[i].low)};
101
102 if(result.item.forecast[i].code == "3200") {
103 forecast.thumbnail = image404;
104 forecast.image = image404;
105 } else {
106 forecast.thumbnail = 'https://s.yimg.com/zz/combo?a/i/us/nws/weather/gr/' + result.item.forecast[i].code + 'ds.png';
107 forecast.image = 'https://s.yimg.com/zz/combo?a/i/us/nws/weather/gr/' + result.item.forecast[i].code + 'd.png';
108 }
109
110 weather.forecast.push(forecast);
111 }
112
113 options.success(weather);
114 } else {
115 options.error('There was a problem retrieving the latest weather information.');
116 }
117 }
118 );
119 return this;
120 }
121 });
122})(jQuery);