0

I wrote a ruby script to download an image URL:

require 'open-uri'

imageAddress = ARGV[0]
targetPath = ARGV[1]

fullFileNamePath = "#{targetPath}test.jpg"

  begin
    File.open(fullFileNamePath, 'wb') do |fo|
        fo.write open(imageAddress).read
      end
  rescue OpenURI::HTTPError => ex
    puts ex
    File.delete(fullFileNamePath)
  end

Example Usage:

ruby download_image.rb "https://images.genius.com/b015b15e476c92d10a834d523575d3c9.1000x1000x1.jpg" "/Users/Me/Downloads/"

The problem is, sometimes I run across this output error:

520 Origin Error

Then, when I try the same URL in my browser, I get something like this:

enter image description here

If I reload the page or click the 'Retry for a live version' button in the above image, the page loads.

Then if I run the script again it downloads the image just fine.

So how can I replicate this page reload / 'Retry for a live version' behavior using ruby and without switching to my browser? Running the script again doesn't do the job.

kraftydevil
  • 5,144
  • 6
  • 43
  • 65

1 Answers1

0

It sounds like you are looking for a delay command. If the script fails (or encounters '520 Origin Error') wait and re-try.

This is a quick built recursive function, you may want to add other checks for how many times you have looped, breaking after so many. (Also not tested, may contain errors, meant as an example)

def getFile(params_you_need)
   begin
      File.open(fullFileNamePath, 'wb') do |fo|
      fo.write open(imageAddress).read
   end
   rescue OpenURI::HTTPError => ex
     puts ex
     File.delete(fullFileNamePath)
      if ex == '520 Origin Error'
        sleep(30) #generally a good time to pause
        getFile(params_you_need)
      end
   end
end
Jon
  • 1,954
  • 1
  • 15
  • 13
  • Turns out it doesn't matter how long you wait. It only works if you try it and reload in the browser and THEN invoke the script again. – kraftydevil Apr 17 '17 at 18:49
  • One last idea (well a couple) Depending on how far you want to go with this. You could try to execute wget or curl via the command line using system, an example would be 'system "wget url_to_file_or_image" ' Nokoguri has worked for me in the past, it has some nice features to connect to external sources. – Jon Apr 17 '17 at 23:46