-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdkb-crawl.php
More file actions
executable file
·244 lines (198 loc) · 6.47 KB
/
dkb-crawl.php
File metadata and controls
executable file
·244 lines (198 loc) · 6.47 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/php
<?php
chdir(__DIR__);
require('simple_html_dom.php');
require('config.php');
$url = 'https://www.dkb.de/';
define('CSV_HEADER_LINES', 7);
define('CSV_EC_COLUMN_DATE', 0);
define('CSV_EC_COLUMN_DATE2', 1);
define('CSV_EC_COLUMN_SUBJECT1', 3);
define('CSV_EC_COLUMN_SUBJECT2', 4);
define('CSV_EC_COLUMN_VALUE', 7);
define('CSV_CC_COLUMN_DATE', 2);
define('CSV_CC_COLUMN_SUBJECT', 3);
define('CSV_CC_COLUMN_VALUE', 4);
function doCurlPost($action, $data) {
global $url, $ch;
$lastUri = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if ($lastUri) { curl_setopt($ch, CURLOPT_REFERER, $lastUri); }
curl_setopt($ch, CURLOPT_URL, $url . $action);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
return curl_exec($ch);
}
function doCurlGet($path) {
global $url, $ch;
$lastUri = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if ($lastUri) { curl_setopt($ch, CURLOPT_REFERER, $lastUri); }
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
return curl_exec($ch);
}
function cleanLine($line) {
$synonyms = array(
'EINMAL LASTSCHRIFT' => 'Lastschrift',
'FOLGELASTSCHRIFT' => 'Lastschrift',
'GUTSCHRIFT' => 'Gutschrift',
'KARTENZAHLUNG/-ABRECHNUNG' => 'KARTENZAHLUNG',
'ONLINE-UEBERWEISUNG' => mb_convert_encoding('ÜBERWEISUNG', 'windows-1252', 'UTF-8'),
);
// clean out dynamic stuff ..
$line = preg_replace('#ABW(E|A)\+[^"]+#', '', $line);
$line = preg_replace('#\s+"#', '"', $line);
// stip out last column (ec: kundenreferenz/ cc:ursprünglicher betrag)
$line = preg_replace('#[^"]*";$#', '"', $line);
$line = str_ireplace(array_keys($synonyms), array_values($synonyms), $line);
return $line;
}
function findLineInCSV($line, $csv) {
$line = cleanLine($line);
foreach ($csv as $k => $v) {
if ($k < CSV_HEADER_LINES) continue;
$v = cleanLine($v);
if ($v == $line) {
return $k;
}
}
return false;
}
//
// CURL init
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'data/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'data/cookie.txt');
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
//
// LOGIN
//
echo 'Logging in...';
$result = doCurlGet($url.'banking');
$dom = str_get_html($result);
$form = $dom->find('form', 1);
$post_data = array();
foreach ($form->find('input') as $elem) {
if ($elem->name == 'j_username') $elem->value = $kto;
if ($elem->name == 'j_password') $elem->value = $pin;
$post_data[$elem->name] = $elem->value;
}
$html_ = doCurlPost('banking', $post_data);
if (strpos($html_, 'Letzte Anmeldung:') !== false) {
echo "OK!\n";
} else {
echo 'Error. Login failed!';
die();
}
//
// get Konten
//
echo "get Konten...\n";
$accounts = array();
$matches = array();
$dom_ = str_get_html($html_);
$cnt = 0;
foreach ($dom_->find('table[class=financialStatusTable] tr') as $k => $row) {
if ($row->class != 'mainRow') { continue; }
// loop
$td = $row->find('td', 0);
if (!$td) continue;
$desc = trim(strip_tags($td->find('div', 0)->plaintext));
$nr = trim($td->find('div', 1)->plaintext);
$nr = str_replace('*', '_', $nr);
$ec = strpos($td->find('div', 1)->plaintext, 'DE') !== false;
if ($desc == 'Depot') break;
echo " found '$desc' ($nr)";
echo $ec ? " - is EC" : " - is CC";
echo " - load Details";
$html = doCurlGet($url . 'DkbTransactionBanking/content/banking/financialstatus/FinancialComposite/FinancialStatus.xhtml?$event=paymentTransaction&row='.$cnt.'&group=0');
// download CSV
echo " - download CSV";
$ums = $ec ? 'kontoumsaetze' : 'kreditkartenumsaetze';
$csv = doCurlGet($url . 'banking/finanzstatus/'.$ums.'?$event=csvExport');
$row->clear();
unset($row);
$cnt++;
echo "\n";
$accounts[$nr] = ['desc' => $desc, 'csv' => $csv, 'nr' => $nr, 'type' => $ec?'ec':'cc'];
}
//
// Logout
//
echo "Logout!\n";
$html = doCurlGet($url . '/DkbTransactionBanking/banner.xhtml?$event=logout');
//
// Parse CSV
//
echo "Parse CSV\n";
$push = array();
foreach ($accounts as $account) {
$cnt = 0;
$lines = explode("\n", $account['csv']);
$exists = file_exists($file = __DIR__ . '/data/' . $account['nr']);
$csv = $exists? file($file, FILE_IGNORE_NEW_LINES) : false;
file_put_contents($file, $account['csv']);
if (!$exists) {
// no push on first run. just save the csv for later comparison
continue;
}
$first = true;
foreach ($lines as $k => $line) {
if ($k < CSV_HEADER_LINES || !$line) continue;
$first = false;
if (strpos($line, 'Auslandseinsatz') !== false) continue;
$data = explode(';', $line);
$data = array_map(function($e){return trim($e, '" ><');}, $data);
// skip predated transaction
if ($account['type'] == 'ec' && !$data[CSV_EC_COLUMN_DATE2]) continue;
$lineNbr = findLineInCSV($line, $csv);
if ($lineNbr === false) {
// push
if (++$cnt >= 5) break; // no more than 5 push messages per account per run
echo $str = " new entry: $line\n";
if ($account['type'] == 'ec') {
// Strip CC data out of Verwendungszweck
$data[CSV_EC_COLUMN_SUBJECT2] = preg_replace('#(\d{4}) \d{4} \d{4} (\d{4})#', '$1 XXXX XXXX $2', $data[CSV_EC_COLUMN_SUBJECT2]);
$push[] = array(
$account['desc'],
$data[CSV_EC_COLUMN_DATE],
$data[CSV_EC_COLUMN_SUBJECT1] . ' ' . $data[CSV_EC_COLUMN_SUBJECT2],
$data[CSV_EC_COLUMN_VALUE]
);
} else {
$push[] = array(
$account['desc'],
$data[CSV_CC_COLUMN_DATE],
$data[CSV_CC_COLUMN_SUBJECT],
$data[CSV_CC_COLUMN_VALUE]
);
}
} else {
// line found in old CSV .. we can stop here with this CSV
break;
}
}
}
//
// Push
//
echo "PUSH via Pushover\n";
foreach ($push as $k => $elem) {
if ($k && $k%3 == 0) {
echo "Sleeping..\n";
sleep(10);
}
list($desc, $date, $subject, $value) = $elem;
$color = $value[0] == '-' ? 'red' : 'green';
$title = $desc . ' ' . $value . ' Euro';
$message = '<b>'.$date . '</b><br>' . utf8_encode($subject) . '<br><br><b style="color:'.$color.'">' . $value . ' Euro</b>';
// play sound only on first push
$sound = $k == 0 ? 'cashregister' : 'none';
exec($cmd = 'curl -s --form-string "token='.$pushover_token.'" --form-string "user='.$pushover_user.'" --form-string "html=1" --form-string "message='.$message.'" --form-string "sound='.$sound.'" --form-string "title='.$title.'" https://api.pushover.net/1/messages.json');
echo "\n";
}