Skip to content

Instantly share code, notes, and snippets.

@rawnly
Last active October 15, 2018 14:55
Show Gist options
  • Save rawnly/b047c0b81369ebe78a004ab07b4f71e1 to your computer and use it in GitHub Desktop.
Save rawnly/b047c0b81369ebe78a004ab07b4f71e1 to your computer and use it in GitHub Desktop.
// You grab this data from the page
let hours = [5, 6, 7, 8]
let minutes = [
[15, 15, 05, 05],
[45, 35, 15, 15],
[null, 45, 25, 25],
[null, 55, 45, 30],
[null, null, 40, 40],
[null, null, 50, 45],
[null, null, 55, 55],
]
// The object
let calendar = {}
// Logic 🛠️
hours.forEach((item, column) => {
calendar[item] = [];
minutes.forEach(row => {
calendar[item].push(row[column])
})
})
/*
That should be the result
> calendar
{ '5': [ 15, 45, null, null, null, null, null ],
'6': [ 15, 35, 45, 55, null, null, null ],
'7': [ 5, 15, 25, 45, 40, 50, 55 ],
'8': [ 5, 15, 25, 30, 40, 45, 55 ] }
*/
const got = require('got');
const { JSDOM } = require('jsdom');
(async () => {
const { body } = await got("https://ttsl.pt/passageiros/horarios-de-ligacoes-fluviais/ligacao-barreiro-terreiro-do-paco/");
const { window: { document } } = new JSDOM(body);
const [t1, t2] = Array.from(document.getElementById('Dias úteis').children)
.filter(element => element.classList.contains('table-responsive'))
.map(tableContainer => {
const item = Array.from(tableContainer.children[0].children);
return { thead: item[0], tbody: item[1] }
})
const hours = { t1: {}, t2: {} }
const T1Hours = Array.from(t1.thead.children[0].children).map(item => item.innerHTML)
const T1Minutes = Array.from(t1.tbody.children).map(item => Array.from(item.children).map(item => {
let content = item.innerHTML;
return content.trim() !== '' ? content.replace(/\D/g, '') : null
}))
const T2Hours = Array.from(t2.thead.children[0].children).map(item => item.innerHTML)
const T2Minutes = Array.from(t2.tbody.children).map(item => Array.from(item.children).map(item => {
let content = item.innerHTML;
return content.trim() !== '' ? content.replace(/\D/g, '') : null
}))
T1Hours.forEach((item, column) => {
hours.t1[item] = [];
T1Minutes.forEach(row => {
hours.t1[item].push(row[column])
})
})
T2Hours.forEach((item, column) => {
hours.t2[item] = [];
T2Minutes.forEach(row => {
hours.t2[item].push(row[column])
})
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment