Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%*
- async function twentyFourHoursFormat(t){
- const time = await t.split(' ')
- if (time[1] === 'AM') {
- return time[0]
- } else {
- const hours = await Number(time[0].split(':')[0]) + 12
- return `${hours}:${time[0].split(':')[1]}`
- }
- }
- async function currentWeather() {
- const city = 'city_name' // set you city
- const api_key = 'api_key' // get api key from weatherapi.com
- const days = 1
- const lang = 'en'
- const url = `https://api.weatherapi.com/v1/forecast.json?q=${city}&days=${days}&aqi=yes&lang=${lang}&key=${api_key}`;
- const response = await fetch(url).then(res => res.json())
- const cityName = await response.location.name
- const sunrise = await twentyFourHoursFormat(response.forecast.forecastday[0].astro.sunrise)
- const sunset = await twentyFourHoursFormat(response.forecast.forecastday[0].astro.sunset)
- const avgHumidity = await response.forecast.forecastday[0].day.avghumidity
- const avgDayTemp = await Math.round(response.forecast.forecastday[0].day.avgtemp_c)
- const maxDayTemp = await Math.round(response.forecast.forecastday[0].day.maxtemp_c)
- const minDayTemp = await Math.round(response.forecast.forecastday[0].day.mintemp_c)
- const conditionIcon = await response.forecast.forecastday[0].day.condition.icon.slice(2, )
- const conditionText = await response.forecast.forecastday[0].day.condition.text.toLowerCase()
- const rainChance = await Boolean(response.forecast.forecastday[0].day.daily_chance_of_rain) ? `rain chance ${response.forecast.forecastday[0].day.daily_chance_of_rain}%` : ''
- const snowChance = await Boolean(response.forecast.forecastday[0].day.daily_chance_of_snow) ? ` chance of snowfall
- ${response.forecast.forecastday[0].day.daily_chance_of_snow}%` : ''
- const currentConditionIcon = await response.current.condition.icon.slice(2, )
- const currentConditionText = await response.current.condition.text.toLowerCase()
- const feels = await Math.round(response.current.feelslike_c)
- const dateTime = await response.current.last_updated.split(' ')
- const dateFormat = dateTime[0].split('-').reverse().join('.')
- const pressure = await String((response.current.pressure_mb*0.75006375541921).toFixed())
- const currentTemp = await Math.round(response.current.temp_c)
- return `Weather for ${dateFormat} ${conditionText}, temp (max ${maxDayTemp}°C, avg ${avgDayTemp}°C, min ${minDayTemp}°C) ${rainChance} ${snowChance}\n\n > [!NOTE]- Weather now \n>Sunrise ${sunrise}, sunset ${sunset}.\n>\n>In ${cityName} at ${dateTime[1]} ${currentConditionText}, temp ${currentTemp}°C feels like ${feels}°C, pressure ${pressure} mmHg`
- }
- tR += await currentWeather()
- %>
Advertisement
Add Comment
Please, Sign In to add comment