Bosch e-bike connect - fetch statistics with Python

I’m a tinkerer. I like to experiment, break stuff, improve. I like to analyze how things work in general. When I was a kid, this often resulted in a shock for my mother whenever she discovered I completely disassembled something. I think I was five years old when I decided to take apart my bicycle. At the age of fourteen, when the first computer entered our house, it probably took two hours and the thing was in pieces on the floor. Luckily, for me, I was able to re-assemble the system.

When I received my e-bike yesterday (Sparta d-burst energy metb) one of the first things I got interested in is how the portal of Bosch contains all the Kiox computer data. I’m no data scientist but I immediately felt the urge to somehow extract that information from the portal.

Twenty minutes later and I hacked together a Python script to login on the website, grab the authentication token and request the statistics. I have no clue yet what I actually want to do with this data. I haven’t logged any activities yet, so perhaps I need to further adapt the script to also fetch your activity data.

Perhaps I can migrate the activity data together with weather data and do some statistics / analysis on how often there was rainfall during my commutes to work for instance. To be continued!

Python script to fetch the Bosch e-bike connect data

#!/usr/bin/python
import random, requests

BOSCH_URLS = {
    'login': 'https://www.ebike-connect.com/ebikeconnect/api/portal/login/public',
    'statistics': 'https://www.ebike-connect.com/ebikeconnect/api/portal/statistics/box'
}

BOSCH_CREDENTIALS = {
    'username': 'user@example.com',
    'password': 'example_password'
}

class BoschEbike:
    data = {}

    def __init__(self):
        self.current_session = requests.Session()
        self.login()

    def login(self):
        payload = {
            'username': BOSCH_CREDENTIALS['username'],
            'password': BOSCH_CREDENTIALS['password'],
            'rememberme': 'true'
        }

        self.current_session.post(BOSCH_URLS['login'], json=payload)
        remember_cookie_token = self.current_session.cookies['REMEMBER']
        self.fetch_data(remember_cookie_token)

    def fetch_data(self, remember_cookie_token):
        headers = {
            'cookie': 'REMEMBER=' + remember_cookie_token,
            'referer': 'https://www.ebike-connect.com/dashboard',
            'user-agent': str(random.random())[3:],
            'authority': 'www.ebike-connect.com',
            'protect-from': 'CSRF'
        }

        self.data = self.current_session.get(BOSCH_URLS['statistics'], headers=headers).json()

    def get_statistics(self):
        print (self.data)

bosch_ebike = BoschEbike()
statistics = bosch_ebike.get_statistics()

print(statistics)

Example output

{
    "current_month": {
        "month": 7,
        "distance": 0,
        "average_speed": 0.0,
        "calories_burned": 0,
        "elevation_gain": 0
    },
    "last_month": {
        "month": 6,
        "distance": 0,
        "average_speed": 0.0,
        "calories_burned": 0,
        "elevation_gain": 0
    },
    "total_statistics": {
        "distance": 0,
        "elevation_gain": 0
    }
}