Skip to content

Instantly share code, notes, and snippets.

@voidet
Last active December 25, 2015 01:59
Show Gist options
  • Save voidet/6899647 to your computer and use it in GitHub Desktop.
Save voidet/6899647 to your computer and use it in GitHub Desktop.
GarminConnect 2 Strava
#!/usr/bin/env ruby
require 'net/smtp'
require 'optparse'
require "rubygems"
require "json"
require "net/http"
require "uri"
#######
## OPTIONS
smtpUsername = ""
smtpPassword = ""
smtpAddress = ""
smtpPort = 587
fromAddress = ""
garminConnectUsername = ""
#####
uri = URI.parse("http://connect.garmin.com/proxy/activitylist-service/activities/#{garminConnectUsername}?start=1&limit=1")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if response.code == "200"
lastRide = JSON.parse(response.body)
ride = lastRide["activityList"][0]
begin
lastActId = File.open("lastride", "rb"){|io| io.read}
rescue
lastActId = ""
end
if !lastActId || lastActId.to_s != ride['activityId'].to_s
File.open("lastride", 'w') {|f| f.write(ride['activityId']) }
uri = URI.parse("http://connect.garmin.com/proxy/activity-service-1.1/gpx/activity/#{ride['activityId']}?full=true")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
gpx = http.request(request).body
if gpx
File.open("ride.gpx", 'w') {|f| f.write(gpx) }
end
end
end
boundary = (0...12).map{ ('a'..'z').to_a[rand(26)] }.join
message = <<MESSAGE_BODY
To: upload@strava.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{boundary}
--#{boundary}
Content-Type: application/octet-stream; name="ride.gpx"
Content-Disposition: attachment; filename="ride.gpx"
Content-Transfer-Encoding:base64
#{[File.read("ride.gpx")].pack("m")}
--#{boundary}--
MESSAGE_BODY
if gpx
smtp = Net::SMTP.new(smtpAddress, smtpPort)
smtp.enable_starttls()
smtp.start(fromAddress.split("@")[1], smtpUsername, smtpPassword, :login)
smtp.send_message(message, fromAddress, "upload@strava.com")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment