lørdag 12. mai 2012

Strava API for efforts per segment: Limitations and workarounds

The Strava API for downloading efforts per segment never returns more that 50 efforts. By including the option best=true, you get the 50 all time best efforts, i.e. the overall leaderboard from 1 to 50. However, this option does not work in combination with the startDate/endDate options. Thus, if you're interested in compiling leaderboards including athletes who are not among the top fifty, or if you want to show the best efforts from within a given timespan, you have to start doing a bit more work. Marc Durdin sums it up pretty good in this posting.

In essence what Marc is saying is that you have to call the API repeatedly, and whenever you get 50 efforts back, you split the time span in two halves and do two new calls. The lowest resolution available is per day, so what happens if you at the end are down to a day and you still get fifty efforts back? You're stuck. By combining this approach with a single call to the best=true, you're sure that you have the 50 all time best efforts + a substantial number of all efforts ever made. But for days when more than 50 efforts have been made, you are most certainly missing efforts.

Initially, I though this probably might not pose a problem, but I was wrong. A popular segment around here is Grefsenkollen, and on four occasions this spring 50 or more efforts have been made on this segment during a single day. The dates are:
  • 17th of April (Tuesday)
  • 19th of April (Thursday)
  • 24th of April (Tuesday)
  • 3rd of May (Thursday)

I thought these official numbers fra Strava HQ (posted December 2011) were interesting. One particulary interesting piece of information was that the fourth most active country in the world was Norway (Norway is of course not a very big country). So, this was 2011 - the numbers I'm seeing now indicate the usage has really risen for 2012, at least here in the Oslo area. I'll write a blog post about this sometime.

6 kommentarer:

  1. When Marc Durdin wrote the post you referenced, the "offset" parameter was not working properly. Strava fixed this a couple months ago and now you can use "offset" to retrieve all the results without doing the binary search, and without the problem of 50+ efforts in a day. Even big segments like Grefsenkollen work OK: I'm seeing 83 segments on May 3, for example. http://www.jonathanokeeffe.com/strava/segmentDetails.php?segmentId=660072

    - Jonathan

    SvarSlett
  2. Jonathan,

    Thanks!

    Too bad that I didn't discover this before I wrote my code and then wrote a blog post about it... At least it was fun writing the code.

    I'll be updating my code, and the posting soon.

    Ståle

    SvarSlett
  3. Hello

    I'm curious to know how the offset is supposed to be used, I'm making the call from Xcode and my url looks like this but I've had no success in making the offset to work:

    http://www.strava.com/api/v1/segments/229781/efforts?best=true&offset=50

    I've read on other blogs that you need to use ? instead of & but I'm not getting any results either.

    Any help would be appreciated.

    Thanks

    Martin

    SvarSlett
    Svar
    1. Hi,

      If I remember correctly, the offset doesn't work with the "best" parameter alone. What I do is to combine the offset parameter with "startDate" and "endDate", and that works.

      For example, the three urls below will returnn every single effort on the given segment on the date of March 3rd.

      http://www.strava.com/api/v1/segments/660072/efforts?endDate=2012-05-04&offset=0&startDate=2012-05-03
      http://www.strava.com/api/v1/segments/660072/efforts?endDate=2012-05-04&offset=50&startDate=2012-05-03
      http://www.strava.com/api/v1/segments/660072/efforts?endDate=2012-05-04&offset=100&startDate=2012-05-03

      Hope this helps!

      Slett
    2. Hello

      I have used the dates in the past and that works, I'm just curious to know why best=true and offset=50 doesn't work together. I've seen other blog posts saying it does.

      Thanks !

      Martin

      Slett
  4. Denne kommentaren har blitt fjernet av forfatteren.

    SvarSlett