Skip to content

Commit

Permalink
Add dry-run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrandda committed Jul 1, 2024
1 parent 50bbe07 commit e9e741c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Create `config.json` file from `config.json.sample` and complete with your prefe

`location`: a list of courts ordered by preference - [full list](https://tennis.paris.fr/tennis/jsp/site/Portal.jsp?page=tennisParisien&view=les_tennis_parisiens)

`date` a string representing a date formated D/M/YYYY
`date` a string representing a date formated D/M/YYYY, if not set the date 6 days in future is used

`hours` a list of hours ordered by preference

Expand All @@ -21,11 +21,22 @@ Create `config.json` file from `config.json.sample` and complete with your prefe

To pass the payement phase without trouble you need a "carnet de réservation", be carefull you need a "carnet" that maches with your `priceType` & `courtType` [combination](https://tennis.paris.fr/tennis/jsp/site/Portal.jsp?page=rate&view=les_tarifs) selected previously

To run this project locally, install the dependencies and run the script:
To run this project locally, install the dependencies

```sh
npm install
```

and run the script:

```sh
npm start
```

To test your configuration, you can run this project in dry-run mode. It will check court availability but no reservations will be made:

```sh
npm run start-dry
```

You can start script automatically using cron or equivalent
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const config = require('./config.json')
dayjs.extend(customParseFormat)

const bookTennis = async () => {
const DRY_RUN_MODE = process.argv.includes('--dry-run')
if (DRY_RUN_MODE) {
console.log('----- DRY RUN START -----')
console.log('Script lancé en mode DRY RUN. Afin de tester votre configuration, une recherche va être lancé mais AUCUNE réservation ne sera réalisée')
}

console.log(`${dayjs().format()} - Starting searching tennis`)
const browser = await chromium.launch({ headless: true, slowMo: 0, timeout: 120000 })

Expand All @@ -14,7 +20,7 @@ const bookTennis = async () => {
page.setDefaultTimeout(120000)
await page.goto('https://tennis.paris.fr/tennis/jsp/site/Portal.jsp?page=tennis&view=start&full=1')

await page.click('#button_suivi_inscription'),
await page.click('#button_suivi_inscription')
await page.fill('#username', config.account.email)
await page.fill('#password', config.account.password)
await page.click('#form-login >> button')
Expand All @@ -25,6 +31,7 @@ const bookTennis = async () => {
await page.waitForSelector('.main-informations')

try {
locationsLoop:
for (const location of config.locations) {
console.log(`${dayjs().format()} - Search at ${location}`)
await page.goto('https://tennis.paris.fr/tennis/jsp/site/Portal.jsp?page=recherche&view=recherche_creneau#!')
Expand All @@ -46,6 +53,7 @@ const bookTennis = async () => {
// wait until the results page is fully loaded before continue
await page.waitForLoadState('domcontentloaded')

let selectedHour
hoursLoop:
for (const hour of config.hours) {
const dateDeb = `[datedeb="${date.format('YYYY/MM/DD')} ${hour}:00:00"]`
Expand All @@ -63,6 +71,7 @@ const bookTennis = async () => {
if (!config.priceType.includes(priceType) || !config.courtType.includes(courtType)) {
continue
}
selectedHour = hour
await page.click(bookSlotButton)

break hoursLoop
Expand Down Expand Up @@ -102,6 +111,18 @@ const bookTennis = async () => {
})
await paymentMode.fill('existingTicket')

if (DRY_RUN_MODE) {
console.log(`${dayjs().format()} - Fausse réservation faite : ${location}`)
console.log(`pour le ${date.format('YYYY/MM/DD')} à ${selectedHour}h`)
console.log('----- DRY RUN END -----')
console.log("Pour réellement réserver un crénau, relancez le script sans le paramètre --dry-run")

await page.click('#previous')
await page.click('#btnCancelBooking')

break locationsLoop
}

const submit = await page.$('#order_select_payment_form #envoyer')
submit.evaluate(el => el.classList.remove('hide'))
await submit.click()
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"author": "Bertrand d'Aure",
"license": "MIT",
"scripts": {
"start": "node index.js"
"start": "node index.js",
"start-dry": "node index.js --dry-run"
},
"dependencies": {
"dayjs": "^1.11.9",
Expand Down

0 comments on commit e9e741c

Please sign in to comment.