-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
130 lines (116 loc) · 3.47 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import puppeteer from 'puppeteer-extra';
import * as fs from 'fs';
import axios from 'axios';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
import { executablePath } from 'puppeteer';
const TEMP_MAIL_URL = 'https://tempmailo.com/';
const ORELLY_REGISTER_URL =
'https://www.oreilly.com/start-trial/api/v1/registration/individual/';
const PASSWORD = 'Temp@123';
puppeteer.use(StealthPlugin());
const getEmailId = async () => {
try {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox'],
ignoreHTTPSErrors: true,
executablePath: executablePath(),
});
const page = await browser.newPage();
const url = TEMP_MAIL_URL;
let emailId = '';
await page.goto(url, { waitUntil: 'networkidle0' });
await page.waitForSelector('#i-email', { visible: true });
const emailIdBody = await page.evaluate(() =>
document.getElementById('i-email')
);
const title = await page.evaluate(() => document.title);
console.log('Title: ' + title);
console.log('emailIdBody: ' + emailIdBody);
if (typeof emailIdBody != 'undefined' && emailIdBody != null) {
emailId = '_value' in emailIdBody ? emailIdBody._value : 'temp@email.com';
}
await page.screenshot({
path: './screenshot.jpg',
type: 'jpeg',
fullPage: 'true',
});
await browser.close();
return emailId;
} catch (err) {
throw new Error(err);
}
};
const getRandomName = () => {
return (Math.random() + 1).toString(36).substring(7);
};
const createOReillyAccount = async () => {
const emailId = await getEmailId();
console.log('emailId: ' + emailId);
const headers = {
'accept-language': 'en-GB,en;q=0.9',
'content-type': 'application/json',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'sec-gpc': '1',
'Content-Type': 'application/json',
Referer: 'https://www.oreilly.com/',
'Referrer-Policy': 'strict-origin',
};
const data = {
email: emailId,
password: PASSWORD,
first_name: getRandomName(),
last_name: getRandomName(),
country: 'IN',
t_c_agreement: true,
contact: true,
path: '/start-trial/',
source: 'payments-client-register',
trial_length: 10,
};
try {
let response = await axios.post(ORELLY_REGISTER_URL, data, {
headers: headers,
});
console.log('Account created successfully');
console.log('Status: ' + response.status);
console.log('Data: ' + JSON.stringify(response.data));
let textContent = {};
textContent.emailId = emailId;
textContent.password = PASSWORD;
textContent.accountCreatedOn = new Date().toLocaleDateString();
fs.appendFile(
'account-details.txt',
'\n' + JSON.stringify(textContent),
(err) => {
if (err) {
console.log(
'Account created but got some error while saving data in file'
);
throw new Error(err);
}
console.log('Successfully saved!');
}
);
} catch (err) {
console.log('Failed to create OReilly account');
throw new Error(err);
}
};
let accountCreated = false;
let retryCount = 10;
while (!accountCreated & (retryCount > 0)) {
retryCount--;
try {
let response = await createOReillyAccount();
console.log('Successfully created OReilly account! Enjoy');
accountCreated = true;
} catch (err) {
console.log(
'There was some error while trying to create Orelly account. ',
err
);
}
}