Skip to content

Instantly share code, notes, and snippets.

@i-blis
Created May 8, 2012 21:05
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save i-blis/2639276 to your computer and use it in GitHub Desktop.
Save i-blis/2639276 to your computer and use it in GitHub Desktop.
How to get Hubic Webdav credentials
#!/usr/bin/env ruby
# hubic.rb Get credentials to Webdav server of Hubic account
require 'httpclient'
require 'json'
require 'optparse'
require 'highline/import'
module Hubic
extend self
attr_accessor :proxy
BASE_URL = 'https://ws.ovh.com/cloudnas/r0/ws.dispatcher'
URLS = {
:login => BASE_URL + '/nasLogin',
:nas => BASE_URL + '/getNas',
:credentials => BASE_URL + '/getCredentials' }
def credentials(login, pass, &block)
result = {}
client = HTTPClient.new
client.proxy = proxy if proxy
client.agent_name = 'hubiC/1.0.9 (Windows NT 6.1; fr_FR)'
response = client.post URLS[:login],
{ :session => '', :params => { :email => login , :password => pass}.to_json }
check_error response
id = JSON.parse(response.body)['answer']['id']
response = client.post URLS[:nas], { :session => id }
check_error response
result.merge! :url => JSON.parse(response.body)['answer']['url']
response = client.post URLS[:credentials], { :session => id }
check_error response
result.merge! :username => JSON.parse(response.body)['answer']['username']
result.merge! :secret => JSON.parse(response.body)['answer']['secret']
return block_given? ? yield(result) : result
end
private
def check_error response
case
when response.status_code != 200
raise "Connexion failed -> " + response.reason_phrase
when JSON.parse(response.body)['answer'].nil?
raise "Connexion failed -> " + JSON.parse(response.body)['error']['message']
end
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [-l login] [-m [mountpoint]]"
opts.on("-l", "--login LOGINNAME", "Account's loginname (email)") do |l|
options[:login] = l
end
opts.on("-p", "--password PASSWORD", "Account's password") do |p|
options[:paswd] = p
end
opts.on("-x", "--proxy PROXYSERVER", "Routes through proxy") do |p|
Hubic.proxy = p
end
opts.on("-m", "--mount [MOUNTPOINT]", "Offers to mount the Webdav server",
"Available only on Unix") do |m|
options[:mount] = true
options[:mntpt] = m
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
login = options[:login] ? options[:login] : ask("Loginname: ") { |q| q.echo = true }
password = options[:paswd] ? options[:paswd] : ask('Password : ') { |q| q.echo = '*' }
Hubic.credentials(login, password) do |data|
data.each { |key, value| puts key.to_s + ": " + value.to_s }
if options[:mount]
mountpoint = options[:mntpt] || '/mnt'
case
when RUBY_PLATFORM.match(/linux/i)
`mount -t davfs #{data[:url]} #{mountpoint}`
when RUBY_PLATFORM.match(/darwin|bsd/i)
`mount_webdav -s #{data[:url]} #{mountpoint}`
else
raise "Don't know how to mount a Webdav volume on non-Unix plateforms"
end
end
end
@Thinkscape
Copy link

Doesn't work:

hubic.rb:42:in `check_error': undefined method `reason_phrase' for #<HTTP::Message:0x007ff4b3290b58> (NoMethodError)
    from hubic.rb:26:in `credentials'
    from hubic.rb:74:in `<main>'

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