|
1 | | -/* |
2 | | - * simpleWeather |
3 | | - * http://simpleweatherjs.com |
4 | | - * |
5 | | - * A simple jQuery plugin to display current weather data |
6 | | - * for any location and doesn't get in your way. |
7 | | - * |
8 | | - * Developed by James Fleeting <@fleetingftw> <http://iwasasuperhero.com> |
9 | | - * Another project from monkeeCreate <http://monkeecreate.com> |
10 | | - * |
11 | | - * Version 2.7.0 - Last updated: April 17 2014 |
12 | | - */ |
| 1 | +/*! simpleWeather v3.0.0 - http://simpleweatherjs.com */ |
13 | 2 | (function($) { |
14 | 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 | + |
15 | 13 | $.extend({ |
16 | 14 | simpleWeather: function(options){ |
17 | 15 | options = $.extend({ |
|
23 | 21 | }, options); |
24 | 22 |
|
25 | 23 | var now = new Date(); |
26 | | - var weatherUrl = '//query.yahooapis.com/v1/public/yql?format=json&rnd='+now.getFullYear()+now.getMonth()+now.getDay()+now.getHours()+'&diagnostics=true&callback=?&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&q='; |
| 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='; |
27 | 25 | if(options.location !== '') { |
28 | 26 | weatherUrl += 'select * from weather.forecast where woeid in (select woeid from geo.placefinder where text="'+options.location+'" and gflags="R") and u="'+options.unit+'"'; |
29 | 27 | } else if(options.woeid !== '') { |
30 | 28 | weatherUrl += 'select * from weather.forecast where woeid='+options.woeid+' and u="'+options.unit+'"'; |
31 | 29 | } else { |
32 | | - options.error("Could not retrieve weather due to an invalid location."); |
| 30 | + options.error({message: "Could not retrieve weather due to an invalid location."}); |
33 | 31 | return false; |
34 | 32 | } |
35 | 33 |
|
36 | 34 | $.getJSON( |
37 | 35 | encodeURI(weatherUrl), |
38 | 36 | function(data) { |
39 | 37 | if(data !== null && data.query !== null && data.query.results !== null && data.query.results.channel.description !== 'Yahoo! Weather Error') { |
40 | | - $.each(data.query.results, function(i, result) { |
41 | | - if (result.constructor.toString().indexOf("Array") !== -1) { |
42 | | - result = result[0]; |
43 | | - } |
44 | | - |
45 | | - var altTemps = [], heatIndex, images = []; |
46 | | - var compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']; |
47 | | - var windDirection = compass[Math.round(result.wind.direction / 22.5)]; |
48 | | - |
49 | | - if(result.item.condition.temp < 80 && result.atmosphere.humidity < 40) { |
50 | | - 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)); |
51 | | - } else { |
52 | | - heatIndex = result.item.condition.temp; |
53 | | - } |
| 38 | + var result = data.query.results.channel, |
| 39 | + weather = {}, |
| 40 | + forecast, |
| 41 | + compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'], |
| 42 | + image404 = "https://s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png"; |
54 | 43 |
|
55 | | - if(options.unit === "f") { |
56 | | - altTemps.unit = "c"; |
57 | | - altTemps.temp = Math.round((5.0/9.0)*(result.item.condition.temp-32.0)); |
58 | | - altTemps.high = Math.round((5.0/9.0)*(result.item.forecast[0].high-32.0)); |
59 | | - altTemps.low = Math.round((5.0/9.0)*(result.item.forecast[0].low-32.0)); |
60 | | - altTemps.forecastOneHigh = Math.round((5.0/9.0)*(result.item.forecast[1].high-32.0)); |
61 | | - altTemps.forecastOneLow = Math.round((5.0/9.0)*(result.item.forecast[1].low-32.0)); |
62 | | - altTemps.forecastTwoHigh = Math.round((5.0/9.0)*(result.item.forecast[2].high-32.0)); |
63 | | - altTemps.forecastTwoLow = Math.round((5.0/9.0)*(result.item.forecast[2].low-32.0)); |
64 | | - altTemps.forecastThreeHigh = Math.round((5.0/9.0)*(result.item.forecast[3].high-32.0)); |
65 | | - altTemps.forecastThreeLow = Math.round((5.0/9.0)*(result.item.forecast[3].low-32.0)); |
66 | | - altTemps.forecastFourHigh = Math.round((5.0/9.0)*(result.item.forecast[4].high-32.0)); |
67 | | - altTemps.forecastFourLow = Math.round((5.0/9.0)*(result.item.forecast[4].low-32.0)); |
68 | | - } else { |
69 | | - altTemps.unit = "f"; |
70 | | - altTemps.temp = Math.round((9.0/5.0)*result.item.condition.temp+32.0); |
71 | | - altTemps.high = Math.round((9.0/5.0)*result.item.forecast[0].high+32.0); |
72 | | - altTemps.low = Math.round((9.0/5.0)*result.item.forecast[0].low+32.0); |
73 | | - altTemps.forecastOneHigh = Math.round((9.0/5.0)*(result.item.forecast[1].high+32.0)); |
74 | | - altTemps.forecastOneLow = Math.round((9.0/5.0)*(result.item.forecast[1].low+32.0)); |
75 | | - altTemps.forecastTwoHigh = Math.round((9.0/5.0)*(result.item.forecast[2].high+32.0)); |
76 | | - altTemps.forecastTwoLow = Math.round((9.0/5.0)*(result.item.forecast[2].low+32.0)); |
77 | | - altTemps.forecastThreeHigh = Math.round((9.0/5.0)*(result.item.forecast[3].high+32.0)); |
78 | | - altTemps.forecastThreeLow = Math.round((9.0/5.0)*(result.item.forecast[3].low+32.0)); |
79 | | - altTemps.forecastFourHigh = Math.round((9.0/5.0)*(result.item.forecast[4].high+32.0)); |
80 | | - altTemps.forecastFourLow = Math.round((9.0/5.0)*(result.item.forecast[4].low+32.0)); |
81 | | - } |
| 44 | + weather.title = result.item.title; |
| 45 | + weather.temp = result.item.condition.temp; |
| 46 | + weather.code = result.item.condition.code; |
| 47 | + weather.todayCode = result.item.forecast[0].code; |
| 48 | + weather.currently = result.item.condition.text; |
| 49 | + weather.high = result.item.forecast[0].high; |
| 50 | + weather.low = result.item.forecast[0].low; |
| 51 | + weather.text = result.item.forecast[0].text; |
| 52 | + weather.humidity = result.atmosphere.humidity; |
| 53 | + weather.pressure = result.atmosphere.pressure; |
| 54 | + weather.rising = result.atmosphere.rising; |
| 55 | + weather.visibility = result.atmosphere.visibility; |
| 56 | + weather.sunrise = result.astronomy.sunrise; |
| 57 | + weather.sunset = result.astronomy.sunset; |
| 58 | + weather.description = result.item.description; |
| 59 | + weather.city = result.location.city; |
| 60 | + weather.country = result.location.country; |
| 61 | + weather.region = result.location.region; |
| 62 | + weather.updated = result.item.pubDate; |
| 63 | + weather.link = result.item.link; |
| 64 | + weather.units = {temp: result.units.temperature, distance: result.units.distance, pressure: result.units.pressure, speed: result.units.speed}; |
| 65 | + weather.wind = {chill: result.wind.chill, direction: compass[Math.round(result.wind.direction / 22.5)], speed: result.wind.speed}; |
82 | 66 |
|
83 | | - if(result.item.condition.code == "3200") { |
84 | | - images.thumbnail = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png"; |
85 | | - images.image = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png"; |
86 | | - } else { |
87 | | - images.thumbnail = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"ds.png"; |
88 | | - images.image = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"d.png"; |
89 | | - } |
| 67 | + if(result.item.condition.temp < 80 && result.atmosphere.humidity < 40) { |
| 68 | + 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)); |
| 69 | + } else { |
| 70 | + weather.heatindex = result.item.condition.temp; |
| 71 | + } |
90 | 72 |
|
91 | | - if(result.item.forecast[1].code == "3200") |
92 | | - images.forecastOne = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png"; |
93 | | - else |
94 | | - images.forecastOne = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[1].code+"d.png"; |
| 73 | + if(result.item.condition.code == "3200") { |
| 74 | + weather.thumbnail = image404; |
| 75 | + weather.image = image404; |
| 76 | + } else { |
| 77 | + weather.thumbnail = "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"ds.png"; |
| 78 | + weather.image = "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.condition.code+"d.png"; |
| 79 | + } |
95 | 80 |
|
96 | | - if(result.item.forecast[2].code == "3200") |
97 | | - images.forecastTwo = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png"; |
98 | | - else |
99 | | - images.forecastTwo = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[2].code+"d.png"; |
| 81 | + 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)}; |
| 82 | + if(options.unit === 'f') { |
| 83 | + weather.alt.unit = 'c'; |
| 84 | + } else { |
| 85 | + weather.alt.unit = 'f'; |
| 86 | + } |
100 | 87 |
|
101 | | - if(result.item.forecast[3].code == "3200") |
102 | | - images.forecastThree = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png"; |
103 | | - else |
104 | | - images.forecastThree = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[3].code+"d.png"; |
| 88 | + weather.forecast = []; |
| 89 | + for(var i=0;i<result.item.forecast.length;i++) { |
| 90 | + forecast = result.item.forecast[i]; |
| 91 | + forecast.alt = {high: getAltTemp(options.unit, result.item.forecast[i].high), low: getAltTemp(options.unit, result.item.forecast[i].low)}; |
105 | 92 |
|
106 | | - if(result.item.forecast[4].code == "3200") |
107 | | - images.forecastFour = "//s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png"; |
108 | | - else |
109 | | - images.forecastFour = "//l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[4].code+"d.png"; |
| 93 | + if(result.item.forecast[i].code == "3200") { |
| 94 | + forecast.image = image404; |
| 95 | + } else { |
| 96 | + forecast.image = "http://l.yimg.com/a/i/us/nws/weather/gr/"+result.item.forecast[1].code+"d.png"; |
| 97 | + } |
110 | 98 |
|
111 | | - var weather = { |
112 | | - title: result.item.title, |
113 | | - temp: result.item.condition.temp, |
114 | | - tempAlt: altTemps.temp, |
115 | | - code: result.item.condition.code, |
116 | | - todayCode: result.item.forecast[0].code, |
117 | | - units:{ |
118 | | - temp: result.units.temperature, |
119 | | - distance: result.units.distance, |
120 | | - pressure: result.units.pressure, |
121 | | - speed: result.units.speed, |
122 | | - tempAlt: altTemps.unit |
123 | | - }, |
124 | | - currently: result.item.condition.text, |
125 | | - high: result.item.forecast[0].high, |
126 | | - highAlt: altTemps.high, |
127 | | - low: result.item.forecast[0].low, |
128 | | - lowAlt: altTemps.low, |
129 | | - forecast: result.item.forecast[0].text, |
130 | | - wind:{ |
131 | | - chill: result.wind.chill, |
132 | | - direction: windDirection, |
133 | | - speed: result.wind.speed |
134 | | - }, |
135 | | - humidity: result.atmosphere.humidity, |
136 | | - heatindex: heatIndex, |
137 | | - pressure: result.atmosphere.pressure, |
138 | | - rising: result.atmosphere.rising, |
139 | | - visibility: result.atmosphere.visibility, |
140 | | - sunrise: result.astronomy.sunrise, |
141 | | - sunset: result.astronomy.sunset, |
142 | | - description: result.item.description, |
143 | | - thumbnail: images.thumbnail, |
144 | | - image: images.image, |
145 | | - tomorrow:{ |
146 | | - high: result.item.forecast[1].high, |
147 | | - highAlt: altTemps.forecastOneHigh, |
148 | | - low: result.item.forecast[1].low, |
149 | | - lowAlt: altTemps.forecastOneLow, |
150 | | - forecast: result.item.forecast[1].text, |
151 | | - code: result.item.forecast[1].code, |
152 | | - date: result.item.forecast[1].date, |
153 | | - day: result.item.forecast[1].day, |
154 | | - image: images.forecastOne |
155 | | - }, |
156 | | - forecasts:{ |
157 | | - one:{ |
158 | | - high: result.item.forecast[1].high, |
159 | | - highAlt: altTemps.forecastOneHigh, |
160 | | - low: result.item.forecast[1].low, |
161 | | - lowAlt: altTemps.forecastOneLow, |
162 | | - forecast: result.item.forecast[1].text, |
163 | | - code: result.item.forecast[1].code, |
164 | | - date: result.item.forecast[1].date, |
165 | | - day: result.item.forecast[1].day, |
166 | | - image: images.forecastOne |
167 | | - }, |
168 | | - two:{ |
169 | | - high: result.item.forecast[2].high, |
170 | | - highAlt: altTemps.forecastTwoHigh, |
171 | | - low: result.item.forecast[2].low, |
172 | | - lowAlt: altTemps.forecastTwoLow, |
173 | | - forecast: result.item.forecast[2].text, |
174 | | - code: result.item.forecast[2].code, |
175 | | - date: result.item.forecast[2].date, |
176 | | - day: result.item.forecast[2].day, |
177 | | - image: images.forecastTwo |
178 | | - }, |
179 | | - three:{ |
180 | | - high: result.item.forecast[3].high, |
181 | | - highAlt: altTemps.forecastThreeHigh, |
182 | | - low: result.item.forecast[3].low, |
183 | | - lowAlt: altTemps.forecastThreeLow, |
184 | | - forecast: result.item.forecast[3].text, |
185 | | - code: result.item.forecast[3].code, |
186 | | - date: result.item.forecast[3].date, |
187 | | - day: result.item.forecast[3].day, |
188 | | - image: images.forecastThree |
189 | | - }, |
190 | | - four:{ |
191 | | - high: result.item.forecast[4].high, |
192 | | - highAlt: altTemps.forecastFourHigh, |
193 | | - low: result.item.forecast[4].low, |
194 | | - lowAlt: altTemps.forecastFourLow, |
195 | | - forecast: result.item.forecast[4].text, |
196 | | - code: result.item.forecast[4].code, |
197 | | - date: result.item.forecast[4].date, |
198 | | - day: result.item.forecast[4].day, |
199 | | - image: images.forecastFour |
200 | | - }, |
201 | | - }, |
202 | | - city: result.location.city, |
203 | | - country: result.location.country, |
204 | | - region: result.location.region, |
205 | | - updated: result.item.pubDate, |
206 | | - link: result.item.link |
207 | | - }; |
| 99 | + weather.forecast.push(forecast); |
| 100 | + } |
208 | 101 |
|
209 | | - options.success(weather); |
210 | | - }); |
| 102 | + options.success(weather); |
211 | 103 | } else { |
212 | | - if (data.query.results === null) { |
213 | | - options.error("An invalid WOEID or location was provided."); |
214 | | - } else { |
215 | | - options.error("There was an error retrieving the latest weather information. Please try again."); |
216 | | - } |
| 104 | + options.error({message: "There was an error retrieving the latest weather information. Please try again.", error: data.query.results.channel.item.title}); |
217 | 105 | } |
218 | 106 | } |
219 | 107 | ); |
|
0 commit comments