Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # This script requires that you have Curl installed. It is installed by
- # default on Mac OS X and many Linux distributions.
- #
- # This script also assumes that you have the highline and json gems installed.
- # If not, you can install them with the following command:
- #
- # sudo gem install highline json
- #
- if __FILE__ == $0
- directory = ARGV.pop
- if directory == nil || directory == ''
- script_name = File.basename($0)
- puts "Usage: ruby #{script_name} directory"
- exit
- end
- require 'rubygems'
- require 'highline/import'
- require 'net/http'
- require 'json'
- BITSPACE_HOST = 'bitspace.at'
- BITSPACE_PORT = 80
- # Ask for a username and password
- username = ENV['BITSPACE_USERNAME'] || ask('Username:')
- password = ENV['BITSPACE_PASSWORD'] || ask('Password:') {|q| q.echo = false }
- # Create a new upload session
- upload_session = {}
- Net::HTTP.start(BITSPACE_HOST, BITSPACE_PORT) do |http|
- req = Net::HTTP::Get.new('/uploads/new', { 'Accept' => 'application/json' })
- req.basic_auth username, password
- res = http.request(req)
- case res
- when Net::HTTPSuccess:
- upload_session = JSON.parse(res.body)
- if upload_session['params'].is_a?(String)
- upload_session['params'] = JSON.parse(upload_session['params'])
- end
- else
- puts "Error: #{res.body}"
- exit
- end
- end
- # Upload the files
- Dir.glob(File.join(directory, '*.mp3')).each do |file|
- puts "Uploading #{file}:"
- params = upload_session['params'].map{|key,val| "-F \"#{key}=#{val.gsub('$','\$')}\"" }.join(' ')
- `curl #{params} -F "#{upload_session['file_param']}=@#{file}" #{upload_session['url']}`
- # Notify Bitspace of the uploaded file
- if $?.exitstatus == 0
- Net::HTTP.start(BITSPACE_HOST, BITSPACE_PORT) do |http|
- req = Net::HTTP::Post.new('/uploads')
- req.basic_auth username, password
- req.set_form_data({ 'upload[key]' =>
- upload_session['params']['key'].gsub('${filename}', File.basename(file)) })
- res = http.request(req)
- puts "Error: #{res.body}" and exit unless res.is_a?(Net::HTTPSuccess)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment