expand tilde-slash arguments in the completion scirpt
[hband-tools.git] / crawler-bin / erzsebetplusz.js
blob18ba579b3c8711b3595c5fb11245c6b26ef04870
1 #!/usr/bin/phantomjs --ssl-protocol=TLSv1
3 var llDebug = 1, llStatus = 2, llError = 3;
4 var vlDebug = false;
5 var vlStatus = false;
6 var vlError = true;
7 var stderr = require("system").stderr;
8 var Glob = {}
10 for(var i=0; i<phantom.args.length; i++)
12         if(phantom.args[i] == '--debug') vlDebug = true;
13         else if(phantom.args[i] == '--verbose') vlStatus = true;
14         else if(phantom.args[i].match(/^-/))
15         {
16                 stderr.write("Unknown option: "+phantom.args[i]+"\n");
17                 phantom.exit(2);
18         }
19         else
20         {
21                 Glob.username = phantom.args[i];
22                 Glob.password = phantom.args[i+1];
23                 i += 1;
24         }
27 if(Glob.password == undefined)
29         stderr.write("Usage: erzsebetplusz.js [--debug] [--verbose] <kartya-utolso-8-szamjegye> <jelszo>\n");
30         phantom.exit(2);
34 var url_login = "https://egyenleg.erzsebetutalvanyplusz.hu/cardholderlogin";
35 var loadInProgress = false;
36 var stepindex = 0;
37 var page = new WebPage();
39 function log(level, msg)
41         if(level == llDebug && vlDebug || 
42            level == llStatus && vlStatus ||
43            level == llError && vlError)
44         {
45                 stderr.write(msg + "\n");
46         }
49 function output(text)
51         console.log(text);
55 page.onConsoleMessage = function(msg)
57         log(llDebug, ">>> " + msg);
60 page.onLoadStarted = function()
62         loadInProgress = true;
63         log(llStatus, "load started [" + page.url + "]");
66 page.onLoadFinished = function()
68         loadInProgress = false;
69         log(llStatus, "load finished [" + page.evaluate(function(){return document.title;}) + "]");
72 page.onResourceRequested = function(requestData, networkRequest)
74         log(llDebug, requestData.method+' '+requestData.url);
75         /*
76         for(var i=0; i<requestData.headers.length; i++)
77         {
78                 log(llDebug, requestData.headers[i].name + ': ' + requestData.headers[i].value);
79         }
80         log(llDebug, JSON.stringify(requestData));
81         */
85 page.open(url_login, function(status)
87         if(status !== 'success')
88         {
89                 log(llError, "ERROR " + status);
90                 phantom.exit(1);
91         }
92         else
93         {
94                 var steps = [
95                         function(param)
96                         {
97                                 //return document.getElementById('login_form').innerHTML;
98                                 document.getElementById('username').value = param.username;
99                                 document.getElementById('password').value = param.password;
100                                 //document.getElementById('login_form').setAttribute('method','GET');
101                                 var captcha_field = document.getElementById('g-recaptcha-response');
102                                 captcha_field.parentElement.removeChild(captcha_field);
103                                 document.getElementById('login_form').submit.click();
104                                 return 1;
105                         },
106                         function()
107                         {
108                                 var a = document.querySelectorAll('.alert');
109                                 if(a && a.length)
110                                 {
111                                         for(var i=0; i<a.length; i++)
112                                         {
113                                                 console.log(a[i].innerHTML.replace(/^\s*()/m, ''));
114                                         }
115                                         return false;
116                                 }
117                                 else
118                                 {
119                                         //console.log(document.querySelectorAll('html')[0].outerHTML);
120                                 }
121                                 return 1;
122                         }
123                 ];
125                 worker = function()
126                 {
127                         if(!loadInProgress)
128                         {
129                                 log(llDebug, "step "+stepindex);
130                                 var ret = page.evaluate(steps[stepindex], Glob);
131                                 log(llDebug, "step "+stepindex+" returned: "+ret);
132                                 if(ret === false)
133                                 {
134                                         phantom.exit(1);
135                                 }
136                                 stepindex++;
137                         }
138                         
139                         if(stepindex < steps.length)
140                         {
141                                 setTimeout(worker, 200);
142                         }
143                         else
144                         {
145                                 log(llDebug, "complete");
146                                 setInterval(checkReady, 200);
147                         }
148                 }               
149                 setTimeout(worker, 200);
151                 readBalance = function()
152                 {
153                         var balance = [];
154                         var name;
155                         var value;
156                         var a = document.querySelectorAll('.egyenlegWidgetHeaderTitleStyle');
157                         for(var i=0; i<a.length; i++)
158                         {
159                                 //console.log(a[i].outerHTML);
160                                 if(name == undefined)
161                                 {
162                                         name = a[i].innerText;
163                                 }
164                                 else
165                                 {
166                                         value = a[i].innerText;
167                                         balance.push({name: name, value: value});
168                                         name = undefined;
169                                 }
170                         }
171                         return JSON.stringify(balance);
172                 }
173                 
174                 checkReady = function()
175                 {
176                         var balance = JSON.parse(page.evaluate(readBalance));
177                         if(balance && balance.length)
178                         {
179                                 for(var i=0; i<balance.length; i++)
180                                 {
181                                         var zseb = balance[i].name.replace(/ zseb.*()/, '');
182                                         var penz = balance[i].value.replace(/[^0-9 \.,]/g, '');
183                                         output(zseb + ': ' + penz);
184                                 }
185                                 phantom.exit(0);
186                         }
187                 }
188         }