Created
September 10, 2008 03:40
-
-
Save acoffman/9816 to your computer and use it in GitHub Desktop.
Really basic script for downloading a file from the web and saving it to a specified location.
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
#ruby script to download a file from the command line given a url | |
# | |
#works with two arguments: | |
#argument 1 is the path to the file, you can leave | |
#the http on or remove it, the script handles both | |
# | |
#second argument is the name of the file as you want it to be saved on disk | |
#i.e. download.zip | |
# | |
#KNOWN BUG: will not work given an IP instead of a url | |
require 'net/http' | |
#pull the file url from the first argument | |
full_url = ARGV[0] | |
#split file path on / characters and check to see if http:// was entered | |
url_array = full_url.split('/') | |
if url_array[0].eql? "http:" | |
domain = url_array[2] | |
start_count = 3 | |
else | |
domain = url_array[0] | |
start_count = 1 | |
end | |
#now that we have the domain, rebuild the path to the file | |
path = "" | |
start_count.upto(url_array.length() -1) do |x| | |
path << '/' | |
path << url_array[x] | |
end | |
#now that we have the domain and path to the file, download and save | |
Net::HTTP.start(domain) do |http| | |
resp = http.get(path) | |
open(ARGV[1], "wb") do |file| | |
file.write(resp.body) | |
end | |
end | |
puts "File Download Complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment