petergardfjall / garminexport

Garmin Connect activity exporter and backup tool
Apache License 2.0
500 stars 85 forks source link

Build badge PyPi release PyPI - Python Version PyPI - License

About

garminexport is both a library and a tool for downloading/backing up Garmin Connect activities to a local disk.

The main utility script is called garmin-backup and performs incremental backups of your Garmin account to a local directory. The first time garmin-backup is run, it will download all activities. After that, it will do incremental backups of your account. That is, the script will only download activities that haven't already been downloaded to the backup directory.

Installation

garminexport is available on PyPi and can be installed with pip.

Vanilla installation

WARNING

GarminConnect employs Cloudflare's bot protection to prevent scripted access to their services. Therefore a vanilla installation is no longer likely to work. Instead, try the Browser-impersonating installation below.

To only install garminexport and required dependencies run:

pip install garminexport

Browser-impersonating installation

To install garminexport with support to circumvent Cloudflare's bot protection you should install the module with the impersonate_browser extra like so:

pip install 'garminexport[impersonate_browser]'

This replaces the default requests library with curl_cffi for HTTP session handling.

When curl_cffi is used, the GARMINEXPORT_IMPERSONATE_BROWSER environment variable can be used to control which browser is impersonated (default is chrome110, see full list).

Usage

Prerequisites

To be of any use you need to register an account at Garmin Connect and populate it with some activities.

As a command-line tool (garmin-backup)

The backup program is run as follows (use the --help flag for a full list of available options):

garmin-backup --backup-dir=activities <username or email>

Once started, the program will prompt you for your account password and then log in to your Garmin Connect account to download activities to the specified backup directory on your machine. The program will only download activities that aren't already in the backup directory.

Activities can be exported in any of the formats outlined below. Note that by default, the program downloads all formats for every activity. Use the --format option to narrow the selection.

Supported export formats:

All files are written to the same directory (activities/ by default). Each activity file is prefixed by its upload timestamp and its activity id.

garminexport also contains a few smaller utility programs:

As a library

To build your own tools around the Garmin Connect API you can import the garminclient module. It handles authentication to establish a secure session with Garmin Connect. For example use, have a look at the command-line tools under garminexport/cli.

For example, in your setup.py, setup.cfg, pyproject.toml (PEP 631) add something like:

install_requires=[
    'garminexport',
    # also installs 'impersonate_browser as a dependency
    # 'garminexport[impersonate_browser]',
    ...
]

Note: if you happen to have cloudscraper on your PACKAGEPATH GarminClient will make use of it whenever it needs to make an HTTP request.

Contribute

To start working on the code, create a virtual environment (an isolated development environment) and install the required dependencies like so:

# create virtualenv and populate it with library dependencies
make dev-init

# activate virtualenv
source .venv/bin/activate

# test
make test