Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for connect.garmin.cn #43

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ except Exception: # pylint: disable=broad-except
print("Unknown error occurred during Garmin Connect Client init")
quit()

"""
If you are in mainland China
Initialize Garmin client with credentials and add `is_cn=True`
Only needed when your program is initialized
"""
print("Garmin(email, password, is_cn=True)")
print("----------------------------------------------------------------------------------------")
try:
client = Garmin(YOUR_EMAIL, YOUR_PASSWORD, is_cn=True)
except (
GarminConnectConnectionError,
GarminConnectAuthenticationError,
GarminConnectTooManyRequestsError,
) as err:
print("Error occurred during Garmin Connect Client init: %s" % err)
quit()
except Exception: # pylint: disable=broad-except
print("Unknown error occurred during Garmin Connect Client init")
quit()

"""
Login to Garmin Connect portal
Expand Down
67 changes: 40 additions & 27 deletions garminconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,55 @@ class Garmin(object):
"""
Object using Garmin Connect 's API-method.
See https://connect.garmin.com/
or you are in mainland China
See https://connect.garmin.cn/
"""
url_user_summary = BASE_URL + '/proxy/usersummary-service/usersummary/daily/'
url_user_summary_chart = BASE_URL + \
'/proxy/wellness-service/wellness/dailySummaryChart/'
url_heartrates = BASE_URL + '/proxy/wellness-service/wellness/dailyHeartRate/'
url_sleepdata = BASE_URL + '/proxy/wellness-service/wellness/dailySleepData/'
url_body_composition = BASE_URL + \
'/proxy/weight-service/weight/daterangesnapshot'
url_activities = BASE_URL + \
'/proxy/activitylist-service/activities/search/activities'
url_hydrationdata = BASE_URL + '/proxy/usersummary-service/usersummary/hydration/daily/'
url_activity = BASE_URL + '/proxy/activity-service/activity/'
url_personal_record = BASE_URL + '/proxy/personalrecord-service/personalrecord/'
url_tcx_download = BASE_URL + "/proxy/download-service/export/tcx/activity/"
url_gpx_download = BASE_URL + "/proxy/download-service/export/gpx/activity/"
url_kml_download = BASE_URL + "/proxy/download-service/export/kml/activity/"
url_fit_download = BASE_URL + "/proxy/download-service/files/activity/"
url_csv_download = BASE_URL + "/proxy/download-service/export/csv/activity/"
url_device_list = BASE_URL + '/proxy/device-service/deviceregistration/devices'
url_device_service = BASE_URL + \
'/proxy/device-service/deviceservice/'

headers = {
'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',
'origin': 'https://sso.garmin.com'
}

def __init__(self, email, password):

def __init__(self, email, password, is_cn=False):
"""
Init module
"""
global BASE_URL
global SSO_URL
global SIGNIN_URL
self.email = email
self.password = password
self.req = requests.session()
self.logger = logging.getLogger(__name__)
self.display_name = ""
self.full_name = ""
self.unit_system = ""
self.is_cn = is_cn
if is_cn:
BASE_URL = BASE_URL.replace(".com", ".cn")
SSO_URL = SSO_URL.replace(".com", ".cn")
SIGNIN_URL = SIGNIN_URL.replace(".com", ".cn")

self.url_user_summary = BASE_URL + '/proxy/usersummary-service/usersummary/daily/'
self.url_user_summary_chart = BASE_URL + \
'/proxy/wellness-service/wellness/dailySummaryChart/'
self.url_heartrates = BASE_URL + '/proxy/wellness-service/wellness/dailyHeartRate/'
self.url_sleepdata = BASE_URL + '/proxy/wellness-service/wellness/dailySleepData/'
self.url_body_composition = BASE_URL + \
'/proxy/weight-service/weight/daterangesnapshot'
self.url_activities = BASE_URL + \
'/proxy/activitylist-service/activities/search/activities'
self.url_hydrationdata = BASE_URL + '/proxy/usersummary-service/usersummary/hydration/daily/'
self.url_activity = BASE_URL + '/proxy/activity-service/activity/'
self.url_personal_record = BASE_URL + '/proxy/personalrecord-service/personalrecord/'
self.url_tcx_download = BASE_URL + "/proxy/download-service/export/tcx/activity/"
self.url_gpx_download = BASE_URL + "/proxy/download-service/export/gpx/activity/"
self.url_kml_download = BASE_URL + "/proxy/download-service/export/kml/activity/"
self.url_fit_download = BASE_URL + "/proxy/download-service/files/activity/"
self.url_csv_download = BASE_URL + "/proxy/download-service/export/csv/activity/"
self.url_device_list = BASE_URL + '/proxy/device-service/deviceregistration/devices'
self.url_device_service = BASE_URL + \
'/proxy/device-service/deviceservice/'

self.headers = {
'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',
'origin': 'https://sso.garmin.com' if not self.is_cn else "https://sso.garmin.cn"
}

def login(self):
"""
Expand Down Expand Up @@ -81,6 +92,8 @@ def login(self):
'embedWidget': 'false',
'generateExtraServiceTicket': 'false'
}
if self.is_cn:
params['cssUrl'] = 'https://static.garmincdn.cn/cn.garmin.connect/ui/css/gauth-custom-v1.2-min.css'

data = {
'username': self.email,
Expand Down