-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhfcfra.lua
245 lines (193 loc) · 9.17 KB
/
hfcfra.lua
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
245
-- Inofficial hfc frankfurt Extension (https://www.hfc-frankfurt.de) for MoneyMoney
-- Fetches flight balance from hfc-frankfurt and returns them
--
-- Copyright (c) 2021 Nima Barraci
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
WebBanking{version = 1.00,
url = "https://www.hfc-frankfurt.de",
services = {"Hanseatischer Fliegerclub Frankfurt"},
description = "Kontostand Hanseatischer Fliegerclub Frankfurt"}
function SupportsBank (protocol, bankCode)
return protocol == ProtocolWebBanking and bankCode == "Hanseatischer Fliegerclub Frankfurt"
end
-- State
local connection = Connection()
local html
local _username
function InitializeSession (protocol, bankCode, username, customer, password)
-- Fetch login page.
connection.language = "de-de"
html = HTML(connection:get("https://www.hfc-frankfurt.de/index.php?op=login"))
_username = username
html:xpath("//input[@name='userid']"):attr("value", username)
html:xpath("//input[@name='password']"):attr("value", password)
html = HTML(connection:request(html:xpath("//input[@type='submit']"):click()))
if html:xpath("//a[@href='/index.php?op=logout']"):length() == 0 then
MM.printStatus("Login Failed")
return LoginFailed
end
end
function ListAccounts (knownAccounts)
-- Return array of accounts.
local account = {
name = "HFC Frankfurt",
owner = _username,
accountNumber = _username,
bankCode = "",
currency = "EUR",
type = AccountTypeOther
}
return {account}
end
function RefreshAccount (account, since)
-- Return balance and array of transactions.
local _transactions = {}
html = HTML(connection:get("https://www.hfc-frankfurt.de/account.php"))
local _balanceTable = html:xpath("//table[@class='manager rwd']")
-- Parse each transaction
local _transactionsRows = _balanceTable:xpath("(//table//tbody//tr)")
for i=1,_transactionsRows:length() do
local _currentRow = _transactionsRows:get(i)
local _leadElement = _currentRow:children():get(1):text()
if (_leadElement == 'V' or _leadElement == 'U' or _leadElement == 'L' or _leadElement == 'N' or _leadElement == 'J') then
_transactions[#_transactions+1] = {
bookingDate = getPOSIXdate(_currentRow:children():get(2):text()),
purpose = getTransactionEntry(_currentRow:children():get(1):text(),
_currentRow:children():get(2):text(),
_currentRow:children():get(3):text(),
_currentRow:children():get(4):text(),
_currentRow:children():get(5):text(),
_currentRow:children():get(6):text(),
_currentRow:children():get(7):text(),
_currentRow:children():get(8):text(),
_currentRow:children():get(9):text(),
_currentRow:children():get(10):text()),
amount = toNumber(_currentRow:children():get(11):text())
}
end
end
-- End transaction
-- Begin balance
local _balance = _balanceTable:xpath("(//table//td[@data-header='Kontostand'])"):text()
-- End balance
return {balance=toNumber(_balance), transactions=_transactions}
end
function EndSession ()
-- Logout.
end
-- Helper
function getTransactionEntry(type, date, acreg, from, to, flighttime, flightCosts, landings, landingsCost, comments)
local _workstring
if (type== 'V') then
_workstring = "Vortrag\n" .. "Comments: " .. comments
elseif (type == 'U') then
_workstring = "Überweisung\n" .. "Comments: " .. comments
elseif (type == 'L') then
_workstring = "Schulungsflug\n" .. "AC: " .. string.upper(acreg) .. " " ..
"From: " .. string.upper(from) .. " " ..
"To: " .. string.upper(to) .. " " ..
"Date: " .. date .. " " ..
"Flight Time: " .. flighttime .. " " ..
"Flight Cost: " .. flightCosts .. " " ..
"Landings: " .. landings .. " " ..
"Landings Cost: " .. landingsCost .. " "
elseif (type == 'J') then
_workstring = "Jahresbeitrag\n" .. "Comments: " .. comments
elseif (type == 'N') then
_workstring = "Flug\n" .. "AC: " .. string.upper(acreg) .. " " ..
"From: " .. string.upper(from) .. " " ..
"To: " .. string.upper(to) .. " " ..
"Date: " .. date .. " " ..
"Flight Time: " .. flighttime .. " " ..
"Flight Cost: " .. flightCosts .. " " ..
"Landings: " .. landings .. " " ..
"Landings Cost: " .. landingsCost .. " "
else
_workstring = "Sonstiges\n" .. "Comments: " .. comments
end
return _workstring
end
function getPOSIXdate(str)
local _splits = mysplit(str,"-")
return os.time{day=_splits[3], year=_splits[1], month=_splits[2]}
end
function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function toNumber(str)
local _workstring = trim1(str)
local _mult = 1
_workstring = string.sub(_workstring,0,string.len(_workstring))
if string.match(_workstring, "-") then
_mult = -1
end
_workstring = string.match(_workstring,'%f[%d]%d[,.%d]*%f[%D]')
_workstring = string.gsub(_workstring,",",".")
local _number = _workstring * _mult
return _number
end
function trim1(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- originally from http://stackoverflow.com/questions/6075262/lua-table-tostringtablename-and-table-fromstringstringtable-functions
-- modified fixed a serialization issue with invalid name. and wrap with 2 functions to serialize / deserialize
function tableToString(table)
return "return"..serializeTable(table)
end
function stringToTable(str)
local f = loadstring(str)
return f()
end
function serializeTable(val, name, skipnewlines, depth)
skipnewlines = skipnewlines or false
depth = depth or 0
local tmp = string.rep(" ", depth)
if name then
if not string.match(name, '^[a-zA-z_][a-zA-Z0-9_]*$') then
name = string.gsub(name, "'", "\\'")
name = "['".. name .. "']"
end
tmp = tmp .. name .. " = "
end
if type(val) == "table" then
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
for k, v in pairs(val) do
tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
end
tmp = tmp .. string.rep(" ", depth) .. "}"
elseif type(val) == "number" then
tmp = tmp .. tostring(val)
elseif type(val) == "string" then
tmp = tmp .. string.format("%q", val)
elseif type(val) == "boolean" then
tmp = tmp .. (val and "true" or "false")
else
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
end
return tmp
end
-- SIGNATURE: MCwCFEs1QAdLuu6WYxsVnnVRkYCOqyKoAhQKoEnYY2FuPWKAlMC3AOPyYX4RXA==