Skip to content

Commit 7b5231f

Browse files
committed
Added code
1 parent bff368b commit 7b5231f

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

garmin-connect/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Initialize the garmin-connect package."""

garmin-connect/garmin.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import logging
2+
import random
3+
import json
4+
import re
5+
import requests
6+
from datetime import date
7+
from datetime import timedelta
8+
9+
BASE_URL = 'https://connect.garmin.com'
10+
SSO_URL = 'https://sso.garmin.com/sso'
11+
MODERN_URL = 'https://connect.garmin.com/modern'
12+
SIGNIN_URL = 'https://sso.garmin.com/sso/signin'
13+
14+
class Garmin(object):
15+
"""
16+
Object using Garmin Connect 's API-method.
17+
See https://connect.garmin.com/
18+
"""
19+
20+
url = MODERN_URL + '/proxy/usersummary-service/usersummary/daily/'
21+
headers = {
22+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
23+
'origin': 'https://sso.garmin.com'
24+
}
25+
26+
def __init__(self, username, password):
27+
"""
28+
Init module
29+
"""
30+
self.username = username
31+
self.password = password
32+
self.req = requests.session()
33+
34+
self.login(self.username, self.password)
35+
36+
def login(self, username, password):
37+
"""
38+
Login to portal
39+
"""
40+
params = {
41+
'webhost': BASE_URL,
42+
'service': MODERN_URL,
43+
'source': SIGNIN_URL,
44+
'redirectAfterAccountLoginUrl': MODERN_URL,
45+
'redirectAfterAccountCreationUrl': MODERN_URL,
46+
'gauthHost': SSO_URL,
47+
'locale': 'en_US',
48+
'id': 'gauth-widget',
49+
'cssUrl': 'https://static.garmincdn.com/com.garmin.connect/ui/css/gauth-custom-v1.2-min.css',
50+
'clientId': 'GarminConnect',
51+
'rememberMeShown': 'true',
52+
'rememberMeChecked': 'false',
53+
'createAccountShown': 'true',
54+
'openCreateAccount': 'false',
55+
'usernameShown': 'false',
56+
'displayNameShown': 'false',
57+
'consumeServiceTicket': 'false',
58+
'initialFocus': 'true',
59+
'embedWidget': 'false',
60+
'generateExtraServiceTicket': 'false'
61+
}
62+
63+
response = self.req.get(SIGNIN_URL, headers=self.headers, params=params)
64+
response.raise_for_status()
65+
66+
data = {
67+
'username': username,
68+
'password': password,
69+
'embed': 'true',
70+
'lt': 'e1s1',
71+
'_eventId': 'submit',
72+
'displayNameRequired': 'false'
73+
}
74+
75+
response = self.req.post(SIGNIN_URL, headers=self.headers, params=params, data=data)
76+
response.raise_for_status()
77+
78+
response_url = re.search(r'"(https:[^"]+?ticket=[^"]+)"', response.text)
79+
if not response_url:
80+
raise Exception('Could not find response URL')
81+
response_url = re.sub(r'\\', '', response_url.group(1))
82+
response = self.req.get(response_url)
83+
84+
self.user_prefs = self.parse_json(response.text, 'VIEWER_USERPREFERENCES')
85+
self.display_name = self.user_prefs['displayName']
86+
response.raise_for_status()
87+
88+
def parse_json(self, html, key):
89+
"""
90+
Find and return json data
91+
"""
92+
found = re.search(key + r" = JSON.parse\(\"(.*)\"\);", html, re.M)
93+
if found:
94+
text = found.group(1).replace('\\"', '"')
95+
return json.loads(text)
96+
97+
def fetch_stats(self):
98+
"""
99+
Fetch all available data
100+
"""
101+
today = str(date.today())
102+
getURL = self.url + self.display_name + '?' + 'calendarDate=' + today
103+
response = self.req.get(getURL, headers=self.headers)
104+
response.raise_for_status()
105+
return(response.json())
106+

setup.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import setuptools
2+
3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="garmin-connect",
8+
version="0.1.0",
9+
author="Ron Klinkien",
10+
author_email="ron@cyberjunky.nl",
11+
description="Python API wrapper for Garmin Connect",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/cyberjunky/garmin-connect",
15+
packages=setuptools.find_packages(),
16+
classifiers=[
17+
"Programming Language :: Python :: 3",
18+
"License :: OSI Approved :: MIT License",
19+
"Operating System :: OS Independent",
20+
],
21+
)

0 commit comments

Comments
 (0)