Skip to content

Commit

Permalink
Use modern activity type listing, fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Abadie authored Apr 6, 2018
2 parents 2bdd407 + 1d8338f commit f4adef9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
25 changes: 10 additions & 15 deletions garmin_uploader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
URL_HOST_CONNECT = 'connect.garmin.com'
URL_UPLOAD = 'https://connect.garmin.com/modern/proxy/upload-service/upload'
URL_ACTIVITY_BASE = 'https://connect.garmin.com/modern/proxy/activity-service/activity' # noqa
URL_ACTIVITY_TYPE = 'https://connect.garmin.com/proxy/activity-service-1.2/json/type' # noqa
URL_ACTIVITY_TYPES = 'https://connect.garmin.com/proxy/activity-service-1.2/json/activity_types' # noqa
URL_ACTIVITY_TYPES = 'https://connect.garmin.com/modern/proxy/activity-service/activity/activityTypes' # noqa


class GarminAPIException(Exception):
Expand Down Expand Up @@ -197,17 +196,14 @@ def load_activity_types(self):
if self.activity_types:
return self.activity_types

logger.debug('Fecthing activity types')
logger.debug('Fetching activity types')
resp = requests.get(URL_ACTIVITY_TYPES)
if not resp.ok:
raise GarminAPIException('Failed to retrieve activity types')

# Store as a clean dict, mapping keys and lower case common name
types = resp.json()["dictionary"]
out = [(t['key'], t['key']) for t in types]
out += [(t['display'].lower(), t['key']) for t in types]
out = dict(out)
self.activity_types = out
types = resp.json()
self.activity_types = {t['typeKey']: t for t in types}

logger.debug('Fetched {} activity types'.format(len(self.activity_types))) # noqa
return self.activity_types
Expand All @@ -226,14 +222,13 @@ def set_activity_type(self, session, activity):
logger.error("Activity type '{}' not valid".format(activity.type))
return False

url = '{}/{}'.format(URL_ACTIVITY_TYPE, activity.id)
url = '{}/{}'.format(URL_ACTIVITY_BASE, activity.id)
data = {
'value': type_key,
'activityId': activity.id,
'activityTypeDTO': type_key
}
res = session.post(url, data)
headers = dict(self.common_headers) # clone
headers['X-HTTP-Method-Override'] = 'PUT' # weird. again.
res = session.post(url, json=data, headers=headers)
if not res.ok:
raise GarminAPIException('Activity type not set: {}'.format(res.content)) # noqa

res = res.json()
if "activityType" not in res or res["activityType"]["key"] != type_key:
raise GarminAPIException('Activity type not set: {}'.format(res.content)) # noqa
42 changes: 30 additions & 12 deletions garmin_uploader/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,37 @@
The following list of activity types should be valid for setting
your activity type on Garmin Connect...

running, street_running, track_running, trail_running,
treadmill_running, cycling, cyclocross, downhill_biking, indoor_cycling,
mountain_biking, recumbent_cycling, road_biking, track_cycling,
fitness_equipment, elliptical, , indoor_cardio, indoor_rowing,
stair_climbing, strength_training, hiking, swimming, lap_swimming,
open_water_swimming, walking, casual_walking, speed_walking, transition,
swimToBikeTransition, bikeToRunTransition, runToBikeTransition,
motorcycling, other, backcountry_skiing_snowboarding, boating,
cross_country_skiing, driving_general, flying, golf, horseback_riding,
inline_skating, mountaineering, paddling, resort_skiing_snowboarding,
rowing, sailing, skate_skiing, skating, snowmobiling, snow_shoe,
stand_up_paddleboarding, whitewater_rafting_kayaking, wind_kite_surfing.
uncategorized

cycling, bmx, cyclocross, downhill_biking, gravel_cycling,
indoor_cycling, mountain_biking, recumbent_cycling, road_biking,
track_cycling, virtual_ride

running, indoor_running, obstacle_run, street_running, track_running,
trail_running, treadmill_running, virtual_run

fitness_equipment, elliptical, indoor_cardio, indoor_rowing,
stair_climbing, strength_training

hiking, walking, casual_walking, speed_walking

swimming, lap_swimming, open_water_swimming

other, backcountry_skiing_snowboarding, boating, cross_country_skiing,
driving_general, floor_climbing, flying, golf, hang_gliding,
horseback_riding, hunting_fishing, inline_skating, mountaineering,
paddling, rc_drone, resort_skiing_snowboarding, rock_climbing, rowing,
sailing, skate_skiing, skating, sky_diving, snow_shoe, snowmobiling,
stand_up_paddleboarding, stop_watch, surfing, tennis, wakeboarding,
whitewater_rafting_kayaking, wind_kite_surfing, wingsuit_flying, yoga

transition, bikeToRunTransition, runToBikeTransition,
swimToBikeTransition

motorcycling, atv, motocross

diving, apnea_diving, apnea_hunting, gauge_diving, multi_gas_diving,
single_gas_diving

Examples:
Upload file and set activty name:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def test_types(api):
assert 'all' in types
assert 'running' in types
assert 'track_cycling' in types
assert 'track cycling' in types
assert types['track_cycling'] == types['track cycling']


def test_rename(api, user, sample_activity):
Expand Down

0 comments on commit f4adef9

Please sign in to comment.