Skip to content

Instantly share code, notes, and snippets.

@agross
Created January 2, 2012 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agross/1550983 to your computer and use it in GitHub Desktop.
Save agross/1550983 to your computer and use it in GitHub Desktop.
Simyo invoice downloader
#!/usr/bin/env ruby
USAGE = <<EOF
Usage: simyo.rb <Simyo web login password>
Downloads the latest invoice available on the Simyo web page.
EOF
require 'watir'
require 'rautomation'
module RAutomation
module Adapter
module WinFfi
class KeystrokeConverter
class << self
alias_method :old_convert_special_characters, :convert_special_characters
# WinFfi automation doesn't support sending Shift+Tab, so we need to path the parser to send
# {TAB} as Shift+Tab and
# {tab} as Tab.
def convert_special_characters chars
r = old_convert_special_characters chars
if chars =~ /[A-Z]/
r = [Constants::VK_LSHIFT] << r
end
r
end
end
end
end
end
end
module Simyo
class Downloader
@browser
@phone_number
@password
def initialize(phone_number, password)
@phone_number = phone_number
@password = password
end
def browser
@browser ||= Watir::Browser.new
end
def download_invoice
login
display_invoices
download_latest_invoice
logout
end
def login
browser.goto 'https://www.simyo.de/anmelden.html'
form = browser.forms(:action => /anmelden.html/).last
form.text_field(:name => /-login$/).set @phone_number
form.text_field(:name => /-password$/).set @password
form.button(:name => /-loginButton$/).click
end
def display_invoices
browser.goto 'https://www.simyo.de/mein-simyo/rechnungen.html'
end
def download_latest_invoice
invoices = browser.table(:class, /dataTable/)
download = invoices.buttons(:src, /download/).first
invoice_date = Date.parse download.parent.parent.cells.first.text
download.click!
save_invoice_as_pdf_using_IE invoice_date
end
def save_invoice_as_pdf_using_IE(invoice_date)
# Click on 'Save As' in the download bar at the bottom
sleep(1)
ie = RAutomation::Window.new(:hwnd => browser.hwnd)
# {UPCASE} = Shift + upcase
3.times do ie.send_keys '{TAB}' end
2.times do ie.send_keys '{down}' end
ie.send_keys '{enter}'
# Popup window to save
save_as = RAutomation::Window.new(:title => /Save As|Speichern unter/)
save_as.text_field(:class => "Edit", :index => 0).set "#{invoice_date} Simyo #{@phone_number}"
save_as.button(:value => /&Save|&Speichern/).click
end
def logout
browser.button(:class => /logoutLink/).click
browser.close
end
end
end
if ARGV.delete("--help") || ARGV.delete("-h")
puts USAGE
exit
end
phone_numbers = ['491637178546', '491631612432']
password = ARGV.shift
if password.nil?
puts USAGE
exit 1
end
phone_numbers.each do |phone|
Simyo::Downloader.new(phone, password).download_invoice
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment