Last active
December 14, 2015 01:09
-
-
Save voidet/5003930 to your computer and use it in GitHub Desktop.
Plug in your Garmin, wait till it mounts. Then run the script. Happy good times.
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 'rest-client' | |
require 'highline/import' | |
require 'json' | |
require 'time' | |
@activityCount = 0 | |
@uploadedCount = 0 | |
@garminPath = "/Volumes/GARMIN/Garmin/Activities" | |
def startup() | |
email = ask("Email: ") | |
password = ask("Password: ") { |p| p.echo = false } | |
if email && password | |
puts "Authenticating with Strava..." | |
begin | |
response = RestClient.post "https://www.strava.com/api/v2/authentication/login", { email: email, password: password }.to_json, :content_type => :json, :accept => :json | |
rescue Exception => e | |
puts "Uh oh. It failed. #{e.message}" | |
end | |
if response.code == 200 | |
user = JSON.parse(response.body) | |
puts "Your session token is: #{user["token"]}" | |
if !File.exists?("#{@garminPath}/.stravaTrakt") | |
File.open("#{@garminPath}/.stravaTrakt", "w") | |
end | |
dotTrakr = File.open("#{@garminPath}/.stravaTrakt", "r+") | |
timeLastSaved = dotTrakr.read | |
if timeLastSaved.length > 0 | |
timeLastSaved = Time.parse(timeLastSaved) | |
end | |
if !timeLastSaved.is_a?(Time) | |
files = Dir.glob("#{@garminPath}/*.fit") | |
@activityCount = files.length | |
files.each { |f| uploadFile(user, f) } | |
else | |
files = Dir.glob("#{@garminPath}/*.fit"). | |
select{|f| File.mtime(f) > timeLastSaved } | |
@activityCount = files.length | |
files.each{|f| uploadFile(user, f) } | |
end | |
dotTrakr.close | |
if @activityCount == 0 | |
puts "No activities to sync" | |
elsif @uploadedCount == @activityCount | |
timeLastSaved = Time.now | |
dotTrakr = File.open("#{@garminPath}/.stravaTrakt", "w+") | |
dotTrakr.write(timeLastSaved).close | |
puts "#{@activityCount} activities have been uploaded. Good work!" | |
else | |
puts "Some activities were skipped. Look into this" | |
end | |
end | |
end | |
end | |
def uploadFile(user, activity) | |
puts "Syncing Activity - #{File.basename(activity)} to Strava" | |
payload = { token: user["token"], data: File.new(activity, 'rb').read, type: "fit"} | |
begin | |
response = RestClient.post "http://www.strava.com/api/v2/upload", payload | |
if response.code == 200 | |
body = JSON.parse(response.body) | |
puts "Great Success! Upload ID: #{body["upload_id"]}" | |
@uploadedCount = @uploadedCount + 1 | |
end | |
rescue Exception => e | |
puts "Uh oh, something went wrong uploading the activity: #{e.message}" | |
end | |
end | |
startup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment