user648198
user648198

Reputation: 2020

Is there a way to retrieve last-modified date from a image URL in Ruby?

Is there a way to retrieve the last-modified date from a image URL?

I can see the last modified date from the browser, and I think the last-modified date is inside of the HTTP header.

Thanks so much in advance.

Here is my code:

image_file  = open('http://www.example.com/image1.jpg')

Upvotes: 2

Views: 739

Answers (3)

the Tin Man
the Tin Man

Reputation: 160551

require 'open-uri'
require 'prettyprint'

open('http://www.iana.org/_img/2013.1/iana-logo-header.svg') do |f|
  pp f.meta
end

Run that and you'll get something like:

{"server"=>"Apache",
 "last-modified"=>"Fri, 04 Jan 2013 01:17:14 GMT",
 "content-type"=>"image/svg+xml",
 "content-length"=>"32870",
 "accept-ranges"=>"bytes",
 "date"=>"Wed, 16 Oct 2013 03:59:41 GMT",
 "x-varnish"=>"2012021384 2012020567",
 "age"=>"70",
 "via"=>"1.1 varnish",
 "connection"=>"keep-alive"}

Run something like:

require 'open-uri'

last_modified = open('http://www.iana.org/_img/2013.1/iana-logo-header.svg') do |f|
  f.last_modified
end
last_modified # => 2013-01-03 18:17:14 -0700

and you're done.

OpenURI's open accepts a block. Inside that block you have access to different methods that'll return information about the current connection.

From the documentation:

open("http://www.ruby-lang.org/en") {|f|
  f.each_line {|line| p line}
  p f.base_uri         # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
  p f.content_type     # "text/html"
  p f.charset          # "iso-8859-1"
  p f.content_encoding # []
  p f.last_modified    # Thu Dec 05 02:45:02 UTC 2002
}     

Also see the OpenURI::Meta documentation for additional information, such as last_modified.

Upvotes: 3

AGS
AGS

Reputation: 14498

require 'mechanize'

agent = Mechanize.new

modified_date = agent.get("http://example.com/image1.jpg").response["last-modified"]

Upvotes: 1

Nick Veys
Nick Veys

Reputation: 23939

You betcha... Can't use open-uri though.

require 'net/http'

http = Net::HTTP.new('www.example.com', 80)
resp = http.request_get('/image1.jpg')
date = resp['last-modified']

If you want to read the file as well, you can do it in a block...

http.request_get('/index.html') do |resp|
  resp.read_body do |str|
    # ...
  end
end

Upvotes: 2

Related Questions