-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
218 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const searchBox = document.querySelector(".search-box input"); | ||
const searchBtn = document.querySelector("#search"); | ||
const weatherIcon = document.querySelector(".weather-icon"); | ||
|
||
|
||
const weatherApiKey = 'b2fa3271ab55c9c76e1b2a2d1afd0478'; | ||
const weatherURL = `https://api.openweathermap.org/data/2.5/weather?units=metric&q=`; | ||
|
||
|
||
|
||
const imageApiKey ="2BsfBnNAfcAGF3oX4F_fRIlYnOXYBGYyJpeHfo8AWp4"; | ||
const imageURL = "https://api.unsplash.com/search/photos?page=1&query="; | ||
|
||
|
||
|
||
|
||
window.addEventListener('load', ()=>{ | ||
setTimeout(()=>{ | ||
document.querySelector("#preloader").style.display = "none"; | ||
},1500) | ||
}) | ||
|
||
|
||
//Weather API call | ||
async function checkWeather(city){ | ||
const response = await fetch(weatherURL + city + `&appid=${weatherApiKey}`); | ||
const data = await response.json(); | ||
|
||
|
||
if(response.status === 404){ | ||
document.querySelector(".error").style.display = 'block'; | ||
document.querySelector(".weather").style.display = "none"; | ||
document.getElementById("city-img").src = "images/weather.jpg"; | ||
} | ||
else{ | ||
|
||
await generateImage(city); | ||
|
||
//Update the weather data | ||
document.querySelector("#city").textContent = data.name; | ||
document.querySelector("#temp").textContent = Math.round(data.main.temp) +"°c"; | ||
document.querySelector(".humidity").textContent = data.main.humidity + '%'; | ||
document.querySelector(".wind").textContent = data.wind.speed + "Km/h"; | ||
|
||
|
||
//Change the Weather Icon | ||
const weatherCondition = data.weather[0].main; | ||
|
||
if(weatherCondition == 'Clear'){ | ||
weatherIcon.src = "images/clear.png" | ||
document.querySelector("#condition").textContent = data.weather[0].main; | ||
} | ||
else if(weatherCondition == 'Clouds'){ | ||
weatherIcon.src = "images/clouds.png" | ||
document.querySelector("#condition").textContent = data.weather[0].main; | ||
} | ||
else if(weatherCondition == 'Haze'){ | ||
weatherIcon.src = "images/drizzle.png" | ||
document.querySelector("#condition").textContent = data.weather[0].main; | ||
} | ||
else if(weatherCondition == 'Mist'){ | ||
weatherIcon.src = "images/mist.png" | ||
document.querySelector("#condition").textContent = data.weather[0].main; | ||
} | ||
else if(weatherCondition == 'Rain'){ | ||
weatherIcon.src = "images/rain.png" | ||
document.querySelector("#condition").textContent = data.weather[0].main; | ||
} | ||
else if(weatherCondition == 'Snow'){ | ||
weatherIcon.src = "images/snow.png" | ||
document.querySelector("#condition").textContent = data.weather[0].main; | ||
} | ||
|
||
document.querySelector(".weather").style.display = "block"; | ||
document.querySelector(".error").style.display = 'none'; | ||
|
||
} | ||
} | ||
|
||
//Default Call | ||
checkWeather("kolkata"); | ||
|
||
|
||
//Image API call | ||
async function generateImage(city){ | ||
|
||
const response = await fetch(imageURL + city + `&client_id=${imageApiKey}`); | ||
const data = await response.json(); | ||
|
||
document.getElementById("city-img").src = data.results[0].urls.full; | ||
|
||
} | ||
|
||
|
||
searchBtn.addEventListener('click', (e)=>{ | ||
|
||
checkWeather(searchBox.value); | ||
}) |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters