Created
May 20, 2010 16:52
-
-
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
This file contains 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 '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