Created
February 19, 2018 17:07
-
-
Save nighto/9945e4b22510e522cf726a756c9e4c11 to your computer and use it in GitHub Desktop.
Horários BHTrans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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