Quakespotter.app/lib/lib/scraper.rb
# Attempt fetch a resource from the web, cache it locally.
Attempt fetch a resource from the web, cache it locally.
Attempt fetch a resource from the web cache it locally .
def fetch(resource)
    local_path = "data/sources/#{resource}.xml"
    xml = nil
    begin
      if File.exists?(local_path) && File.mtime(local_path) > (Time.now - 300) 
        xml = File.read(local_path)
      else
        Timeout.timeout(REQUEST_TIMEOUT) { xml = open(SOURCES[resource]).read }
        File.open(local_path, 'w+') {|f| f.write(xml) }
      end
    rescue Timeout::Error, OpenURI::HTTPError => e
      xml = File.read(local_path) if File.exists? local_path
    end
    raise "Could not fetch #{resource}" unless xml
    Hpricot xml
  end
def fetch ( resource ) local_path = "data/sources/#{resource}.xml" xml = nil begin if File . exists? ( local_path ) && File . mtime ( local_path ) > ( Time . now - 300 ) xml = File . read ( local_path ) else Timeout . timeout ( REQUEST_TIMEOUT ) { xml = open ( SOURCES [ resource ] ) . read } File . open ( local_path , 'w+' ) { | f | f . write ( xml ) } end rescue Timeout :: Error , OpenURI :: HTTPError => e xml = File . read ( local_path ) if File . exists? local_path end raise "Could not fetch #{resource}" unless xml Hpricot xml end
Attempt fetch a resource from the web, cache it locally.
Attempt fetch a resource from the web cache it locally .
# More than 5 minutes ago.
resource