Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created May 20, 2010 16:52
Show Gist options
  • Save max-mapper/407798 to your computer and use it in GitHub Desktop.
Save max-mapper/407798 to your computer and use it in GitHub Desktop.
gets data from instamapper and saves it to your google latitude account
require 'rubygems'
require 'json'
require 'net/http'
require 'redis'
require 'oauth'
@redis = Redis.new
@instamapper_api_key = "YOUR INSTAMAPPER API KEY"
@consumer_key = "GOOGLE OAUTH CONSUMER KEY"
@consumer_secret = "GOOGLE OAUTH CONSUMER SECRET"
@oauth_token_secret = "GOOGLE OAUTH AUTHENTICATED ACCESS TOKEN SECRET"
@oauth_token = "GOOGLE OAUTH AUTHENTICATED ACCESS TOKEN"
url = "http://www.instamapper.com/api?action=getPositions&key=#{@instamapper_api_key}&num=10&format=json"
resp = Net::HTTP.get_response(URI.parse(url))
points = JSON.parse(resp.body)
if points.has_key? 'Error'
raise "web service error"
end
positions = points["positions"]
last_location_timestamp = @redis["locations:max:last_location"].to_i ||= 1
last_location = positions.find {|point| point["timestamp"].to_i == last_location_timestamp}
if last_location
positions = positions[positions.index(last_location)+1..-1]
end
if positions.length > 0
puts "#{Time.now} #{positions.length} new points"
@redis["locations:max:last_location"] = positions.last["timestamp"]
@consumer = OAuth::Consumer.new(@consumer_key, @consumer_secret, {:site => 'https://www.google.com',
:request_token_path => '/accounts/OAuthGetRequestToken',
:access_token_path => '/accounts/OAuthGetAccessToken',
:authorize_path => '/accounts/OAuthAuthorizeToken'})
@access_token = OAuth::AccessToken.new(@consumer, @oauth_token, @oauth_token_secret)
positions.each do |point|
glatlng = {"data" => {"kind" => "latitude#location",
"latitude" => point["latitude"],
"longitude" => point["longitude"],
"altitude" => point["altitude"].to_i,
"timestampMs" => "#{point["timestamp"]}000"
}
}
@access_token.post('https://www.googleapis.com/latitude/v1/location', glatlng.to_json, {'Content-Type' => 'application/json'})
end
else
puts "#{Time.now} no new points"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment