import { Page } from "puppeteer-core" ; import Debug from "debug" ; import { QueueElement } from "./configuration" ; const debug = Debug ( "impftermin:booking" ) ; import { coloredError } from "./index" ; export async function bookAppointment ( page , queueEntry ) { if ( ! queueEntry . title || ! queueEntry . firstname || ! queueEntry . lastname || ! queueEntry . zip || ! queueEntry . city || ! queueEntry . street || ! queueEntry . streetnumber || ! queueEntry . mobile || ! queueEntry . email || ! queueEntry . earliestdate || ! queueEntry . latestdate ) { debug ( "Personal details incomplete, cannot book appointment." ) ; return false ; } var appointmentDates = new Array ( ) ; var dateNumber = 0 ; var linearDate = 0 ; const desiredEarliestDate = queueEntry . earliestdate ; const desiredEarliestLinearDate = parseInt ( desiredEarliestDate . substring ( 0 , 2 ) ) + parseInt ( desiredEarliestDate . substring ( 3 , 5 ) ) * 31 ; const desiredLatestDate = queueEntry . latestdate ; const desiredLatestLinearDate = parseInt ( desiredLatestDate . substring ( 0 , 2 ) ) + parseInt ( desiredLatestDate . substring ( 3 , 5 ) ) * 31 ; var desiredEarliestDateFound = false ; debug ( " " ) ; debug ( "Desired earliest Date %s" , desiredEarliestDate ) ; debug ( "Desired latest Date %s" , desiredLatestDate ) ; debug ( " " ) ; for ( const listElementTermin of await page . $$ ( "span" ) ) { const idName = await ( await listElementTermin . getProperty ( "innerText" ) ) ?. jsonValue < string > ( ) ; if ( idName && idName . includes ( ".," ) ) { dateNumber ++ ; appointmentDates [ dateNumber ] = idName ; if ( dateNumber % 2 !== 0 ) { debug ( "  " ) ; debug ( "Vacc date pair #%s" , ( dateNumber - ( dateNumber % 2 ) ) / 2 + 1 ) ; linearDate = parseInt ( idName . substring ( 5 , 7 ) ) + parseInt ( idName . substring ( 8 , 10 ) ) * 31 ; if ( linearDate >= desiredEarliestLinearDate && linearDate <= desiredLatestLinearDate ) { if ( ! desiredEarliestDateFound ) { debug ( "This is the earliest Date in desired range!" ) ; await listElementTermin . click ( { delay : 200 } ) ; desiredEarliestDateFound = true ; } } } } if ( idName && idName . includes ( "Uhr" ) ) { appointmentDates [ dateNumber ] = appointmentDates [ dateNumber ] . concat ( idName ) ; debug ( "Teiltermin: %s" , appointmentDates [ dateNumber ] ) ; } } if ( ! desiredEarliestDateFound ) { debug ( "Desired earliest date for first vacc shot not available yet" ) ; debug ( "Latest available date for first shot is %s" , appointmentDates [ dateNumber - 1 ] ) ; for ( const listElementTermin of await page . $$ ( "button" ) ) { const idName = await ( await listElementTermin . getProperty ( "innerText" ) ) ?. jsonValue < string > ( ) ; if ( idName && idName . includes ( "ABBRECHEN" ) ) { await listElementTermin . click ( { delay : 200 } ) ; break ; } } return false ; } await page . waitForTimeout ( 1000 ) ; for ( const listElementTermin of await page . $$ ( "button" ) ) { const idName = await ( await listElementTermin . getProperty ( "innerText" ) ) ?. jsonValue < string > ( ) ; if ( idName && idName . includes ( "AUSWÄHLEN" ) ) { await listElementTermin . click ( { delay : 200 } ) ; break ; } } const filldataButton = await page . $ ( "button.search-filter-button" ) ; await filldataButton ?. click ( { delay : 500 } ) ; for ( const listElementAnrede of await page . $$ ( "span" ) ) { const idName = await ( await listElementAnrede . getProperty ( "innerText" ) ) ?. jsonValue < string > ( ) ; if ( idName && idName . includes ( queueEntry . title ) ) { await listElementAnrede . click ( { delay : 200 } ) ; break ; } } await page . type ( "input[name='firstname']" , queueEntry . firstname ) ; await page . type ( "input[name='lastname']" , queueEntry . lastname ) ; await page . type ( "input[name='plz']" , queueEntry . zip ) ; await page . type ( "input[name='city']" , queueEntry . city ) ; await page . type ( "input[name='street']" , queueEntry . street ) ; await page . type ( "input[formcontrolname='housenumber']" , queueEntry . streetnumber ) ; await page . type ( "input[name='phone']" , queueEntry . mobile ) ; await page . type ( "input[name='notificationReceiver']" , queueEntry . email ) ; await page . waitForTimeout ( 1000 ) ; for ( const listElementTermin of await page . $$ ( "button" ) ) { const idName = await ( await listElementTermin . getProperty ( "innerText" ) ) ?. jsonValue < string > ( ) ; if ( idName && idName . includes ( "ÜBERNEHMEN" ) ) { await listElementTermin . click ( { delay : 200 } ) ; break ; } } const bookingButton = await page . $ ( "button.search-filter-button" ) ; await bookingButton ?. click ( { delay : 500 } ) ; debug ( " " ) ; debug ( "===========================================" ) ; debug ( "===========================================" ) ; debug ( "===== Appointment has been booked :-P =====" ) ; debug ( "===========================================" ) ; debug ( "===========================================" ) ; debug ( " " ) ; let i = 0 ; for ( const listElementBookedTermin of await page . $$ ( "span" ) ) { const idName = await ( await listElementBookedTermin . getProperty ( "innerText" ) ) ?. jsonValue < string > ( ) ; if ( idName && idName . includes ( "Uhr" ) ) { i ++ ; if ( i > 2 ) { break ; } else { debug ( "%s. vaccination" , i ) ; debug ( "Selected Appointment: %s" , idName ) ; debug ( " " ) ; } } } debug ( "===========================================" ) ; return true ; }