mirror of
https://github.com/jbowdre/lolz.git
synced 2024-11-12 17:32:19 +00:00
misc script cleanup
This commit is contained in:
parent
048fb6c332
commit
496afe149b
1 changed files with 84 additions and 89 deletions
173
tempest.html
173
tempest.html
|
@ -6,7 +6,7 @@
|
|||
<title>Weather Test</title>
|
||||
<script>
|
||||
// get ready to calculate relative time
|
||||
var units = {
|
||||
const units = {
|
||||
year : 24 * 60 * 60 * 1000 * 365,
|
||||
month : 24 * 60 * 60 * 1000 * 365/12,
|
||||
day : 24 * 60 * 60 * 1000,
|
||||
|
@ -14,120 +14,115 @@
|
|||
minute: 60 * 1000,
|
||||
second: 1000
|
||||
}
|
||||
var rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
|
||||
var getRelativeTime = (d1, d2 = new Date()) => {
|
||||
var elapsed = d1 - d2
|
||||
let rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
|
||||
let getRelativeTime = (d1, d2 = new Date()) => {
|
||||
let elapsed = d1 - d2
|
||||
for (var u in units)
|
||||
if (Math.abs(elapsed) > units[u] || u == 'second')
|
||||
if (Math.abs(elapsed) > units[u] || u === 'second')
|
||||
return rtf.format(Math.round(elapsed/units[u]), u)
|
||||
}
|
||||
|
||||
var tempRanges = [
|
||||
// set up temperature and rain ranges
|
||||
const tempRanges = [
|
||||
{ upper: 32, label: 'cold' },
|
||||
{ upper: 60, label: 'cool' },
|
||||
{ upper: 80, label: 'warm' },
|
||||
{ upper: Infinity, label: 'hot' }
|
||||
];
|
||||
|
||||
var rainRanges = [
|
||||
const rainRanges = [
|
||||
{ upper: 0.02, label: 'none' },
|
||||
{ upper: 0.2, label: 'light' },
|
||||
{ upper: 1.4, label: 'moderate' },
|
||||
{ upper: Infinity, label: 'heavy' }
|
||||
]
|
||||
|
||||
// weather code inspired by https://kris.omg.lol
|
||||
// fetch from API proxy
|
||||
fetch('https://paste.jbowdre.lol/tempest.json/raw')
|
||||
.then(res => res.json())
|
||||
.then(function(res){
|
||||
|
||||
// calculate age of last update
|
||||
updateTime = res.time;
|
||||
var updateTime = parseInt(updateTime);
|
||||
var updateTime = updateTime*1000;
|
||||
var updateTime = new Date(updateTime);
|
||||
updateAge = getRelativeTime(updateTime);
|
||||
|
||||
// parse data
|
||||
conditions = (res.conditions).toLowerCase();
|
||||
tempDiff = Math.abs(res.air_temperature - res.feels_like);
|
||||
temp = `${res.air_temperature}°f (${(((res.air_temperature - 32) * 5) / 9).toFixed(1)}°c)`;
|
||||
if (tempDiff >= 5) {
|
||||
temp += `, feels ${res.feels_like}°f (${(((res.feels_like - 32) *5) / 9).toFixed(1)}°c)`;
|
||||
}
|
||||
tempLabel = (tempRanges.find(range => res.feels_like < range.upper)).label;
|
||||
humidity = `${res.relative_humidity}% humidity`;
|
||||
wind = `${res.wind_gust}mph (${(res.wind_gust*1.609344).toFixed(1)}kph) from ${(res.wind_direction_cardinal).toLowerCase()}`;
|
||||
precipLabel = (rainRanges.find(range => res.precip_accum_local_day < range.upper)).label;
|
||||
if (res.precip_accum_local_day == 0) {
|
||||
precipToday = 'no rain today';
|
||||
} else {
|
||||
precipToday = `${res.precip_accum_local_day}" rain today`;
|
||||
}
|
||||
pressureTrend = res.pressure_trend;
|
||||
pressure = `${res.station_pressure}inhg and ${pressureTrend}`;
|
||||
icon = res.icon;
|
||||
|
||||
// display data
|
||||
document.getElementById('time').innerHTML = updateAge;
|
||||
document.getElementsByClassName('fa-cloud-sun-rain')[0].classList = CLASS_MAP_WX[icon];
|
||||
document.getElementById('conditions').innerHTML = conditions;
|
||||
document.getElementsByClassName('fa-temperature-half')[0].classList = CLASS_MAP_TEMP[tempLabel];
|
||||
document.getElementById('temp').innerHTML = temp;
|
||||
document.getElementById('humidity').innerHTML = humidity;
|
||||
document.getElementById('wind').innerHTML = wind;
|
||||
document.getElementsByClassName('fa-droplet-slash')[0].classList = CLASS_MAP_RAIN[precipLabel];
|
||||
document.getElementById('precipToday').innerHTML = precipToday;
|
||||
document.getElementsByClassName('fa-arrow-right-long')[0].classList = CLASS_MAP_PRESS[pressureTrend];
|
||||
document.getElementById('pressure').innerHTML = pressure;
|
||||
});
|
||||
|
||||
// change the pressure icon
|
||||
// maps for selecting icons
|
||||
const CLASS_MAP_PRESS = {
|
||||
'steady': 'fa-solid fa-arrow-right-long',
|
||||
'rising': 'fa-solid fa-arrow-trend-up',
|
||||
'falling': 'fa-solid fa-arrow-trend-down'
|
||||
'steady': 'fa-solid fa-arrow-right-long',
|
||||
'rising': 'fa-solid fa-arrow-trend-up',
|
||||
'falling': 'fa-solid fa-arrow-trend-down'
|
||||
}
|
||||
|
||||
// change the rainfall icon
|
||||
const CLASS_MAP_RAIN = {
|
||||
'none': 'fa-solid fa-droplet-slash',
|
||||
'light': 'fa-solid fa-glass-water-droplet',
|
||||
'moderate': 'fa-solid fa-glass-water',
|
||||
'heavy': 'fa-solid fa-bucket'
|
||||
}
|
||||
|
||||
// change the temperature icon
|
||||
const CLASS_MAP_TEMP = {
|
||||
'hot': 'fa-solid fa-thermometer-full',
|
||||
'warm': 'fa-solid fa-thermometer-half',
|
||||
'cool': 'fa-solid fa-thermometer-quarter',
|
||||
'cold': 'fa-solid fa-thermometer-empty'
|
||||
'hot': 'fa-solid fa-thermometer-full',
|
||||
'warm': 'fa-solid fa-thermometer-half',
|
||||
'cool': 'fa-solid fa-thermometer-quarter',
|
||||
'cold': 'fa-solid fa-thermometer-empty'
|
||||
}
|
||||
const CLASS_MAP_WX = {
|
||||
'clear-day': 'fa-solid fa-sun',
|
||||
'clear-night': 'fa-solid fa-moon',
|
||||
'cloudy': 'fa-solid fa-cloud',
|
||||
'foggy': 'fa-solid fa-cloud-showers-smog',
|
||||
'partly-cloudy-day': 'fa-solid fa-clouds-sun',
|
||||
'partly-cloudy-night': 'fa-solid fa-clouds-moon',
|
||||
'possibly-rainy-day': 'fa-solid fa-cloud-sun-rain',
|
||||
'possibly-rainy-night': 'fa-solid fa-cloud-moon-rain',
|
||||
'possibly-sleet-day': 'fa-solid fa-cloud-meatball',
|
||||
'possibly-sleet-night': 'fa-solid fa-cloud-moon-rain',
|
||||
'possibly-snow-day': 'fa-solid fa-snowflake',
|
||||
'possibly-snow-night': 'fa-solid fa-snowflake',
|
||||
'possibly-thunderstorm-day': 'fa-solid fa-cloud-bolt',
|
||||
'possibly-thunderstorm-night': 'fa-solid fa-cloud-bolt',
|
||||
'rainy': 'fa-solid fa-cloud-showers-heavy',
|
||||
'sleet': 'fa-solid fa-cloud-rain',
|
||||
'snow': 'fa-solid fa-snowflake',
|
||||
'thunderstorm': 'fa-solid fa-cloud-bolt',
|
||||
'windy': 'fa-solid fa-wind',
|
||||
}
|
||||
|
||||
// change the weather icon
|
||||
const CLASS_MAP_WX = {
|
||||
'clear-day': 'fa-solid fa-sun',
|
||||
'clear-night': 'fa-solid fa-moon',
|
||||
'cloudy': 'fa-solid fa-cloud',
|
||||
'foggy': 'fa-solid fa-cloud-showers-smog',
|
||||
'partly-cloudy-day': 'fa-solid fa-clouds-sun',
|
||||
'partly-cloudy-night': 'fa-solid fa-clouds-moon',
|
||||
'possibly-rainy-day': 'fa-solid fa-cloud-sun-rain',
|
||||
'possibly-rainy-night': 'fa-solid fa-cloud-moon-rain',
|
||||
'possibly-sleet-day': 'fa-solid fa-cloud-meatball',
|
||||
'possibly-sleet-night': 'fa-solid fa-cloud-moon-rain',
|
||||
'possibly-snow-day': 'fa-solid fa-snowflake',
|
||||
'possibly-snow-night': 'fa-solid fa-snowflake',
|
||||
'possibly-thunderstorm-day': 'fa-solid fa-cloud-bolt',
|
||||
'possibly-thunderstorm-night': 'fa-solid fa-cloud-bolt',
|
||||
'rainy': 'fa-solid fa-cloud-showers-heavy',
|
||||
'sleet': 'fa-solid fa-cloud-rain',
|
||||
'snow': 'fa-solid fa-snowflake',
|
||||
'thunderstorm': 'fa-solid fa-cloud-bolt',
|
||||
'windy': 'fa-solid fa-wind',
|
||||
}
|
||||
// fetch data from pastebin
|
||||
fetch('https://paste.jbowdre.lol/tempest.json/raw')
|
||||
.then(res => res.json())
|
||||
.then(function(res){
|
||||
|
||||
// calculate age of last update
|
||||
let updateTime = res.time;
|
||||
updateTime = parseInt(updateTime);
|
||||
updateTime = updateTime*1000;
|
||||
updateTime = new Date(updateTime);
|
||||
let updateAge = getRelativeTime(updateTime);
|
||||
|
||||
// parse data
|
||||
let conditions = (res.conditions).toLowerCase();
|
||||
let tempDiff = Math.abs(res.air_temperature - res.feels_like);
|
||||
let temp = `${res.air_temperature}°f (${(((res.air_temperature - 32) * 5) / 9).toFixed(1)}°c)`;
|
||||
if (tempDiff >= 5) {
|
||||
temp += `, feels ${res.feels_like}°f (${(((res.feels_like - 32) *5) / 9).toFixed(1)}°c)`;
|
||||
}
|
||||
let tempLabel = (tempRanges.find(range => res.feels_like < range.upper)).label;
|
||||
let humidity = `${res.relative_humidity}% humidity`;
|
||||
let wind = `${res.wind_gust}mph (${(res.wind_gust*1.609344).toFixed(1)}kph) from ${(res.wind_direction_cardinal).toLowerCase()}`;
|
||||
let precipLabel = (rainRanges.find(range => res.precip_accum_local_day < range.upper)).label;
|
||||
let precipToday;
|
||||
if (res.precip_accum_local_day === 0) {
|
||||
precipToday = 'no rain today';
|
||||
} else {
|
||||
precipToday = `${res.precip_accum_local_day}" rain today`;
|
||||
}
|
||||
let pressureTrend = res.pressure_trend;
|
||||
let pressure = `${res.station_pressure}inhg and ${pressureTrend}`;
|
||||
let icon = res.icon;
|
||||
|
||||
// display data
|
||||
document.getElementById('time').innerHTML = updateAge;
|
||||
document.getElementById('conditions').innerHTML = conditions;
|
||||
document.getElementById('temp').innerHTML = temp;
|
||||
document.getElementById('humidity').innerHTML = humidity;
|
||||
document.getElementById('wind').innerHTML = wind;
|
||||
document.getElementById('precipToday').innerHTML = precipToday;
|
||||
document.getElementById('pressure').innerHTML = pressure;
|
||||
// update icons
|
||||
document.getElementsByClassName('fa-cloud-sun-rain')[0].classList = CLASS_MAP_WX[icon];
|
||||
document.getElementsByClassName('fa-temperature-half')[0].classList = CLASS_MAP_TEMP[tempLabel];
|
||||
document.getElementsByClassName('fa-droplet-slash')[0].classList = CLASS_MAP_RAIN[precipLabel];
|
||||
document.getElementsByClassName('fa-arrow-right-long')[0].classList = CLASS_MAP_PRESS[pressureTrend];
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
Loading…
Reference in a new issue