From b82e4fc7c0b109adc7b4ac499ad78190681835ce Mon Sep 17 00:00:00 2001 From: Serg Melikyan Date: Thu, 27 Jul 2017 14:39:41 -0700 Subject: [PATCH] Collect calories stat from Strava during export Strava does provide information about how many calories user spent during activity only when details on specific activity is requested. Additional API call was added to request activity details during downloading activity in order to obtain calories information. Previous behavior was to collect calories stats from the response to request of the list of user activities which doesn't contain this information. Closes #353 --- tapiriik/services/Strava/strava.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tapiriik/services/Strava/strava.py b/tapiriik/services/Strava/strava.py index f466b3769..201c97b1b 100644 --- a/tapiriik/services/Strava/strava.py +++ b/tapiriik/services/Strava/strava.py @@ -174,8 +174,6 @@ def DownloadActivityList(self, svcRecord, exhaustive=False): activity.Stats.Cadence.update(ActivityStatistic(ActivityStatisticUnit.RevolutionsPerMinute, avg=ride["average_cadence"])) if "average_temp" in ride: activity.Stats.Temperature.update(ActivityStatistic(ActivityStatisticUnit.DegreesCelcius, avg=ride["average_temp"])) - if "calories" in ride: - activity.Stats.Energy = ActivityStatistic(ActivityStatisticUnit.Kilocalories, value=ride["calories"]) activity.Name = ride["name"] activity.Private = ride["private"] activity.Stationary = ride["manual"] @@ -216,6 +214,18 @@ def DownloadActivity(self, svcRecord, activity): return activity activityID = activity.ServiceData["ActivityID"] + #Note (smelikyan): request activity details in order to extract calories stat + details = requests.get("https://www.strava.com/api/v3/activities/" + str(activityID), headers=self._apiHeaders(svcRecord)) + if details.status_code == 401: + raise APIException("No authorization to download activity", block=True, user_exception=UserException(UserExceptionType.Authorization, intervention_required=True)) + try: + details = details.json() + except ValueError: + raise APIException("Activity details data returned is not JSON") + + if "calories" in details: + activity.Stats.Energy = ActivityStatistic(ActivityStatisticUnit.Kilocalories, value=details["calories"]) + streamdata = requests.get("https://www.strava.com/api/v3/activities/" + str(activityID) + "/streams/time,altitude,heartrate,cadence,watts,temp,moving,latlng,distance,velocity_smooth", headers=self._apiHeaders(svcRecord)) if streamdata.status_code == 401: raise APIException("No authorization to download activity", block=True, user_exception=UserException(UserExceptionType.Authorization, intervention_required=True))