Skip to content

Instantly share code, notes, and snippets.

@mateomurphy
Created August 28, 2010 05:04
Show Gist options
  • Save mateomurphy/554751 to your computer and use it in GitHub Desktop.
Save mateomurphy/554751 to your computer and use it in GitHub Desktop.
require 'net/http'
def check_response(response, expected)
raise "Unexpected response from server: #{response.code} #{response.message}" unless response.is_a?(expected)
end
def get_actual_download_url(download_url)
puts "requesting #{download_url}"
url = URI.parse(download_url)
response = Net::HTTP.start(url.host, url.port) { |http| http.get(url.path) }
check_response(response, Net::HTTPRedirection)
response['location']
end
def get_response(actual_download_url)
puts "requesting #{actual_download_url}"
url = URI.parse(actual_download_url)
response = Net::HTTP.start(url.host, url.port) { |http| http.head(url.path) }
check_response(response, Net::HTTPSuccess)
response
end
download_url = "http://soundcloud.com/omegadubstep/pink-floyd-brain-damage-omega-remix-v1-free-320-download/download"
actual_download_url = get_actual_download_url(download_url)
response = get_response(actual_download_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment