Skip to content

Instantly share code, notes, and snippets.

@undirectlookable
Created March 21, 2018 10:37
Show Gist options
  • Save undirectlookable/c1bd07c81018e6ebc8ec473053d9996d to your computer and use it in GitHub Desktop.
Save undirectlookable/c1bd07c81018e6ebc8ec473053d9996d to your computer and use it in GitHub Desktop.
AirNow.gov 24 hours AQI table
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>AQI (airnow data)</title>
<script>
var city = location.hash.substr(1) || 'Beijing'
const aqiCats = [
'Good',
'Moderate',
'Unhealthy for Sensitive Groups',
'Unhealthy',
'Very Unhealthy',
'Hazardous'
]
fetch('https://www.dosairnowdata.org/dos/AllPosts24Hour.json').then((res) => {
res.json().then((json) => {
const data = json[city].monitors[1]
const startDate = new Date(data.beginTimeLT)
var html = `<h2>${city} AQI</h2>`
html += '<table style="width:100%;font-size:12px;">'
html += '<thead><tr><td>Time</td><td>PM2.5</td><td>AQI</td><td>Level</td></tr></thead>'
html += '<tbody>'
html += data.aqi.map((val, i) => {
const valDate = new Date(startDate.getTime() + i * 3600 * 1000)
const conc = data.conc[i] || '-'
const cat = data.aqiCat[i]
const catText = cat ? `${cat} - ${aqiCats[data.aqiCat[i]-1]}` : `-`
return `<tr><td>${valDate.getDate()}/${valDate.getMonth()+1} ${('0' + valDate.getHours()).slice(-2)}:00</td><td>${conc}</td><td>${val || '-'}</td><td>${catText}</td></tr>`
}).reverse().join('')
html += '</tbody></table>'
document.body.innerHTML = html
})
})
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment