2012-11-14 59 views
3

Ruby如果url是重定向,如何下載文件?Ruby - 如果url是重定向,如何下載文件?

我試圖下載該網址:

soundcloud.com/stereo-f---/cohete-amigo/download

重定向是這樣的:

[EC-媒體.soundcloud.com/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9 & AWSAccessKeyId = AKIAJ4IAZE5EOI7PA7VQ &過期= 1352919869 &簽名= OVWD9VdV7ew%2B%2FS%2BO0YpkKZLGOCw%3D] [2]

,什麼我已經試過是這樣的:

Net::HTTP.start("ec-media.soundcloud.com") { |http| 
    resp = http.get("/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9&AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&Expires=1352919869&Signature=OVWD9VdV7ew%2B%2Fs%2BO0YpkKZLGOCw%3D") 
    open("test.wav", "wb") { |file| 
    file.write(resp.body) 
    } 
} 
puts "Done." 
sleep 50 

我不明白/瞭解任何的互聯網協議和重定向和事情......請給我一個很好的解釋或與我的腳本幫助用ruby從soundcloud下載文件?

三江源

UPDATE

我已經試過這種方式,但我得到一個錯誤:

Net::HTTP.start("soundcloud.com") do |http| 
    resp = http.get("/dubstep-4/kill-paris-tender-love/download") 

    while resp.code == '301' || resp.code == '302' 
    # moved permanently or temporarily: try again at the new location. 
    resp = http.get(URI.parse(resp.header['location'])) 
    # ideally you should also bail if you make too many redirects. 
    end 

    # make sure the request was successful. 
    if resp.code == '200' 
    open("test.wav", "wb"){ |file| file.write(resp.body) } 
    else 
    puts "Error: HTTP #{resp.code}" 
    end 
end 

錯誤:

C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1860:in `initialize': undefined method `empty?' for #<URI::HTTP:0x25a9ce8> (NoMethodError) 
     from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:2093:in `initialize' 
     from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1026:in `new' 
     from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:1026:in `get' 
     from C:/Users/Administrador/Desktop/1.rb:59:in `block in <main>' 
     from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:745:in `start' 
     from C:/Program Files (x86)/Ruby/lib/ruby/1.9.1/net/http.rb:557:in `start' 
     from C:/Users/Administrador/Desktop/1.rb:48:in `<main>' 

回答

4

有一個稱爲Open URI的標準庫,它比Net HTTP更高。它遵循自動重定向:

require 'open-uri' 
file = open("http://soundcloud.com/stereo-f---/cohete-amigo/download") 
+0

太容易了!非常感謝你。 – ElektroStudios

2

你需要做什麼看看resp.code並處理重定向:

Net::HTTP.start("your.web.site") do |http| 
    resp = http.get("/something") 
    while resp.code == '301' || resp.code == '302' 
    # moved permanently or temporarily: try again at the new location. 
    resp = http.get(URI.parse(resp.header['location'])) 
    # ideally you should also bail if you make too many redirects. 
    end 

    # make sure the request was successful. 
    if resp.code == '200' 
    open("test.wav", "wb"){ |file| file.write(resp.body) } 
    else 
    puts "Error: HTTP #{resp.code}" 
    end 
end 
+0

感謝,但我想我用錯了方式你的榜樣,我已經更新了我的第一個評論,如果你能幫助我mmore ...三江源。 PS:對不起,我的英語 – ElektroStudios

1

我建議你看這個指南here但要點是你必須檢查響應的類型,然後提取人體的URL重定向的情況。

現在,SoundCloud可能在後臺使用cookies和臨時URL等進行zaniness,但這是另一個需要解決的問題。

+0

感謝評論,我試過這種方式,但我的控制檯frezzes!我的控制檯打印了大量加密(我認爲)的信息,然後它凍結,iv'e使用了這個:google = RedirectFollower.new('http://soundcloud.com/dubstep-4/kill-paris-tender-love /download').resolve – ElektroStudios