mirror of https://github.com/captn3m0/outliner.git
50 lines
1.4 KiB
Ruby
Executable File
50 lines
1.4 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
require 'bundler/setup'
|
|
require 'outliner'
|
|
require 'tempfile'
|
|
|
|
def validate
|
|
raise 'Missing arguments' if ARGV.size != 1
|
|
raise 'Invalid directory' unless Dir.exist?(ARGV[0])
|
|
raise 'OUTLINE_BASE_URI not set' unless ENV.key?('OUTLINE_BASE_URI')
|
|
raise 'OUTLINE_TOKEN not set' unless ENV.key?('OUTLINE_TOKEN')
|
|
end
|
|
|
|
# Run validations
|
|
validate
|
|
|
|
# Setup variables
|
|
local_directory = ARGV[0]
|
|
CLIENT = Outliner::Client.new ENV['OUTLINE_BASE_URI']
|
|
|
|
# Download the complete zip
|
|
response = CLIENT.collections__export_all(format: "outline-markdown")
|
|
|
|
raise 'Failed to trigger export_all action' if not response['success']
|
|
|
|
file_operation_id = response['data']['fileOperation']['id']
|
|
fop_info_response = nil
|
|
i = 0
|
|
loop do
|
|
i += 1
|
|
raise 'Timed out waiting for the file export operation to complete' if i > 20
|
|
sleep(2*i)
|
|
fop_info_response = CLIENT.fileOperations__info(id: file_operation_id)
|
|
raise 'Failed to query export file operation info' if not fop_info_response['ok']
|
|
break if fop_info_response['data']['state'] == 'complete'
|
|
end
|
|
|
|
begin
|
|
fop_redirect_response = CLIENT.fileOperations__redirect({id: file_operation_id}, {no_follow: true})
|
|
rescue HTTParty::RedirectionTooDeep => e
|
|
response = HTTParty.get e.response.header['location']
|
|
file = Tempfile.new('download.zip')
|
|
File.open(file.path, 'w') { |f| f.write(response.body) }
|
|
`unzip -o "#{file.path}" -d "#{local_directory}"`
|
|
file.unlink
|
|
end
|
|
|
|
|