Skip to content

Instantly share code, notes, and snippets.

@KlausTrainer
Created July 8, 2011 16:34
Show Gist options
  • Save KlausTrainer/1072209 to your computer and use it in GitHub Desktop.
Save KlausTrainer/1072209 to your computer and use it in GitHub Desktop.
car2go2couch
#!/usr/bin/env ruby
require 'rubygems'
require 'rest_client'
require 'json'
require 'cgi'
def bye
abort("Usage: #{$0} [log_file(s)] [couch_db_url]\nExample: #{$0} log/* http://admin:secret@127.0.0.1:5984/db")
end
def put_it_into_the_couch
unless File.file?(ARGV[0]) && ARGV.last =~ /^https?:\/\/.+?\/.+?$/
bye
end
log_files = ARGV[0..ARGV.length-2]
db_bulk_doc_url = "#{ARGV.last.chomp("/")}/_bulk_docs"
log_files.each do |filename|
next unless File.file? filename
docs = []
unix_timestamp = filename.sub /[^\d]*(\d+).*/, '\1'
datetime = Time.at(unix_timestamp.to_i / 1000).utc.to_s
datetime = datetime.sub(/ /, 'T').sub(/ UTC/, 'Z')
json_in = JSON.parse(IO.read(filename))
json_in["placemarks"].each do |placemark|
doc = {}
doc[:datetime] = datetime
doc[:lng], doc[:lat] = JSON.parse placemark["coordinates"]
doc[:address] = placemark["address"]
doc[:exterior] = placemark["exterior"]
doc[:interior] = placemark["interior"]
doc[:fuel] = placemark["fuel"]
doc[:name] = placemark["name"]
doc[:vin] = placemark["vin"]
docs << doc
end
json_out = {"docs" => docs}.to_json
RestClient.post db_bulk_doc_url, json_out, :content_type => 'application/json'
end
end
put_it_into_the_couch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment