Sha256: 40e111fa96bb7051ed81e2b2ab3860b4a4f5df5c207f5104bb4cef3094fdcdc0

Contents?: true

Size: 877 Bytes

Versions: 7

Compression:

Stored size: 877 Bytes

Contents

require 'open-uri'
require 'fileutils'

fail 'Please pass a URL to this program.' if ARGV[0].empty?

def download_url(url)
  response = open(url)

      puts "Downloading #{url}"

  fail "Error downloading #{url}" unless response.status[0] == '200'

  handle_file url, response.read
end

def handle_file(url, body)
  filename = url.gsub 'http://gd2.mlb.com/components/game/mlb/', ''

  if body['Index of']
    body.scan(%r{<li><a href="([^"]+)">\s*(.*)</a></li>}) do |match|
      # Only the "Parent Directory" link is different
      next if match[0] != match[1]

      download_url("#{url}/#{match[0]}")
    end
  else
    # Save the file
    save_file filename, body
  end
end

def save_file(filename, body)
  save_path = File.join Dir.pwd, filename
  FileUtils.mkdir_p File.dirname save_path
  File.open(save_path, 'w') { |file| file.write body }
end

download_url ARGV[0]

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mlb_gameday-0.1.9 test/download.rb
mlb_gameday-0.1.8 test/download.rb
mlb_gameday-0.1.7 test/download.rb
mlb_gameday-0.1.6 test/download.rb
mlb_gameday-0.1.5 test/download.rb
mlb_gameday-0.1.4 test/download.rb
mlb_gameday-0.1.3 test/download.rb