moneymoney-uberspace/Uberspace.lua

109 lines
3.1 KiB
Lua
Raw Permalink Normal View History

2017-10-27 14:54:12 +02:00
--[[
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
--]]
2017-10-31 18:02:14 +01:00
WebBanking{version = 1.01,
2017-10-27 14:35:33 +02:00
url = 'https://uberspace.de/login',
2017-10-31 14:55:13 +01:00
services = {'Uberspace.de'},
description = string.format(
MM.localizeText("Get balance and transactions for %s"),
"Uberspace.de")
}
2017-10-27 14:35:33 +02:00
function SupportsBank (protocol, bankCode)
return bankCode == 'Uberspace.de' and protocol == ProtocolWebBanking
end
local usConnection = Connection()
local usUsername
function InitializeSession (protocol, bankCode, username, username2,
password, username3)
-- Login.
usUsername = username
html = HTML(usConnection:get('https://uberspace.de/login'))
html:xpath('//input[@name="login"]'):attr('value', username)
html:xpath('//input[@name="password"]'):attr('value', password)
html = HTML(
usConnection:request(html:xpath('//input[@name="submit"]'):click()))
if html:xpath('//input[@name="login"]'):length() > 0 then
-- We are still at the login screen.
return "Failed to log in. Please check your user credentials."
end
end
function ListAccounts (knownAccounts)
-- Return array of accounts.
local account = {
name = 'Uberspace ' .. usUsername,
accountNumber = '1',
portfolio = false,
currency = 'EUR',
type = AccountTypeSavings
}
return {account}
end
function RefreshAccount (account, since)
function ParseAmount (amountString)
local pattern = '(%-?%d+),(%d%d)'
local euro, cent = amountString:match(pattern)
if not euro or not cent then
return nil
end
2017-10-27 14:35:33 +02:00
euro = tonumber(euro)
cent = tonumber(cent) / 100
if euro < 0 then
return euro - cent
else
return euro + cent
end
end
html = HTML(usConnection:get(
'https://uberspace.de/dashboard/accounting'))
tableRows = html:xpath(
'//*[@id="transactions"]//tr[count(td)=3][position()<last()]')
print('Found ' .. tableRows:length() .. ' rows')
local transactions = {}
for i = 1, tableRows:length() do
local row = tableRows:get(i)
local children = row:children()
local pattern = '(%d%d)%.(%d%d)%.(%d%d%d%d)'
local day, month, year = children:get(1):text():match(pattern)
2017-10-31 18:02:27 +01:00
local bookingDate = os.time{day=day, month=month, year=year}
2017-10-27 14:35:33 +02:00
2017-10-31 18:02:27 +01:00
if bookingDate < since then
2017-10-27 14:35:33 +02:00
print('Stopping parsing because transaction is too old.')
2017-10-31 18:02:27 +01:00
print('Date of transaction: ' .. os.date('%c', bookingDate))
2017-10-27 14:35:33 +02:00
print('since: ' .. os.date('%c', since))
break
end
local amount = ParseAmount(children:get(3):text())
table.insert(transactions, {
2017-10-31 18:02:27 +01:00
bookingDate = bookingDate,
2017-10-27 14:35:33 +02:00
amount = amount
})
end
local balanceElement = html:xpath('//*[@id="total"]')
local balance = ParseAmount(balanceElement:text())
if not balance then
balance = 0
end
2017-10-27 14:35:33 +02:00
return {balance=balance, transactions=transactions}
end
function EndSession ()
usConnection:get('https://uberspace.de/logout')
end