Last active
February 9, 2018 07:00
-
-
Save kerbyfc/9b4e79d5a7ac4a2ad0b839fca2e0581b to your computer and use it in GitHub Desktop.
На случай если забыл заказать себе обед на портале
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* INSTALLATION | |
* 1. Install node (https://nodejs.org/en/download/package-manager or https://github.com/creationix/nvm#installation) | |
* 2. $ cd ~/ && mkdir -p makeOrder && npm init -y && npm install moment git+https://github.com/GoogleChrome/puppeteer#ddc59b247282774ccc53e3cc925efc30d4e25675 | |
* 3. $ wget --no-check-certificate https://gist.githubusercontent.com/kerbyfc/9b4e79d5a7ac4a2ad0b839fca2e0581b/raw/1d3d23b4130a86d3fb2dbdf0b046db152e445c78/makeOrder.js | |
* 5. $ crontab -e # add line 20 13 * * * node ~/makeOrder.js | tee ~/makeOrder.log' | |
* 4. edit 'credentials' and 'products' variables in makeOrder script | |
*/ | |
const puppeteer = require('puppeteer'); | |
const moment = require('moment'); | |
const credentials = { | |
username: 'lukin', | |
password: '' | |
}; | |
// [count, product][] | |
const products = [ | |
[5, 'Активия питьевая без'] | |
]; | |
(async () => { | |
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']}) | |
const page = await browser.newPage(); | |
const day = moment().day(); | |
const orderDay = day < 5 ? (day + 1) : 8; // 8 = next monday | |
const headers = new Map(); | |
headers.set( | |
'Authorization', | |
`Basic ${new Buffer(`${credentials.username}:${credentials.password}`).toString('base64')}` | |
); | |
await page.setExtraHTTPHeaders(headers); | |
await page.authenticate(credentials); | |
await page.goto('http://portal.bifit.int/portal/user/dinner/user/index.jsf'); | |
await page.click(`.tableMenu > tbody > tr:nth-child(${orderDay}) a`); | |
await page.waitForNavigation(); | |
for (let productIndex = 0; productIndex < products.length; productIndex++) { | |
const [count, product] = products[productIndex]; | |
for (let i = 0; i < count; i++) { | |
const button = await page.waitForXPath(`//*[contains(text(), '${product}')]/parent::*/*/*/*/*/*/*[@alt='Увеличить']`); | |
await button.click(); | |
} | |
} | |
await page.screenshot({ | |
path: './order.png', | |
fullPage: true | |
}); | |
page.on('dialog', async (dialog) => { | |
await dialog.accept(); | |
setTimeout(() => { | |
browser.close(); | |
}, 500); | |
}); | |
page.click('[id="FORM:confirm"]'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment