Guest User

Untitled

a guest
May 20th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # This script requires that you have Curl installed. It is installed by
  4. # default on Mac OS X and many Linux distributions.
  5. #
  6. # This script also assumes that you have the highline and json gems installed.
  7. # If not, you can install them with the following command:
  8. #
  9. # sudo gem install highline json
  10. #
  11.  
  12. if __FILE__ == $0
  13. directory = ARGV.pop
  14. if directory == nil || directory == ''
  15. script_name = File.basename($0)
  16. puts "Usage: ruby #{script_name} directory"
  17. exit
  18. end
  19.  
  20. require 'rubygems'
  21. require 'highline/import'
  22. require 'net/http'
  23. require 'json'
  24.  
  25. BITSPACE_HOST = 'bitspace.at'
  26. BITSPACE_PORT = 80
  27.  
  28. # Ask for a username and password
  29. username = ENV['BITSPACE_USERNAME'] || ask('Username:')
  30. password = ENV['BITSPACE_PASSWORD'] || ask('Password:') {|q| q.echo = false }
  31.  
  32. # Create a new upload session
  33. upload_session = {}
  34. Net::HTTP.start(BITSPACE_HOST, BITSPACE_PORT) do |http|
  35. req = Net::HTTP::Get.new('/uploads/new', { 'Accept' => 'application/json' })
  36. req.basic_auth username, password
  37. res = http.request(req)
  38. case res
  39. when Net::HTTPSuccess:
  40. upload_session = JSON.parse(res.body)
  41. if upload_session['params'].is_a?(String)
  42. upload_session['params'] = JSON.parse(upload_session['params'])
  43. end
  44. else
  45. puts "Error: #{res.body}"
  46. exit
  47. end
  48. end
  49.  
  50. # Upload the files
  51. Dir.glob(File.join(directory, '*.mp3')).each do |file|
  52. puts "Uploading #{file}:"
  53. params = upload_session['params'].map{|key,val| "-F \"#{key}=#{val.gsub('$','\$')}\"" }.join(' ')
  54. `curl #{params} -F "#{upload_session['file_param']}=@#{file}" #{upload_session['url']}`
  55.  
  56. # Notify Bitspace of the uploaded file
  57. if $?.exitstatus == 0
  58. Net::HTTP.start(BITSPACE_HOST, BITSPACE_PORT) do |http|
  59. req = Net::HTTP::Post.new('/uploads')
  60. req.basic_auth username, password
  61. req.set_form_data({ 'upload[key]' =>
  62. upload_session['params']['key'].gsub('${filename}', File.basename(file)) })
  63. res = http.request(req)
  64. puts "Error: #{res.body}" and exit unless res.is_a?(Net::HTTPSuccess)
  65. end
  66. end
  67. end
  68.  
  69. end
Add Comment
Please, Sign In to add comment