Skip to content

Commit

Permalink
Merge pull request #18 from jat255/sync_strava_notes
Browse files Browse the repository at this point in the history
add code to pull description from Strava API
  • Loading branch information
jat255 authored Oct 13, 2023
2 parents 7c39f36 + 0c2da9a commit 8875205
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions strava_to_fittrackee/s2f.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,43 @@ def auth(self):
logger.debug("Using existing Strava tokens with self-refreshing client")
return self.get_refreshing_client()

def get_detailed_activity(
self,
activity_dict: Dict
):
"""
Get the details for a specific activity from the Strava API.
Given an activity response from the ``/athlete/activities`` endpoint,
get the DetailedActivity
(https://developers.strava.com/docs/reference/#api-models-DetailedActivity)
representation of that activity (only used for getting the description,
currently).
Parameters:
-----------
activity_dict:
A dictionary representation of a SummaryActivity from the Strava
API (https://developers.strava.com/docs/reference/#api-models-SummaryActivity)
"""
success = False
while not success:
try:
logger.debug(
f"Getting activity details for activity {activity_dict['id']}"
)
r = self.client.get(
self.base_url + f"/activities/{activity_dict['id']}"
)
custom_raise_for_status(r)
success = True
except TooManyRequestsError:
logger.warning(
"Hit Strava API limit; sleeping until next 15 minute interval"
)
wait_until_fifteen()
return r.json()

def get_activities(
self,
limit: Union[int, None] = 30,
Expand Down Expand Up @@ -358,7 +395,7 @@ def get_activities(
"No more activities found "
f"(total activities: {len(all_activities)})"
)
return all_activities
return [self.get_detailed_activity(a) for a in all_activities]
else:
all_activities.extend(r.json())
logger.debug(
Expand Down Expand Up @@ -388,7 +425,7 @@ def get_activities(
)
wait_until_fifteen()
activities = r.json()
return activities
return [self.get_detailed_activity(a) for a in activities]

def get_gear(self, gear_id: str) -> Dict:
"""
Expand Down Expand Up @@ -448,15 +485,15 @@ def create_activity_from_strava(self, activity: dict, get_streams: bool = True):
)
custom_raise_for_status(r)
latlng = self.filter_response_by_key(r.json(), 'latlng', [(None, None)])
distance = self.filter_response_by_key(r.json(), 'distance', [0])
distance = self.filter_response_by_key(r.json(), 'distance', [0.0, activity['distance']])

logger.debug(f"Getting timepoints for activity {activity_id}")
r = self.client.get(
self.base_url + f"/activities/{activity_id}/streams",
params={"keys": ["time"]},
)
custom_raise_for_status(r)
time_list = self.filter_response_by_key(r.json(), 'time', [0])
time_list = self.filter_response_by_key(r.json(), 'time', [0.0, activity['moving_time']])

logger.debug(f"Getting altitude for activity {activity_id}")
r = self.client.get(
Expand All @@ -476,13 +513,15 @@ def create_activity_from_strava(self, activity: dict, get_streams: bool = True):
velocity = self.filter_response_by_key(r.json(), 'velocity_smooth', [None])
else:
latlng = [(None, None)]
distance = [0]
time_list = [0]
distance = [0.0, activity['distance']]
time_list = [0, activity['moving_time']]
altitude = [None]
velocity = [None]

# process gear (will be None if no gear defined on activity)
gear = self.get_gear(activity["gear_id"]) if activity["gear_id"] else None

description = activity["description"]

return Activity(
activity_dict=activity,
Expand All @@ -492,6 +531,7 @@ def create_activity_from_strava(self, activity: dict, get_streams: bool = True):
velocity=velocity,
distance=distance,
gear=gear,
description=description
)


Expand All @@ -505,6 +545,7 @@ def __init__(
velocity,
distance,
gear,
description,
):
self.title = activity_dict["name"]
self.activity_dict = activity_dict
Expand All @@ -521,6 +562,7 @@ def __init__(
self.link = f"https://strava.com/activities/{activity_dict['id']}"
self.gear = gear
self.gear_note = self.get_gear_note()
self.description = description

def as_dict(self) -> Dict:
return {
Expand All @@ -537,6 +579,7 @@ def as_dict(self) -> Dict:
'link': self.link,
'gear': self.gear,
'gear_note': self.gear_note,
'description': self.description,
}

def as_gpx(self) -> gpxpy.gpx.GPX:
Expand Down Expand Up @@ -827,6 +870,8 @@ def upload_gpx(self, gpx_file: Union[str, Path]):
with open(gpx_file, "w") as f:
print(gpx.to_xml(), file=f)
data["notes"] += activity_dict['gear_note']
data["notes"] += "\n\nStrava description:\n" + \
activity_dict['description'].replace('\r\n', '\n')

logger.debug(f"POSTing {gpx_file} to FitTrackee")
r = self.client.post(
Expand Down Expand Up @@ -872,6 +917,10 @@ def upload_no_gpx(self, activity: Activity):

data["notes"] += activity.gear_note

if activity.description:
data["notes"] += "\n\nStrava description:\n" + \
activity.description.replace('\r\n', '\n')

logger.debug(f"POSTing workout with no GPX to FitTrackee")
r = self.client.post(
self.base_url + "/workouts/no_gpx",
Expand Down

0 comments on commit 8875205

Please sign in to comment.