Skip to content

Instantly share code, notes, and snippets.

@nighto
Created February 19, 2018 17:07
Show Gist options
  • Save nighto/9945e4b22510e522cf726a756c9e4c11 to your computer and use it in GitHub Desktop.
Save nighto/9945e4b22510e522cf726a756c9e4c11 to your computer and use it in GitHub Desktop.
Horários BHTrans
// Extract timetables from BHTrans page
// for instance: http://servicosbhtrans.pbh.gov.br/bhtrans/e-servicos/S01F02-quadroHorarioResultado.asp?linha=SC01R
// fetch tables
let tables = document.querySelectorAll('table')
// work on each table
Array.from(tables).map((table, index) => {
// ignore first table
if (index) {
// fetch rows
let rows = table.children[0].children
// fetch cells
let parsed_table =
Array.from(rows)
.map(row => Array
.from(row.children)
.map(cell => cell.textContent.trim())
)
// strip out first column
parsed_table = parsed_table.map(row => row.slice(1))
// transpose table
let transposed_table = parsed_table[0].map((col, c) => {
// for each column, iterate all rows
return parsed_table.map((row, r) => {
return parsed_table[r][c]
})
})
// extract times
let times = []
transposed_table.map((row, indexRow) => {
transposed_table[indexRow].map((value, index) => {
if (index && value) {
times.push(`${transposed_table[indexRow][0]}:${value}`)
}
})
})
// returns data
console.log(times.join(', '))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment