dblock / strava-ruby-client

A complete Ruby client for the Strava API v3.
https://code.dblock.org/2018/11/27/writing-a-new-strava-api-ruby-client.html
MIT License
97 stars 22 forks source link

Activity Upload #36

Closed interpegasus closed 3 years ago

interpegasus commented 3 years ago

I would like to know what might cause an activity upload status to remain on: "Your activity is still being processed."

Code

 def self.upload_activity_to_strava(user_id, hd_activity_id)
    user = User.find(user_id)
    hd_activity = HdActivity.find(hd_activity_id)
    activity_upload = nil
    if !user.blank? && !user.strava_auth_code.blank? && !hd_activity.blank?
      if user.strava_token_expiration < Time.now
        StravaMyModelClient.refresh_user_token(user_id)
        user.reload
      end
      client = Strava::Api::Client.new(
        access_token: user.strava_access_token
      )
      file_name = StravaMyModelClient.build_activity_gpx(hd_activity_id)
      activity_upload = client.create_upload(
        file: Faraday::UploadIO.new(file_name, 'application/gpx+xml'),
        data_type: 'gpx',
        external_id: 'strava-ruby-client-upload',
        name: "Virtual #{hd_activity.hd_race.display_name}",
        description: "#{hd_activity.name} #{hd_activity.metric_distance.to_f.round(2).to_s} m",
        type: 'Run'
      )
      puts activity_upload.status
    end
  end

Terminal file_name: /tmp/606f44b972b30251168f7b1f.gpx $ vim /tmp/606f44b972b30251168f7b1f.gpx $ cat /tmp/606f44b972b30251168f7b1f.gpx

<?xml version="1.0"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1" creator="GPX RubyGem 1.0.0 -- http://dougfales.github.io/gpx/" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
  <metadata>
    <name>606f44b972b30251168f7b1f.gpx</name>
    <time>2021-04-08T19:28:44+00:00</time>
    <bound minlat="90.0" minlon="180.0" maxlat="-90.0" maxlon="-180.0"/>
  </metadata>
  <wpt lat="34.14319964919367" lon="34.14319964919367">
    <time>2021-04-08T18:01:28+00:00</time>
    <ele>1802.0</ele>
    <name>Waypoint [34.14319964919367, -118.26633248905605]</name>
  </wpt>
  <wpt lat="34.14337272131732" lon="34.14337272131732">
    <time>2021-04-08T18:02:22+00:00</time>
    <ele>1805.0</ele>
    <name>Waypoint [34.14337272131732, -118.26525667999938]</name>
  </wpt>
  <wpt lat="34.143531606889205" lon="34.143531606889205">
    <time>2021-04-08T18:03:21+00:00</time>
    <ele>1805.0</ele>
    <name>Waypoint [34.143531606889205, -118.26426905201292]</name>
  </wpt>
  <wpt lat="34.14367772460674" lon="34.14367772460674">
    <time>2021-04-08T18:04:31+00:00</time>
    <ele>1806.0</ele>
    <name>Waypoint [34.14367772460674, -118.26336078698965]</name>
  </wpt>
  <wpt lat="34.14391055916302" lon="34.14391055916302">
    <time>2021-04-08T18:05:23+00:00</time>
    <ele>1802.0</ele>
    <name>Waypoint [34.14391055916302, -118.26231086281064]</name>
  </wpt>
  <wpt lat="34.14412607487465" lon="34.14412607487465">
    <time>2021-04-08T18:06:24+00:00</time>
    <ele>1803.0</ele>
    <name>Waypoint [34.14412607487465, -118.26133903216558]</name>
  </wpt>
  <wpt lat="34.14431672833044" lon="34.14431672833044">
    <time>2021-04-08T18:07:23+00:00</time>
    <ele>1808.0</ele>
    <name>Waypoint [34.14431672833044, -118.26041510279569]</name>
  </wpt>
  <wpt lat="34.14451073143204" lon="34.14451073143204">
    <time>2021-04-08T18:08:28+00:00</time>
    <ele>1800.0</ele>
    <name>Waypoint [34.14451073143204, -118.25939976463064]</name>
  </wpt>
  <wpt lat="34.14474356369262" lon="34.14474356369262">
    <time>2021-04-08T18:09:33+00:00</time>
    <ele>1800.0</ele>
    <name>Waypoint [34.14474356369262, -118.25834984045163]</name>
  </wpt>
  <wpt lat="34.14495907727932" lon="34.14495907727932">
    <time>2021-04-08T18:10:33+00:00</time>
    <ele>1803.0</ele>
    <name>Waypoint [34.14495907727932, -118.25737800980657]</name>
  </wpt>
  <wpt lat="34.14511658052343" lon="34.14511658052343">
    <time>2021-04-08T18:11:32+00:00</time>
    <ele>1804.0</ele>
    <name>Waypoint [34.14511658052343, -118.25629871502171]</name>
  </wpt>
  <wpt lat="34.14534500221772" lon="34.14534500221772">
    <time>2021-04-08T18:12:28+00:00</time>
    <ele>1804.0</ele>
    <name>Waypoint [34.14534500221772, -118.2551917430408]</name>
  </wpt>
  <wpt lat="34.14549475872339" lon="34.14549475872339">
    <time>2021-04-08T18:13:49+00:00</time>
    <ele>1804.0</ele>
    <name>Waypoint [34.14549475872339, -118.25416552832729]</name>
  </wpt>
  <wpt lat="34.14565226096881" lon="34.14565226096881">
    <time>2021-04-08T18:14:22+00:00</time>
    <ele>1807.0</ele>
    <name>Waypoint [34.14565226096881, -118.25308623354239]</name>
  </wpt>
  <wpt lat="34.14584291617914" lon="34.14584291617914">
    <time>2021-04-08T18:15:22+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.14584291617914, -118.25208840120777]</name>
  </wpt>
  <wpt lat="34.1459965447889" lon="34.1459965447889">
    <time>2021-04-08T18:16:31+00:00</time>
    <ele>1810.0</ele>
    <name>Waypoint [34.1459965447889, -118.2510356464586]</name>
  </wpt>
  <wpt lat="34.14612822623206" lon="34.14612822623206">
    <time>2021-04-08T18:17:21+00:00</time>
    <ele>1806.0</ele>
    <name>Waypoint [34.14612822623206, -118.25013328524503]</name>
  </wpt>
  <wpt lat="34.14630598463142" lon="34.14630598463142">
    <time>2021-04-08T18:18:23+00:00</time>
    <ele>1801.0</ele>
    <name>Waypoint [34.14630598463142, -118.24912279756417]</name>
  </wpt>
  <wpt lat="34.146503827443716" lon="34.146503827443716">
    <time>2021-04-08T18:19:30+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.146503827443716, -118.24816400293503]</name>
  </wpt>
  <wpt lat="34.14667608438103" lon="34.14667608438103">
    <time>2021-04-08T18:20:22+00:00</time>
    <ele>1803.0</ele>
    <name>Waypoint [34.14667608438103, -118.2472624526678]</name>
  </wpt>
  <wpt lat="34.14684765877311" lon="34.14684765877311">
    <time>2021-04-08T18:21:21+00:00</time>
    <ele>1808.0</ele>
    <name>Waypoint [34.14684765877311, -118.24628711238454]</name>
  </wpt>
  <wpt lat="34.147033144210404" lon="34.147033144210404">
    <time>2021-04-08T18:22:23+00:00</time>
    <ele>1808.0</ele>
    <name>Waypoint [34.147033144210404, -118.24523269045672]</name>
  </wpt>
  <wpt lat="34.1472137619943" lon="34.1472137619943">
    <time>2021-04-08T18:23:28+00:00</time>
    <ele>1806.0</ele>
    <name>Waypoint [34.1472137619943, -118.24428737561341]</name>
  </wpt>
  <wpt lat="34.147365547565386" lon="34.147365547565386">
    <time>2021-04-08T18:24:31+00:00</time>
    <ele>1806.0</ele>
    <name>Waypoint [34.147365547565386, -118.24334383816205]</name>
  </wpt>
  <wpt lat="34.14755954366568" lon="34.14755954366568">
    <time>2021-04-08T18:25:30+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.14755954366568, -118.242328499997]</name>
  </wpt>
  <wpt lat="34.14775918208439" lon="34.14775918208439">
    <time>2021-04-08T18:26:23+00:00</time>
    <ele>1801.0</ele>
    <name>Waypoint [34.14775918208439, -118.24136098905305]</name>
  </wpt>
  <wpt lat="34.147951625879244" lon="34.147951625879244">
    <time>2021-04-08T18:27:25+00:00</time>
    <ele>1806.0</ele>
    <name>Waypoint [34.147951625879244, -118.24042834336835]</name>
  </wpt>
  <wpt lat="34.14817675204603" lon="34.14817675204603">
    <time>2021-04-08T18:28:25+00:00</time>
    <ele>1802.0</ele>
    <name>Waypoint [34.14817675204603, -118.23941312742666]</name>
  </wpt>
  <wpt lat="34.14836907392115" lon="34.14836907392115">
    <time>2021-04-08T18:29:21+00:00</time>
    <ele>1808.0</ele>
    <name>Waypoint [34.14836907392115, -118.23840654217682]</name>
  </wpt>
  <wpt lat="34.14857495754123" lon="34.14857495754123">
    <time>2021-04-08T18:30:22+00:00</time>
    <ele>1807.0</ele>
    <name>Waypoint [34.14857495754123, -118.23747809682843]</name>
  </wpt>
  <wpt lat="34.14873241479673" lon="34.14873241479673">
    <time>2021-04-08T18:31:32+00:00</time>
    <ele>1807.0</ele>
    <name>Waypoint [34.14873241479673, -118.23649928694898]</name>
  </wpt>
  <wpt lat="34.14893637339585" lon="34.14893637339585">
    <time>2021-04-08T18:32:24+00:00</time>
    <ele>1800.0</ele>
    <name>Waypoint [34.14893637339585, -118.23557951865992]</name>
  </wpt>
  <wpt lat="34.1491485981498" lon="34.1491485981498">
    <time>2021-04-08T18:33:21+00:00</time>
    <ele>1802.0</ele>
    <name>Waypoint [34.1491485981498, -118.23455099351231]</name>
  </wpt>
  <wpt lat="34.14930747285886" lon="34.14930747285886">
    <time>2021-04-08T18:34:25+00:00</time>
    <ele>1802.0</ele>
    <name>Waypoint [34.14930747285886, -118.23356336552585]</name>
  </wpt>
  <wpt lat="34.14945205856929" lon="34.14945205856929">
    <time>2021-04-08T18:35:21+00:00</time>
    <ele>1804.0</ele>
    <name>Waypoint [34.14945205856929, -118.23257253752664]</name>
  </wpt>
  <wpt lat="34.14960438967502" lon="34.14960438967502">
    <time>2021-04-08T18:36:24+00:00</time>
    <ele>1807.0</ele>
    <name>Waypoint [34.14960438967502, -118.23152862945601]</name>
  </wpt>
  <wpt lat="34.14983528378196" lon="34.14983528378196">
    <time>2021-04-08T18:37:21+00:00</time>
    <ele>1810.0</ele>
    <name>Waypoint [34.14983528378196, -118.2304873823363]</name>
  </wpt>
  <wpt lat="34.14999557571077" lon="34.14999557571077">
    <time>2021-04-08T18:38:26+00:00</time>
    <ele>1810.0</ele>
    <name>Waypoint [34.14999557571077, -118.22949093624283]</name>
  </wpt>
  <wpt lat="34.15018723684638" lon="34.15018723684638">
    <time>2021-04-08T18:39:30+00:00</time>
    <ele>1808.0</ele>
    <name>Waypoint [34.15018723684638, -118.2284013669174]</name>
  </wpt>
  <wpt lat="34.15034731220097" lon="34.15034731220097">
    <time>2021-04-08T18:40:21+00:00</time>
    <ele>1808.0</ele>
    <name>Waypoint [34.15034731220097, -118.22730437877541]</name>
  </wpt>
  <wpt lat="34.15057571975148" lon="34.15057571975148">
    <time>2021-04-08T18:41:23+00:00</time>
    <ele>1804.0</ele>
    <name>Waypoint [34.15057571975148, -118.22619740679453]</name>
  </wpt>
  <wpt lat="34.15071513959741" lon="34.15071513959741">
    <time>2021-04-08T18:42:24+00:00</time>
    <ele>1806.0</ele>
    <name>Waypoint [34.15071513959741, -118.22524196550957]</name>
  </wpt>
  <wpt lat="34.15090937513301" lon="34.15090937513301">
    <time>2021-04-08T18:43:34+00:00</time>
    <ele>1802.0</ele>
    <name>Waypoint [34.15090937513301, -118.22430060351007]</name>
  </wpt>
  <wpt lat="34.151055249019635" lon="34.151055249019635">
    <time>2021-04-08T18:44:35+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.151055249019635, -118.22330092883229]</name>
  </wpt>
  <wpt lat="34.15126094278173" lon="34.15126094278173">
    <time>2021-04-08T18:45:25+00:00</time>
    <ele>1802.0</ele>
    <name>Waypoint [34.15126094278173, -118.22222432026072]</name>
  </wpt>
  <wpt lat="34.151415852450725" lon="34.151415852450725">
    <time>2021-04-08T18:46:24+00:00</time>
    <ele>1801.0</ele>
    <name>Waypoint [34.151415852450725, -118.22116271883299]</name>
  </wpt>
  <wpt lat="34.15155656182049" lon="34.15155656182049">
    <time>2021-04-08T18:47:28+00:00</time>
    <ele>1803.0</ele>
    <name>Waypoint [34.15155656182049, -118.22019843086946]</name>
  </wpt>
  <wpt lat="34.15173276317261" lon="34.15173276317261">
    <time>2021-04-08T18:48:33+00:00</time>
    <ele>1804.0</ele>
    <name>Waypoint [34.15173276317261, -118.21919673003804]</name>
  </wpt>
  <wpt lat="34.1518973069387" lon="34.1518973069387">
    <time>2021-04-08T18:49:29+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.1518973069387, -118.2181738296235]</name>
  </wpt>
  <wpt lat="34.152097409768984" lon="34.152097409768984">
    <time>2021-04-08T18:50:39+00:00</time>
    <ele>1804.0</ele>
    <name>Waypoint [34.152097409768984, -118.21727141545308]</name>
  </wpt>
  <wpt lat="34.15229164212581" lon="34.15229164212581">
    <time>2021-04-08T18:51:30+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.15229164212581, -118.21633005345359]</name>
  </wpt>
  <wpt lat="34.15253022510133" lon="34.15253022510133">
    <time>2021-04-08T18:52:22+00:00</time>
    <ele>1810.0</ele>
    <name>Waypoint [34.15253022510133, -118.21525409809657]</name>
  </wpt>
  <wpt lat="34.152710370438" lon="34.152710370438">
    <time>2021-04-08T18:53:29+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.152710370438, -118.2141341985048]</name>
  </wpt>
  <wpt lat="34.15284462319693" lon="34.15284462319693">
    <time>2021-04-08T18:54:25+00:00</time>
    <ele>1807.0</ele>
    <name>Waypoint [34.15284462319693, -118.21321414393411]</name>
  </wpt>
  <wpt lat="34.15305503970175" lon="34.15305503970175">
    <time>2021-04-08T18:55:35+00:00</time>
    <ele>1807.0</ele>
    <name>Waypoint [34.15305503970175, -118.2121943351013]</name>
  </wpt>
  <wpt lat="34.153218873151914" lon="34.153218873151914">
    <time>2021-04-08T18:56:32+00:00</time>
    <ele>1809.0</ele>
    <name>Waypoint [34.153218873151914, -118.21126292906504]</name>
  </wpt>
  <wpt lat="34.15336781102291" lon="34.15336781102291">
    <time>2021-04-08T18:57:25+00:00</time>
    <ele>1810.0</ele>
    <name>Waypoint [34.15336781102291, -118.21033702782772]</name>
  </wpt>
</gpx>

Thank you

dblock commented 3 years ago

This seems like a question for Strava, we're just a client library here. But if you use Strava UI activities uploaded don't get processed immediately AFAIK. Does the activity eventually get processed and become visible? Do you need to poll for it?

interpegasus commented 3 years ago

Thank you, You are right. The issue is mostly related to Strava not accepting the file. I tried the API Strava Playground and got an error.

1 2
interpegasus commented 3 years ago

To solve it I will have to recreate the GPX in a different way. Strava uses trkpt instead of wpt

interpegasus commented 3 years ago

Formatting the GPX XML file as follows solved the issue on Strava Swagger UI and strava-ruby-client

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" creator="StravaGPX" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1">
  <metadata>
    <time>2021-04-10 00:03:59 +0000</time>
  </metadata>
  <trk>
    <name>Virtual 5k at Night</name>
    <type>9</type>
    <trkseg>
      <trkpt lat="34.14318705871792" lon="-118.26639350325974">
        <time>2021-04-09T20:18:31+00:00</time>
        <ele>1808.0</ele>
        <name> App Waypoint 0</name>
      </trkpt>
      <trkpt lat="34.159117905321715" lon="-118.18918438667014">
        <time>2021-04-09T21:07:31+00:00</time>
        <ele>1806.0</ele>
        <name> App Waypoint 147</name>
      </trkpt>
      <trkpt lat="34.15921681178095" lon="-118.18870498935557">
        <time>2021-04-09T21:07:51+00:00</time>
        <ele>1807.0</ele>
        <name> App Waypoint 148</name>
      </trkpt>
      <trkpt lat="34.1593354993791" lon="-118.18812971257809">
        <time>2021-04-09T21:08:11+00:00</time>
        <ele>1803.0</ele>
        <name> App Waypoint 149</name>
      </trkpt>
    </trkseg>
  </trk>
</gpx>