|
| 1 | +// api.openweathermap.org/data/2.5/weather?q={city name}&appid={your api key} |
| 2 | + |
| 3 | +const weatherApi = { |
| 4 | + key: "bab281d79e5f1e9755a68d754cc313e7", |
| 5 | + baseUrl: "https://api.openweathermap.org/data/2.5/weather", |
| 6 | +} |
| 7 | + |
| 8 | +const searchInputBox = document.getElementById('input-box'); |
| 9 | + |
| 10 | +// Event Listener Function on keypress |
| 11 | +searchInputBox.addEventListener('keypress', (event) => { |
| 12 | + |
| 13 | + if(event.keyCode == 13) { |
| 14 | + getWeatherReport(searchInputBox.value); |
| 15 | + document.querySelector('.weather-body').style.display = "block"; |
| 16 | + } |
| 17 | + |
| 18 | +}); |
| 19 | + |
| 20 | +// Get Weather Report |
| 21 | +function getWeatherReport(city) { |
| 22 | + fetch(`${weatherApi.baseUrl}?q=${city}&appid=${weatherApi.key}&units=metric`) |
| 23 | + .then(weather => { |
| 24 | + return weather.json(); |
| 25 | + }).then(showWeatherReport); |
| 26 | +} |
| 27 | + |
| 28 | +// Show Weather Report |
| 29 | +function showWeatherReport(weather){ |
| 30 | + |
| 31 | + let city = document.getElementById('city'); |
| 32 | + city.innerText = `${weather.name}, ${weather.sys.country}`; |
| 33 | + |
| 34 | + let temperature = document.getElementById('temp'); |
| 35 | + temperature.innerHTML = `${Math.round(weather.main.temp)}°C`; |
| 36 | + |
| 37 | + let minMaxTemp = document.getElementById('min-max'); |
| 38 | + minMaxTemp.innerHTML = `${Math.floor(weather.main.temp_min)}°C (min)/ ${Math.ceil(weather.main.temp_max)}°C (max) `; |
| 39 | + |
| 40 | + let weatherType = document.getElementById('weather'); |
| 41 | + weatherType.innerText = `${weather.weather[0].main}`; |
| 42 | + |
| 43 | + let date = document.getElementById('date'); |
| 44 | + let todayDate = new Date(); |
| 45 | + date.innerText = dateManage(todayDate); |
| 46 | + |
| 47 | + let weather_iconEl = document.getElementById('weather-icon'); |
| 48 | + weather_iconEl.innerHTML = `<img src="http://openweathermap.org/img/wn/${weather.weather[0].icon}@2x.png"> |
| 49 | + <div class="weather" id="weather">${weather.weather[0].main}</div> |
| 50 | + ` |
| 51 | + let cityname = `${weather.name}` ; |
| 52 | + document.body.style.background = "url('https://source.unsplash.com/1600x900/?" + cityname +"') no-repeat" |
| 53 | + document.body.style.backgroundSize = "cover" |
| 54 | +} |
| 55 | + |
| 56 | +// Date manage |
| 57 | +function dateManage(dateArg) { |
| 58 | + |
| 59 | + let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; |
| 60 | + |
| 61 | + let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; |
| 62 | + |
| 63 | + let year = dateArg.getFullYear(); |
| 64 | + let month = months[dateArg.getMonth()]; |
| 65 | + let date = dateArg.getDate(); |
| 66 | + let day = days[dateArg.getDay()]; |
| 67 | + |
| 68 | + return `${date} ${month} (${day}), ${year}`; |
| 69 | +} |
| 70 | + |
0 commit comments