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
|
const getWeather = function (options) {
const defaults = { apiKeyIp: null, apiKeyWeather: null, selector: '#app', convertTemp: false, noWeather: 'Unable to get weather data at this time. Sorry!', showIcon: true, showTemp: true, showCity: true, showConditions: true, };
const settings = Object.assign(defaults, options);
const app = document.querySelector(settings.selector);
const sanitizeHTML = function (str) { const temp = document.createElement('div'); temp.textContent = str; return temp.innerHTML; }
const FarenheitToCelcius = function (temp) { if (settings.convertTemp) { return `${(Math.round((temp) * 9/5)) + 32} ℉`; }
return `${Math.round(temp)} ℃`; };
const getIcon = function (fetchData) { if (!settings.showIcon) return '';
const html = ` <div class="weather_icon"> <img src="https://openweathermap.org/img/wn/${sanitizeHTML(fetchData.weather[0].icon)}@2x.png"> </div>` return html; };
const getTemp = function (fetchData) { if (!settings.showTemp) return '';
const html = ` <h3 class="weather_temperature"> ${FarenheitToCelcius(sanitizeHTML(fetchData.main.temp))}; </h3>` return html; };
const getCity = function (fetchData) { if (!settings.showCity) return '';
const html = ` <h4 class="weather_city-name"> ${sanitizeHTML(fetchData.name)} </h4>` return html; };
const getConditions = function (fetchData) { if (!settings.showConditions) return '';
const html = ` <p class="weather_desc"> ${sanitizeHTML(fetchData.weather[0].description)} </p>` return html; };
const renderWeather = function (fetchData) { app.innerHTML = (` <div class="weather_container"> ${getIcon(fetchData)} ${getTemp(fetchData)} ${getCity(fetchData)} ${getConditions(fetchData)} </div> `) };
const renderNoWeather = function () { app.innerHTML = settings.noWeather; };
if (!settings.apiKeyIp || !settings.apiKeyWeather) { console.warn('Please provide an API key.'); return; }
fetch(`https://api.ipgeolocation.io/ipgeo?apiKey=${settings.apiKeyIp}`).then(function (response) { if (response.ok) { return response.json(); } else { return Promise.reject(response); } }).then(function (data) { return fetch(`https://api.openweathermap.org/data/2.5/weather?q=${data.city}&appid=${settings.apiKeyWeather}&units=metric`); }).then(function (response) { if (response.ok) { return response.json(); } else { return Promise.reject(response); } }).then(function (data) { renderWeather(data); }).catch(function (err) { console.warn(err); renderNoWeather(); }) }
getWeather({ apiKeyIp: 'd9f7add9f68440818a0659381720a532', apiKeyWeather: '5e995a3338fe2917188f0f98d08abcec', selector: '#app', convertTemp: false, noWeather: 'Unable to get weather data at this time. Sorry!', showIcon: true, showTemp: true, showCity: true, showConditions: true, });
|