-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (66 loc) · 2.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require('express');
const chromium = require('chrome-aws-lambda');
if (process.env.NODE_ENV === 'development') require('dotenv').config();
const app = express();
const port = process.env.PORT || 8080;
app.get('/', async (req, res) => {
try {
const { cinemaId, showId, featureId } = req.query;
const { totalSeats, availableSeats } = await getSeats({ cinemaId, showId });
return res.send({ totalSeats, availableSeats });
} catch (error) {
console.log('TCL: error', error);
res.sendStatus(500);
}
});
app.listen(port, () => console.log('App listening on port ' + port));
console.log('TCL: process.env.CINEMARK_USERNAME', process.env.CINEMARK_USERNAME);
console.log('TCL: process.env.CINEMARK_PASSWORD', process.env.CINEMARK_PASSWORD);
const getSeats = async ({ cinemaId, showId }) => {
// const browser = await puppeteer.launch({
// args: ['--no-sandbox'],
// headless: process.env.NODE_ENV !== 'development',
// });
const browser = await chromium.puppeteer.launch({
executablePath: await chromium.executablePath,
args: ['--no-sandbox'],
headless: chromium.headless,
defaultViewport: chromium.defaultViewport,
});
const page = await browser.newPage();
if (process.env.NODE_ENV === 'development') await page.setViewport({ width: 1280, height: 720 });
const navigationPromise = page.waitForNavigation();
const requestURL = `https://tickets.cinemarkhoyts.com.ar/NSTicketing/?CinemaId=${cinemaId}&SessionId=${showId}&FeatureId=0`;
await page.goto(requestURL, {
waitUntil: 'networkidle0',
referer: 'no-referrer-when-downgrade',
});
await page.waitForSelector('#txtEmailAddress');
await page.focus('#txtEmailAddress');
await page.keyboard.type(process.env.CINEMARK_USERNAME);
await page.waitForSelector('.md-w-6 #txtPassword');
await page.focus('.md-w-6 #txtPassword');
await page.keyboard.type(process.env.CINEMARK_PASSWORD);
await page.waitForSelector('#logiform #btnLogin');
await page.click('#logiform #btnLogin');
await navigationPromise;
try {
const modalSelector = '.b-modal';
await page.waitForSelector(modalSelector, { timeout: 400 });
await page.evaluate((sel) => {
const elements = document.querySelectorAll(sel);
elements.forEach((element) => element.parentNode.removeChild(element));
}, modalSelector);
} catch (error) {}
await page.waitForSelector('div.categories-list > div.category:nth-child(2) a.ticket-price-reg');
await page.click('div.categories-list > div.category:nth-child(2) a.ticket-price-reg');
await page.waitForSelector('#btnNext');
await page.click('#btnNext');
await navigationPromise;
const available = await page.$$('a.available');
const availableSeats = available.length;
const singleSeats = await page.$$('.seat-single > a:not(.void)');
const totalSeats = singleSeats.length;
await browser.close();
return { totalSeats, availableSeats };
};