Create a gist now

Instantly share code, notes, and snippets.

@bds /creek.rb
Created Nov 29, 2013

What would you like to do?
Fetches static image from the City of Palo Alto "Creek Camera - San Francisquito Creek at West Bayshore Road", respecting response e-tag.
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