Last active
August 29, 2015 14:20
-
-
Save corny/3ffcc6e33a83185dad6d to your computer and use it in GitHub Desktop.
Holt Kreditkartenumsätze von Pluscard ab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mechanize' | |
class Pluscard | |
START = 'https://www.pluscard.de/kris/anmeldung/anmeldung.php' | |
Entry = Struct.new(:number,:buchungsdatum,:belegdatum,:betrag,:text) | |
def initialize(username,password) | |
@username = username | |
@password = password | |
@agent = Mechanize.new | |
end | |
def list | |
login unless @numbers | |
invoices = [] | |
@numbers.each do |number| | |
select_card number | |
page = get '/kris/umsaetze/index.php' | |
next if page.body.include?("Es liegen keine aktuellen Ums") | |
table = page.at("table[width='100%']") or (raise "table not found") | |
table.elements.each do |row| | |
if (cells = row.search("td[class^=tabellenzeile]")).any? | |
invoices << Entry.new( | |
number, | |
Date.parse(cells[0].text), | |
Date.parse(cells[1].text), | |
BigDecimal.new(cells[2].text.gsub("- ","-").sub("EUR","").gsub(".","").sub(",",".").gsub(/[\u00A0 ]/,"")), | |
cells[3].children.select{|c| Nokogiri::XML::Text===c }.map{|c| c.text.gsub(/[\u00A0 ]+/," ").strip }.join("\n") | |
) | |
end | |
end | |
end | |
invoices | |
end | |
protected | |
def login | |
page = get(START) | |
form = page.form("login") | |
form.krednr = @username | |
form.kennwort = @password | |
# Einloggen | |
page = form.submit | |
# Login fehlgeschlagen? | |
raise LoginException if page.uri.path != '/kris/saldo/index.php' | |
@csrf = page.forms[0].CSRFToken | |
@numbers = page.search("input[name=krednr]").collect{|input| input["value"] } | |
raise ArgumentError, "Keine Kreditkarten gefunden" if @numbers.empty? | |
end | |
# Wählt eine Kreditkarte aus. | |
# Die Ausgewählte wird in der Session gespeichert. | |
def select_card(number) | |
get '/kris/common/navigation.php', abrechnung_neu: "J", krednr: number | |
end | |
def get(path, params={}) | |
url = path.sub(/^\.\./,'/kris') | |
url << "?" << params.merge(CSRFToken: @csrf).to_param if @csrf && !url.include?("?") | |
@agent.get url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment