Created
May 8, 2012 21:05
-
-
Save i-blis/2639276 to your computer and use it in GitHub Desktop.
How to get Hubic Webdav credentials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work: