This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
146 lines (137 loc) · 3.23 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env node
var readline = require('linebyline');
var wdio = require('wdio');
var selenium = require('selenium-standalone');
var chalk = require('chalk');
var meow = require('meow');
var inquirer = require('inquirer');
var includes = require('lodash.includes');
// init variable
var user = {};
var freeInMultipleLangage = [
'Gratuit',
'Free',
'Kostenlos',
'Gratuito',
'Gratis',
'無料',
'무료',
'Bezpłatny',
'Бесплатно',
'Ücretsiz',
'免费',
'免費',
];
var options = {
desiredCapabilities: {
browserName: 'chrome',
},
};
var browser = wdio.getBrowser(options);
var listUrlCoupon = [];
var prompt = [{
type: 'input',
message: 'Email ? ',
name: 'email',
}, {
type: 'password',
message: 'Password ? ',
name: 'password',
},
{
type: 'input',
message: 'Filename',
name: 'filename',
},
];
// for --help
meow(`
Usage
$ add-free-course-udemy
`);
console.log(chalk.blue('Enter your information : '));
inquirer.prompt(prompt).then(function (answers) {
user = answers;
start();
});
function start() {
readCoupon();
// init selenium server
selenium.start(function (err) {
if (err) {
console.log(chalk.red(err));
return;
}
// init browser
wdio.run(initAddCoupon, function (err) {
if (err) {
console.log(chalk.red(err));
}
browser.end();
process.exit();
});
});
}
function initAddCoupon() {
console.log(chalk.green('begin program!'));
browser.init();
browser.setViewportSize({ width: 1366, height: 768 });
browser.url('https://www.udemy.com');
browser.click('.dropdown--login');
// wait input login
while (!browser.isVisible('.emailinput')) {
browser.pause(500);
}
// login
browser.addValue('.emailinput', user.email);
browser.addValue('.textinput', user.password);
// wait for safety
browser.pause(2000);
browser.click('.submit-row .btn-primary');
browser.pause(2000);
iterate();
}
/**
* for each link, add course on account if course is free
*/
function iterate() {
console.log(chalk.blue(`begin with ${listUrlCoupon.length} urls`));
while (listUrlCoupon.length > 0) {
var urlCoupon = listUrlCoupon.pop();
console.log(`current Url : ${urlCoupon}`);
browser.url(urlCoupon);
browser.pause(2500);
// if course already buy
if (browser.isVisible('.course-cta--buy')) {
// if course is free
var textPrice = browser.getText('.price-text__current') || '';
textPrice = textPrice.trim();
try {
textPrice = /[^\s]+$/.exec(textPrice)[0];
} catch (err) {
console.log(chalk.red(err));
}
if (includes(freeInMultipleLangage, textPrice)) {
browser.click('.course-cta--buy');
// wait end buy
while (browser.getUrl() === urlCoupon) {
browser.pause(500);
}
// wait for safety
browser.pause(2500);
}
}
}
// end of program
console.log(chalk.green('End of program, all url used!'));
}
// read all url in file
function readCoupon() {
var rl = readline(`./${user.filename}`);
rl.on('line', function (line, lineCount, byteCount) {
listUrlCoupon.push(line);
}).on('error', function (e) {
console.error(chalk.red(e));
process.exit();
});
}