Skip to content

Instantly share code, notes, and snippets.

@shrhdk
Created November 2, 2013 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrhdk/7279511 to your computer and use it in GitHub Desktop.
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
#!/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