-
Notifications
You must be signed in to change notification settings - Fork 2
/
craw.js
129 lines (104 loc) · 4.13 KB
/
craw.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
var LOGIN_URL,
LOGIN_USERNAME,
LOGIN_PASSWORD,
FREE_TO_PLAY_URL,
FREE_GAMES_URL,
games;
var casper = require('casper').create({
viewportSize: {
width: 1024,
height: 768
},
verbose: true,
logLevel: 'warning'
});
if (!casper.cli.has('username') && !casper.cli.has('password')) {
casper.echo('Usage: $ casperjs craw.js --username=USERNAME --password=PASSWORD').exit(-1);
}
LOGIN_USERNAME = casper.cli.get('username');
LOGIN_PASSWORD = casper.cli.get('password');
LOGIN_URL = 'https://auth.api.sonyentertainmentnetwork.com/login.jsp';
FREE_GAMES_URL = 'https://store.sonyentertainmentnetwork.com/#!/en-us/free-games/cid=STORE-MSF77008-PSPLUSFREEGAMES';
FREE_TO_PLAY_URL = 'https://store.sonyentertainmentnetwork.com/#!/en-us/free-to-play/cid=STORE-MSF77008-PS3F2PPS3';
var URLS = [];
URLS.push(FREE_TO_PLAY_URL);
if (casper.cli.has('psn-plus-member')) {
URLS.push(FREE_GAMES_URL);
}
casper.start(LOGIN_URL, function() {
'use strict';
this.echo('Logging in...');
this.echo(this.getTitle());
this.fill('#signInForm', {
'j_username': LOGIN_USERNAME,
'j_password': LOGIN_PASSWORD
}, true);
});
casper.waitForUrl(/loginSuccess\.jsp$/, function() {
this.echo('Login successful!');
}, function() {
this.echo('Failed to login.', 'ERROR').exit(-1);
});
casper.each(URLS, function(self, url) {
self.thenOpen(url, function() {
'use strict';
this.echo(this.getTitle());
this.waitUntilVisible(".addToCartBtn", function() {
games = this.evaluate(function(){
var elements = document.querySelectorAll(".cellGridGameStandard");
games = [];
[].forEach.call(elements, function(element, index) {
if (!element.classList.contains('ownAlready')) {
games.push({
'id': element.id,
'title': element.querySelector(".cellTitle").innerHTML
});
}
});
return games;
});
if (games.length == 0) {
this.echo("No free donuts for you today, brotha!", 'ERROR').exit(-1);
}
this.echo("Found out " + games.length + " left FREE games!");
games.forEach(function(game, index) {
casper.echo("Title: " + game.title);
});
this.echo('Purchasing title "' + games[0].title + '".');
}, function() {
this.echo("No free donuts for you today, brotha!", 'ERROR').exit(-1);
}, 20000);
this.then(function() {
if (!games.length) {
this.exit();
}
if (!this.visible('#' + games[0].id + ' .addToCartBtn')) {
this.echo('This title is already on cart. Please remove it so I can purchase it.', 'error').exit(-1);
}
this.click('#' + games[0].id + ' .addToCartBtn');
this.waitForSelector("#_modal_content .showCartBtn", function(){
casper.click('#_modal_content .showCartBtn');
}, function() {
casper.log("Could not click on link to cart on the modal window after adding game to cart.")
}, 15000);
});
this.waitForSelector('.familyCellGridCart', function() {
this.click('a.proceedBtn');
}, function(){
this.log("Could not proceed with purchase once on cart.");
}, 15000);
this.waitForSelector("a.actionBtn", function(){
if (this.fetchText('.totalPrice') != '$0.00$0.00') {
this.log("Total Amount: " + this.fetchText('.totalPrice'));
this.echo("Wow! This order is NOT FREE! Stopping...", 'error').exit(-1);
}
this.echo("On Cart: " + this.fetchText('.firstCell .title'));
this.click('a.actionBtn');
});
this.waitForSelector('.orderSummary .receiptMsg', function() {
this.echo(this.fetchText('.orderSummary .receiptMsg'));
}, function(){
}, 15000);
});
});
casper.run();