jquery.simpleWeather.js 6.0 KB

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