petergardfjall 2018-04-02 09:25:31 +02:00
parent 186fa4ac7b
commit 769d02b2a2

View File

@ -155,7 +155,8 @@ class GarminClient(object):
"""Return all activity ids stored by the logged in user, along """Return all activity ids stored by the logged in user, along
with their starting timestamps. with their starting timestamps.
:returns: The full list of activity identifiers. :returns: The full list of activity identifiers (along with their
starting timestamps).
:rtype: tuples of (int, datetime) :rtype: tuples of (int, datetime)
""" """
ids = [] ids = []
@ -171,8 +172,9 @@ class GarminClient(object):
@require_session @require_session
def _fetch_activity_ids_and_ts(self, start_index, max_limit=100): def _fetch_activity_ids_and_ts(self, start_index, max_limit=100):
"""Return a sequence of activity ids starting at a given index, """Return a sequence of activity ids (along with their starting
with index 0 being the user's most recently registered activity. timestamps) starting at a given index, with index 0 being the user's
most recently registered activity.
Should the index be out of bounds or the account empty, an empty Should the index be out of bounds or the account empty, an empty
list is returned. list is returned.
@ -182,26 +184,32 @@ class GarminClient(object):
:param max_limit: The (maximum) number of activities to retrieve. :param max_limit: The (maximum) number of activities to retrieve.
:type max_limit: int :type max_limit: int
:returns: A list of activity identifiers. :returns: A list of activity identifiers (along with their
:rtype: list of str starting timestamps).
:rtype: tuples of (int, datetime)
""" """
log.debug("fetching activities {} through {} ...".format( log.debug("fetching activities {} through {} ...".format(
start_index, start_index+max_limit-1)) start_index, start_index+max_limit-1))
response = self.session.get( response = self.session.get(
"https://connect.garmin.com/proxy/activity-search-service-1.2/json/activities", params={"start": start_index, "limit": max_limit}) "https://connect.garmin.com/modern/proxy/activitylist-service/activities/search/activities",
params={"start": start_index, "limit": max_limit})
if response.status_code != 200: if response.status_code != 200:
raise Exception( raise Exception(
u"failed to fetch activities {} to {} types: {}\n{}".format( u"failed to fetch activities {} to {} types: {}\n{}".format(
start_index, (start_index+max_limit-1), start_index, (start_index+max_limit-1),
response.status_code, response.text)) response.status_code, response.text))
results = json.loads(response.text)["results"] activities = json.loads(response.text)
if not "activities" in results: if not activities:
# index out of bounds or empty account # index out of bounds or empty account
return [] return []
entries = [ (int(entry["activity"]["activityId"]), entries = []
dateutil.parser.parse(entry["activity"]["activitySummary"]["BeginTimestamp"]["value"])) for activity in activities:
for entry in results["activities"] ] id = int(activity["activityId"])
timestamp_utc = dateutil.parser.parse(activity["startTimeGMT"])
# make sure UTC timezone gets set
timestamp_utc = timestamp_utc.replace(tzinfo=dateutil.tz.tzutc())
entries.append( (id, timestamp_utc) )
log.debug("got {} activities.".format(len(entries))) log.debug("got {} activities.".format(len(entries)))
return entries return entries
@ -217,8 +225,10 @@ class GarminClient(object):
:returns: The activity summary as a JSON dict. :returns: The activity summary as a JSON dict.
:rtype: dict :rtype: dict
""" """
response = self.session.get("https://connect.garmin.com/modern/proxy/activity-service-1.3/json/activity_embed/{}".format(activity_id)) response = self.session.get("https://connect.garmin.com/modern/proxy/activity-service/activity/{}".format(activity_id))
if response.status_code != 200: if response.status_code != 200:
log.error(u"failed to fetch json summary for activity {}: {}\n{}".format(
activity_id, response.status_code, response.text))
raise Exception(u"failed to fetch json summary for activity {}: {}\n{}".format( raise Exception(u"failed to fetch json summary for activity {}: {}\n{}".format(
activity_id, response.status_code, response.text)) activity_id, response.status_code, response.text))
return json.loads(response.text) return json.loads(response.text)