Created
November 29, 2013 17:19
-
-
Save bds/7708987 to your computer and use it in GitHub Desktop.
Fetches static image from the City of Palo Alto "Creek Camera - San Francisquito Creek at West Bayshore Road", respecting response e-tag.
This file contains hidden or 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
require 'open-uri' | |
require 'digest/sha1' | |
class CreekImage | |
attr_reader :etag, :data, :uri, :sha1 | |
def initialize | |
@uri = 'http://archive.cityofpaloalto.org/earlywarning/creekcam/creekcam.jpg' | |
open(@uri) do |img| | |
@etag = img.meta['etag'] | |
@data = img.read | |
@sha1 = Digest::SHA1.hexdigest @data | |
end | |
write | |
end | |
def write | |
raise "data is empty" unless @data | |
filename = Time.now.strftime("%Y%m%dT%H%M%S%z") + ".jpg" | |
File.open(filename, 'wb') {|fo| fo.write @data} | |
puts "#{Time.now} Created #{filename} with SHA-1 #{@sha1}" | |
end | |
def update | |
begin | |
open(@uri, 'If-None-Match' => @etag) do |img| | |
@etag = img.meta['etag'] | |
@data = img.read | |
@sha1 = Digest::SHA1.hexdigest @data | |
end | |
write | |
rescue OpenURI::HTTPError | |
puts "#{Time.now} No file available or file has not changed." | |
end | |
end | |
end | |
# Reads image into memory, sets etag, writes file | |
@img = CreekImage.new | |
# Update every 5-minutes | |
20.times do | |
@img.update | |
sleep 300 | |
end | |
# If you have ImageMagick installed you can animate: | |
# $ convert *.jpg -loop 0 creek.gif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment