Retrieving activities for a date range

1,174 views
Skip to first unread message

Philippe Gauvin

unread,
Feb 14, 2014, 5:08:39 PM2/14/14
to strav...@googlegroups.com
I must be missing something but it seems I can't retrieve all activities for a date range. I can only specify a before OR after parameter. Is that correct? Any trick?

Paul Mach

unread,
Feb 14, 2014, 5:12:43 PM2/14/14
to Philippe Gauvin, strav...@googlegroups.com
There are two options.

1. use the "before" parameter and then a per_page of 100 or something. Assuming you'll have less than 100 activities in your range.
2. use "before", get 30 activities, if the last one isn't out of your range, do another request setting the before parameter as the start_date of the last activity. This will effectively give you the next page. Continue until you have all you need.

Dr. Paul Mach
STRAVA


On Fri, Feb 14, 2014 at 2:08 PM, Philippe Gauvin <gauvin....@gmail.com> wrote:
I must be missing something but it seems I can't retrieve all activities for a date range. I can only specify a before OR after parameter. Is that correct? Any trick?

--
You received this message because you are subscribed to the Google Groups "Strava API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strava-api+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jim Pravetz

unread,
Mar 21, 2014, 12:27:02 PM3/21/14
to strav...@googlegroups.com, Philippe Gauvin
Note that the max value for per_page appears to be 200 (determined by trial and error). 

Paul Mach

unread,
Mar 21, 2014, 1:43:26 PM3/21/14
to Jim Pravetz, strav...@googlegroups.com
No need for trial and error, it's in the documentation http://strava.github.io/api/#pagination

Dr. Paul Mach
STRAVA


For more options, visit https://groups.google.com/d/optout.

sascha...@googlemail.com

unread,
Mar 22, 2014, 3:04:08 AM3/22/14
to strav...@googlegroups.com
I've written this code to get all activities for a date range. You can modify it for your Programming language:

        /// <summary>
        /// Gets all the activities recorded in a specified period of time.
        /// </summary>
        /// <param name="after">Date after the activity was recorded.</param>
        /// <param name="before">Date before the activity was recorded.</param>
        /// <returns>A list of activities that was recorded between 'after' and 'before'.</returns>
        public List<Activity> GetActivities(DateTime after, DateTime before)
        {
            List<Activity> activities = new List<Activity>();
            int page = 1;
            bool hasEntries = true;

            while (hasEntries)
            {
                List<Activity> request = GetActivities(after, before, page++, 100);

                if (request.Count == 0)
                {
                    hasEntries = false;
                }

                foreach (Activity activity in request)
                {
                    activities.Add(activity);

                    if (ActivityReceived != null)
                    {
                        ActivityReceived(null, new ActivityReceivedEventArgs(activity));
                    }
                }
            }

            return activities;
        }

        /// <summary>
        /// Gets all the activities recorded in a specified period of time.
        /// </summary>
        /// <param name="after">Date after the activity was recorded.</param>
        /// <param name="before">Date before the activity was recorded.</param>
        /// <param name="page">Page of activities. Default value is 30.</param>
        /// <param name="perPage">Number of activities per page.</param>
        /// <returns>A list of activities that was recorded between 'after' and 'before'.</returns>
        public List<Activity> GetActivities(DateTime after, DateTime before, int page, int perPage)
        {
            String getUrl = String.Format("{0}?after={1}&before={2}&page={3}&per_page={4}&access_token={5}",
                Endpoints.Activities,
                DateConverter.GetSecondsSinceUnixEpoch(after),
                DateConverter.GetSecondsSinceUnixEpoch(before),
                page,
                perPage,
                Authentication.AccessToken);
            String json = WebRequest.SendGet(new Uri(getUrl));

            return Unmarshaller<List<Activity>>.Unmarshal(json);
        }
Reply all
Reply to author
Forward
Message has been deleted
0 new messages