Created
August 9, 2012 16:28
-
-
Save amaia/3305648 to your computer and use it in GitHub Desktop.
Script to download all invoice pdfs from facturagem.com
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
#!/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