talraash

Untitled

Jan 22nd, 2024
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. <%*
  2. async function twentyFourHoursFormat(t){
  3. const time = await t.split(' ')
  4. if (time[1] === 'AM') {
  5. return time[0]
  6. } else {
  7. const hours = await Number(time[0].split(':')[0]) + 12
  8. return `${hours}:${time[0].split(':')[1]}`
  9. }
  10. }
  11. async function currentWeather() {
  12. const city = 'city_name' // set you city
  13. const api_key = 'api_key' // get api key from weatherapi.com
  14. const days = 1
  15. const lang = 'en'
  16. const url = `https://api.weatherapi.com/v1/forecast.json?q=${city}&days=${days}&aqi=yes&lang=${lang}&key=${api_key}`;
  17. const response = await fetch(url).then(res => res.json())
  18. const cityName = await response.location.name
  19. const sunrise = await twentyFourHoursFormat(response.forecast.forecastday[0].astro.sunrise)
  20. const sunset = await twentyFourHoursFormat(response.forecast.forecastday[0].astro.sunset)
  21. const avgHumidity = await response.forecast.forecastday[0].day.avghumidity
  22. const avgDayTemp = await Math.round(response.forecast.forecastday[0].day.avgtemp_c)
  23. const maxDayTemp = await Math.round(response.forecast.forecastday[0].day.maxtemp_c)
  24. const minDayTemp = await Math.round(response.forecast.forecastday[0].day.mintemp_c)
  25. const conditionIcon = await response.forecast.forecastday[0].day.condition.icon.slice(2, )
  26. const conditionText = await response.forecast.forecastday[0].day.condition.text.toLowerCase()
  27. const rainChance = await Boolean(response.forecast.forecastday[0].day.daily_chance_of_rain) ? `rain chance ${response.forecast.forecastday[0].day.daily_chance_of_rain}%` : ''
  28. const snowChance = await Boolean(response.forecast.forecastday[0].day.daily_chance_of_snow) ? ` chance of snowfall
  29. ${response.forecast.forecastday[0].day.daily_chance_of_snow}%` : ''
  30. const currentConditionIcon = await response.current.condition.icon.slice(2, )
  31. const currentConditionText = await response.current.condition.text.toLowerCase()
  32. const feels = await Math.round(response.current.feelslike_c)
  33. const dateTime = await response.current.last_updated.split(' ')
  34. const dateFormat = dateTime[0].split('-').reverse().join('.')
  35. const pressure = await String((response.current.pressure_mb*0.75006375541921).toFixed())
  36. const currentTemp = await Math.round(response.current.temp_c)
  37. 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`
  38. }
  39. tR += await currentWeather()
  40. %>
Advertisement
Add Comment
Please, Sign In to add comment