Skip to content

Instantly share code, notes, and snippets.

@Masa331
Last active October 19, 2018 14:45
Show Gist options
  • Save Masa331/6330bdec4c6a5885e361fb249ba5dd93 to your computer and use it in GitHub Desktop.
Save Masa331/6330bdec4c6a5885e361fb249ba5dd93 to your computer and use it in GitHub Desktop.
Download all uploaded documents from UOL accounting api
#!/usr/bin/env ruby
require 'net/http'
require 'json'
def fetch(uri, token)
uri = URI(uri)
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri)
request['Authorization'] = "Basic #{token}"
request['Accept'] = 'application/json'
response = http.request request
end
JSON.parse response.body
end
def traverse(url, token, &block)
response = fetch(url, token)
response['items'].each do |item|
yield item
end
next_page_url = response.dig('_meta', 'pagination', 'next')
traverse(next_page_url, token, &block) if next_page_url
end
db_name = ARGV.first
token = ARGV.last
traverse("https://#{db_name}.ucetnictvi.uol.cz/api/v1/uploaded_documents", token) do |item|
uri = URI item['file']
name = uri.path.split('/').last
`wget -O #{name} "#{item['file']}"`
end
@Masa331
Copy link
Author

Masa331 commented Oct 19, 2018

You need to have ruby and wget installed. The script has to be chmoded to be executable(chmod +x get_documents.rb)

Then to start it: ./get_documents.rb <your uol db code> <your uol api token>. All documents are downloaded into current directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment