@ -1,3 +1,12 @@
const parseTemp = el => {
return parseFloat ( el . textContent )
}
const parsePrec = el => {
const text = el . textContent
if ( text === '- assenti -' ) return 0
return parseFloat ( text )
}
const scrapePage = async ( browser , url , isToday ) => {
const scrapePage = async ( browser , url , isToday ) => {
let result = { }
let result = { }
@ -6,51 +15,89 @@ const scrapePage = async (browser, url, isToday) => {
const weatherTable = await page . locator ( '.weather_table' ) . waitHandle ( )
const weatherTable = await page . locator ( '.weather_table' ) . waitHandle ( )
const startTime = isToday ? new Date ( ) . getHours ( ) + 2 : 0
const startTime = isToday ? new Date ( ) . getHours ( ) + 1 : 1
const endTime = 24
const endTime = 24
for ( let i = startTime ; i < endTime ; i ++ ) {
if ( isToday ) {
try {
const row = await weatherTable . $$ ( '.latest_detection' )
const fields = await row [ 1 ] ? . $$ ( 'td' )
const temp = await fields [ 2 ] . evaluate ( parseTemp )
const prec = await fields [ 3 ] . evaluate ( parsePrec )
result [ startTime - 1 ] = { temp , prec }
} catch ( error ) {
retult [ startTime - 1 ] = null
// console.log(error)
}
}
for ( let i = startTime ; i <= endTime ; i ++ ) {
try {
const selector = isToday
const selector = isToday
? ` .forecast_1h[data-dialogid=" ${ i } "] td `
? ` .forecast_1h[data-dialogid=" ${ i } "] td `
: ` .forecast_1h[data-dialogid=" ${ 1000 + i } "] td `
: ` .forecast_1h[data-dialogid=" ${ 1000 + i - 1 } "] td `
const fields = await weatherTable . $$ ( selector )
const fields = await weatherTable . $$ ( selector )
const temp = await fields [ 2 ] . evaluate ( el => parseFloat ( el . textContent ) )
const temp = await fields [ 2 ] . evaluate ( parseTemp )
const prec = await fields [ 3 ] . evaluate ( el => el . textContent )
const prec = await fields [ 3 ] . evaluate ( parsePrec )
result [ i ] = { temp , prec }
result [ i ] = { temp , prec }
} catch ( error ) {
retult [ i ] = null
// console.log(error)
}
}
}
await page . close ( )
await page . close ( )
return result
return result
}
}
const fetchILMeteoToday = async browser => {
return await scrapePage ( browser , 'https://www.ilmeteo.it/meteo/Pisa' , true )
const rearrangeResults = results => {
for ( let i = 0 ; i < 6 ; i ++ ) {
const midnight = results [ i ] [ 24 ]
delete results [ i ] [ 24 ]
results [ i + 1 ] [ 0 ] = midnight
}
}
const fetchILMeteoTomorrow = async browser => {
delete results [ 6 ] [ 24 ]
return await scrapePage (
browser ,
return results
'https://www.ilmeteo.it/meteo/Pisa/domani' ,
false ,
)
}
}
const fetchILMeteoDayAfterTomorrow = async browser => {
return await scrapePage (
const getDaySummary = day => {
browser ,
let minTemp = Number . MAX _VALUE
'https://www.ilmeteo.it/meteo/Pisa/dopodomani' ,
let maxTemp = Number . MIN _VALUE
false ,
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 => {
export default async browser => {
const [ today , tomorrow , dayAfterTomorrow ] = await Promise . all ( [
const url = text => ` https://www.ilmeteo.it/meteo/Pisa/ ${ text } `
fetchILMeteoToday ( browser ) ,
fetchILMeteoTomorrow ( browser ) ,
const results = rearrangeResults (
fetchILMeteoDayAfterTomorrow ( browser ) ,
await Promise . all ( [
] )
scrapePage ( browser , url ( '' ) , true ) ,
scrapePage ( browser , url ( 'domani' ) , false ) ,
scrapePage ( browser , url ( 'dopodomani' ) , false ) ,
scrapePage ( browser , url ( '3' ) , false ) ,
scrapePage ( browser , url ( '4' ) , false ) ,
scrapePage ( browser , url ( '5' ) , false ) ,
scrapePage ( browser , url ( '6' ) , false ) ,
] ) ,
)
return {
return {
today ,
today : results [ 0 ] ,
tomorrow ,
tomorrow : results [ 1 ] ,
dayAfterTomorrow ,
dayAfterTomorrow : results [ 2 ] ,
week : results . map ( getDaySummary ) ,
}
}
}
}