started working on 3Bmeteo
parent
ea45e8832d
commit
4ddc0ba163
@ -0,0 +1,87 @@
|
||||
const parseTemp = el => {
|
||||
return parseFloat(el.textContent)
|
||||
}
|
||||
const parsePrec = el => {
|
||||
const text = el.textContent
|
||||
if (text === ' assenti\n ') return 0
|
||||
|
||||
//TODO not 100% sure that this would work as it was never tested
|
||||
return parseFloat(text)
|
||||
}
|
||||
|
||||
const scrapePage = async (browser, url, isToday) => {
|
||||
let result = {}
|
||||
|
||||
const page = await browser.newPage()
|
||||
await page.goto(url)
|
||||
|
||||
const tablePrevisioni = await page
|
||||
.locator('.table-previsioni-ora')
|
||||
.waitHandle()
|
||||
const rows = await tablePrevisioni.$$('.row-table.noPad')
|
||||
|
||||
const startTime = isToday ? new Date().getHours() : 0
|
||||
const endTime = 23
|
||||
|
||||
for (let i = startTime; i <= endTime; i++) {
|
||||
try {
|
||||
const row = rows[rows.length - 1 - (endTime - i)]
|
||||
// console.log(await row.evaluate(el => el.children))
|
||||
const [rowLeft, rowRight] = await row.$$(':scope > div')
|
||||
|
||||
const codeDiv = (await (await rowLeft.$('.row-table')).$$('div'))[2]
|
||||
const [tempDiv, precDiv] = await (
|
||||
await rowRight.$('.row-table')
|
||||
).$$('div')
|
||||
|
||||
const code = await codeDiv.evaluate(el => el.textContent.trim())
|
||||
const temp = await (await tempDiv.$('span')).evaluate(parseTemp)
|
||||
const prec = await (await precDiv.$('span')).evaluate(parsePrec)
|
||||
result[i] = { temp, prec, code }
|
||||
} catch (error) {
|
||||
result[i] = null
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
await page.close()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
const getDaySummary = day => {
|
||||
let minTemp = Number.MAX_VALUE
|
||||
let maxTemp = Number.MIN_VALUE
|
||||
let totPrec = 0
|
||||
for (const h in day) {
|
||||
minTemp = Math.min(minTemp, day[h].temp)
|
||||
maxTemp = Math.max(maxTemp, day[h].temp)
|
||||
totPrec += day[h].prec
|
||||
}
|
||||
return {
|
||||
minTemp,
|
||||
maxTemp,
|
||||
totPrec,
|
||||
}
|
||||
}
|
||||
|
||||
export default async browser => {
|
||||
const url = text => `https://www.3bmeteo.com/meteo/pisa/${text}`
|
||||
|
||||
const results = await Promise.all([
|
||||
scrapePage(browser, url(''), true),
|
||||
scrapePage(browser, url('1'), false),
|
||||
scrapePage(browser, url('2'), false),
|
||||
scrapePage(browser, url('3'), false),
|
||||
scrapePage(browser, url('4'), false),
|
||||
scrapePage(browser, url('5'), false),
|
||||
scrapePage(browser, url('6'), false),
|
||||
])
|
||||
|
||||
return {
|
||||
today: results[0],
|
||||
tomorrow: results[1],
|
||||
dayAfterTomorrow: results[2],
|
||||
week: results.map(getDaySummary),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue