Skip to content

Instantly share code, notes, and snippets.

@jangaraj
Last active November 10, 2020 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jangaraj/d5dffbabe692aeb6c5bbc09eb9ee538a to your computer and use it in GitHub Desktop.
Save jangaraj/d5dffbabe692aeb6c5bbc09eb9ee538a to your computer and use it in GitHub Desktop.
Connect to Endomondo API with local credentials
import requests
import uuid, socket
class Endomondo:
# user your own non OIDC credentials here
email = "<EMAIL>"
password = "<PASSWORD>"
country = 'GB'
device_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname()))
os = "Android"
app_version="7.1"
app_variant="M-Pro"
os_version="2.3.7"
model="HTC Vision"
auth_token = False
Requests = requests.session()
URL_AUTH = 'https://api.mobile.endomondo.com/mobile/auth?v=2.4&action=PAIR'
URL_WORKOUTS = 'https://api.mobile.endomondo.com/mobile/api/workouts'
def __init__(self):
self.Requests.headers['User-Agent'] = "Dalvik/1.4.0 (Linux; U; %s %s; %s Build/GRI40)" % (self.os, self.os_version, self.model)
if self.email and self.password:
self.auth_token = self.request_auth_token(self.email, self.password)
def get_auth_token(self):
if self.auth_token:
return self.auth_token
self.auth_token = self.request_auth_token()
config.set('endomondo', 'auth_token', self.auth_token)
return self.auth_token
def request_auth_token(self, email, password):
params = {
'email': email,
'password': password,
'country': self.country,
'deviceId': self.device_id,
'os' : self.os,
'appVersion': self.app_version,
'appVariant': self.app_variant,
'osVersion': self.os_version,
'model': self.model
}
r = self.Requests.get(self.URL_AUTH, params=params)
lines = r.text.split("\n")
if lines[0] != "OK":
raise ValueError("Could not authenticate with Endomondo, Expected 'OK', got '%s'" % lines[0])
lines.pop(0)
for line in lines:
key, value = line.split("=")
if key == "authToken":
return value
return False
def make_request(self, url, params={}):
params.update({
'authToken': self.get_auth_token(),
'language': 'EN',
'fields': 'simple',
'maxResults': '10000',
'after': '1985-01-01 00:00:00 UTC',
'before': '2022-01-01 00:00:00 UTC'
})
r = self.Requests.get(url, params=params)
if (r.status_code != requests.codes.ok and r.status_code != 201):
print("Could not retrieve URL %s (Status code %s)" % (r.url, r.status_code))
r.raise_for_status()
return r
def print_workouts(self):
r = self.make_request(self.URL_WORKOUTS)
print(r.json())
print(len(r.json()['data']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment