Created
November 2, 2013 14:35
-
-
Save shrhdk/7279511 to your computer and use it in GitHub Desktop.
Download from specific URL and save it to the file. Example 1: wget('http://example.com/index.html', 'example.html') => example.html
Example 2: wget('http://example.com/index.html') => index.html
This file contains 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/ruby | |
# -*- mode:ruby; coding:utf-8 -*- | |
require 'open-uri' | |
def wget(url, fileName='') | |
if fileName == '' then | |
fileName = File.basename(url) | |
end | |
open(fileName, 'wb') do |output| | |
open(url) do |data| | |
output.write(data.read) | |
end | |
end | |
if File.exist?(fileName) then | |
return fileName | |
else | |
return nil | |
end | |
end | |
# main | |
if $0 == __FILE__ then | |
fileName = nil | |
if ARGV.length == 1 then | |
fileName = wget(ARGV[0]) | |
elsif ARGV.length == 2 then | |
wget(ARGV[0], ARGV[1]) | |
else | |
raise ArgumentError | |
end | |
if fileName != nil | |
puts ARGV[0] + ' => ' + fileName | |
exit 0 | |
else | |
puts 'Failed to save.' | |
exit 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment