Skip to content

Instantly share code, notes, and snippets.

@breunigs
Created August 7, 2012 07:04
Show Gist options
  • Save breunigs/3282599 to your computer and use it in GitHub Desktop.
Save breunigs/3282599 to your computer and use it in GitHub Desktop.
Script that downloads all invoices in your KabelBW login center. (Read: I’m not going to cope with KabelBW’s cr… issues)
#!/usr/bin/ruby
# encoding: utf-8
# Public Domain
# (someone claimed the next lines would be useful for… people. So here goes:
# © 2012 Stefan Breunig
# stefan+kabelbwrechnungen@mathphys.fsk.uni-heidelberg.de)
USER = "DEIN KABELBW ACCOUNT"
PASS = "DEIN KABELBW PASSWORT"
PATH = "ABSOLUTER PFAD AN DEM DIE PDFS ABGELEGT WERDEN SOLLEN"
require "rubygems"
require "mechanize"
a = Mechanize.new
puts "Logging in…"
a.get("http://www.kabelbw.de/rechnungen") do |login_page|
logged_in = login_page.form_with(:name => 'login') do |f|
f.cscLoginName = USER
f.cscLoginPwd = PASS
end.click_button
links = logged_in.links_with(:text => "Rechnung")
pdf_ids = links.map { |l| l.attributes["onclick"] }
pdf_ids.map! { |id| id.split(",").map { |s| s.match(/'[^']+/).to_s[1..-1] } }
# selects the dates by going up to the <tr> element, then selecting the
# first <td>’s text content
dates = links.map { |l| l.node.parent.parent.children.first.children.text }
dates.map! { |d| Date.parse(d).strftime }
puts "Found #{pdf_ids.size} PDFs…"
pdf_ids.each_with_index do |id, ind|
if Dir.glob(File.join(PATH, "*#{id[1]}*.pdf")).any?
puts "Skipping #{id[1]}: Already exists"
next
end
puts "Processing #{id[1]}…"
pdf = logged_in.form_with(:name => "pdfview") do |f|
f.cscInvoiceType = id[0]
f.cscInvoiceId = id[1]
f.cscInvoiceTitle = id[2]
end.click_button
filename = File.join(PATH, "#{id[0]}_#{dates[ind]}_#{id[1]}.pdf")
File.open(filename, 'w+b') { |file| file << pdf.body }
end
puts "Logging out…"
logged_in.form_with(:name => "loginboxform").click_button
end
puts "Thank you, come again."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment