Skip to content

Commit

Permalink
Add support for getting gear attached to an activity
Browse files Browse the repository at this point in the history
Uses the `gear-service` to get a list of JSON objects

Example data:

```json
[
    {
        "gearPk": 12345678,
        "uuid": "abcdef12345678abcdef12345678",
        "userProfilePk": 12345,
        "gearMakeName": "Other",
        "gearModelName": "Unknown Bike",
        "gearTypeName": "Bike",
        "gearStatusName": "active",
        "displayName": "gray",
        "customMakeModel": "Marin Headlands 1",
        "imageNameLarge": null,
        "imageNameMedium": null,
        "imageNameSmall": null,
        "dateBegin": "2022-02-18T00:00:00.0",
        "dateEnd": null,
        "maximumMeters": 643737.6000000001,
        "notified": true,
        "createDate": "2022-06-23T18:37:07.0",
        "updateDate": "2022-07-10T18:03:00.0"
    },
    {
        "gearPk": 12345679,
        "uuid": "bcdef12345678abcdef12345678a",
        "userProfilePk": 12345,
        "gearMakeName": "Other",
        "gearModelName": "Unknown Shoes",
        "gearTypeName": "Shoes",
        "gearStatusName": "active",
        "displayName": "bike",
        "customMakeModel": "Shimano RC3",
        "imageNameLarge": null,
        "imageNameMedium": null,
        "imageNameSmall": null,
        "dateBegin": "2022-05-17T00:00:00.0",
        "dateEnd": null,
        "maximumMeters": 643738.0,
        "notified": true,
        "createDate": "2022-06-23T18:39:00.0",
        "updateDate": "2022-07-10T18:03:00.0"
    }
]
```
  • Loading branch information
drewbrew committed Nov 12, 2023
1 parent d445a4d commit 8268718
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
17 changes: 16 additions & 1 deletion garminexport/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@

log = logging.getLogger(__name__)

supported_export_formats = ["json_summary", "json_details", "gpx", "tcx", "fit"]
supported_export_formats = [
"json_summary",
"json_details",
"gear",
"gpx",
"tcx",
"fit",
]
"""The range of supported export formats for activities."""

format_suffix = {
"json_summary": "_summary.json",
"json_details": "_details.json",
"gear": "_gear.json",
"gpx": ".gpx",
"tcx": ".tcx",
"fit": ".fit"
Expand Down Expand Up @@ -132,6 +140,13 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
with codecs.open(dest, encoding="utf-8", mode="w") as f:
f.write(json.dumps(activity_details, ensure_ascii=False, indent=4))

if 'gear' in export_formats:
log.debug('getting gear for %s', id)
activity_gear = retryer.call(client.get_activity_gear, id)
dest = os.path.join(backup_dir, export_filename(activity, 'gear'))
with codecs.open(dest, encoding='utf-8', mode='w') as f:
f.write(json.dumps(activity_gear, ensure_ascii=False, indent=4))

not_found_path = os.path.join(backup_dir, not_found_file)
with open(not_found_path, mode="a") as not_found:
if 'gpx' in export_formats:
Expand Down
23 changes: 23 additions & 0 deletions garminexport/garminclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,29 @@ def get_activity_tcx(self, activity_id):
activity_id, response.status_code, response.text))
return response.text

@require_session
def get_activity_gear(self, activity_id):
"""Return a list of JSON objects describing the gear used in an activity,
if any. If there is no gear attached, the result will be an empty list.
:param activity_id: Activity identifier.
:type activity_id: int
:returns: The JSON object describing the gear or ``None`` if there is
no gear attached
:rtype list
"""
response = self.session.get(
f'https://connect.garmin.com/proxy/gear-service/gear/filterGear?activityId={activity_id}',
)
if response.status_code == 400:
return None
if response.status_code != 200:
raise Exception(
f'failed to fetch gear for activity {activity_id}: {response.status_code}\n'
f'{response.text}'
)
return response.json()

def get_original_activity(self, activity_id):
"""Return the original file that was uploaded for an activity.
If the activity doesn't have any file source (for example,
Expand Down

0 comments on commit 8268718

Please sign in to comment.