-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom.js
163 lines (154 loc) · 5 KB
/
phantom.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var webpage = require('webpage');
var page = webpage.create();
// Global loading flog
var loading = false;
// Global load jquery flag
var loadJQ = false;
// Credentials file
var config = require('./config');
page.onLoadStarted = function() {
loading = true;
loadJQ = true;
};
page.onLoadFinished = function() {
loading = false;
};
// Step index
var index = 0;
var data = {};
var steps = [
function() {
page.open("https://www.att.com");
},
function() {
// Enter credentials
page.evaluate(function(user, password){
$('form#ssoLoginForm input#userid').val(user);
$('form#ssoLoginForm input#userPassword').val(password);
}, config.user, config.password);
},
function() {
// Enter login
page.evaluate(function() {
$('form#ssoLoginForm').submit();
});
},
function() {
// Hack to wait for passcode field to appear
var result = page.evaluate(function() {
return document.querySelector("form input#passcode0");
});
if (!result) {
index--;
} else {
// Enter passcode
page.evaluate(function(passcode) {
$('form input#passcode0').val(passcode);
}, config.passcode);
}
},
function() {
// Login
page.evaluate(function() {
$('.btnRt .button.small.primary').click()
})
},
function() {
// Go to bill detail page
page.open("https://www.att.com/olam/passthroughAction.myworld?actionType=ViewBillDetails");
},
function() {
// Hack to wait for report
var result = page.evaluate(function() {
return $(".bPOD-bill-title.PadBot3imp.MarTop10:nth(1)");
});
if (!result) {
index--;
} else {
// Following code is compiled from coffeescript, may be confusing
data.lines = page.evaluate(function() {
var SUBTITLE_REGEX, bill, number, numberTitle, ref, ref1, ref2, subGroup;
bill = {};
// Regex for matching item title
SUBTITLE_REGEX = /(Monthly|Surcharges|Government|Data|Equipment|International|Smartphone|iPhone|30GB|National|Access)/;
// Div containing the line number
numberTitle = $('.bPOD-bill-title.PadBot3imp.MarTop10:nth(1)').next();
// Srapge until it cannot find the line number div
while (numberTitle) {
// Scrape the line number
number = (ref = numberTitle.find('a')) != null ? (ref1 = ref.text()) != null ? (ref2 = ref1.match(/([A-Z -]+)([0-9\-]+)/)) != null ? ref2[2] : void 0 : void 0 : void 0;
if (!number) {
break;
}
// Bill table for the line
subGroup = numberTitle.next();
bill[number] = {};
subGroup.find('.sub-group-title').each(function(index, elem) {
var ref3, title;
title = (ref3 = $(elem).find('a').text().match(SUBTITLE_REGEX)) != null ? ref3[1].toLowerCase() : void 0;
if (title === 'monthly') {
// Go into details of monthly charges
return $(elem).next().find('.botDotBorder, .BotSolidBorder').each(function(index, elem) {
var ref4;
title = (ref4 = $(elem).find('.accRow:nth(0)').text().match(SUBTITLE_REGEX)) != null ? ref4[1].toLowerCase() : void 0;
return bill[number][title] = parseFloat($(elem).find('.accRow:nth(1)').text().replace(/( |\$|\n|\t)/g, '').replace(/−/g, '-'));
});
} else {
// Otherwise just save the amount with title
return bill[number][title] = parseFloat($(elem).find('.flipper').text().replace(/( |\$|\n|\t)/g, '').replace(/−/g, '-'));
}
});
// Scrape next line
numberTitle = subGroup.next();
}
return bill;
});
}
},
function() {
// Go to usage detail page
page.open("https://www.att.com/olam/billUsageTiles.myworld");
},
function() {
// Hack again
var result = page.evaluate(function() {
return $('#usageTableToggleId tbody tr');
});
if (!result) {
index--;
} else {
// Scrape usages
data.usages = page.evaluate(function() {
var usages;
usages = {};
$('#usageTableToggleId tbody tr').each(function(index, elem) {
var number, usage;
number = $(elem).find('td:nth(0) span').text().replace(/(\n| |\t)/g, '').replace(/\./g, '-');
usage = parseFloat($(elem).find('td:nth(1) a span strong').text());
return usages[number] = usage;
});
return usages;
});
}
}
]
// Steps iterator with 2s interval
setInterval(function() {
// Only proceed to next step when page is fully loaded, otherwise, wait for next iteration
if (!loading && steps[index]) {
if (loadJQ) {
// Inject jquery for scraping if it hasn't been injected yet
page.injectJs("jquery-1.12.0.min.js")
loadJQ = false;
}
steps[index]();
index++;
}
// Finshed
if (!steps[index]) {
var fs = require('fs');
// Write data to data.json
fs.write('data.json', JSON.stringify(data, null, 2), 'w');
phantom.exit();
}
}, 2000);