Skip to content

Instantly share code, notes, and snippets.

@amaia
Created August 9, 2012 16:28
Show Gist options
  • Save amaia/3305648 to your computer and use it in GitHub Desktop.
Save amaia/3305648 to your computer and use it in GitHub Desktop.
Script to download all invoice pdfs from facturagem.com
#!/usr/bin/env ruby
require 'httparty'
login = ENV['LOGIN']
password = ENV['PASSWORD']
options = {:body => {:login => login, :password => password}}
r = HTTParty.post('https://facturagem.com/api/session.json', options)
api_key = r.parsed_response['user']['api_key']
options = {:query => {:api_key => api_key, :format => 'json', :strict => false}}
invoices_link = r.parsed_response['user']['links'].select{|l| l['rel'] == 'invoices'}.first
page = 1
while true
options[:query].merge!({:page => page})
r = HTTParty.get(invoices_link['uri'], options)
if r.parsed_response['invoices']
r.parsed_response['invoices'].each do |i|
invoice_uri = i['links'].first['uri']
invoice = HTTParty.get(invoice_uri, options)
pdf_link = invoice.parsed_response['invoice']['links'].select{|l| l['rel']=='pdf'}.first['uri']
invoice_number = invoice.parsed_response['invoice']['number']
`wget -O #{invoice_number}.pdf #{pdf_link}`
end
page += 1
else
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment