Skip to content

Commit 0685700

Browse files
authored
Merge pull request #266 from dpfrakes/master
Available and In-progress badges
2 parents edcd427 + f720de3 commit 0685700

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"x": f"Get Heart Rate Variability data (HRV) for '{today.isoformat()}'",
120120
"z": f"Get progress summary from '{startdate.isoformat()}' to '{today.isoformat()}' for all metrics",
121121
"A": "Get gear, the defaults, activity types and statistics",
122-
"B": f"Get weight-ins from '{startdate.isoformat()}' to '{today.isoformat()}'",
122+
"B": f"Get weigh-ins from '{startdate.isoformat()}' to '{today.isoformat()}'",
123123
"C": f"Get daily weigh-ins for '{today.isoformat()}'",
124124
"D": f"Delete all weigh-ins for '{today.isoformat()}'",
125125
"E": f"Add a weigh-in of {weight}{weightunit} on '{today.isoformat()}'",
@@ -708,7 +708,7 @@ def switch(api, i):
708708
f"api.get_gear_stats({uuid}) / {name}", api.get_gear_stats(uuid)
709709
)
710710

711-
# WEIGHT-INS
711+
# WEIGH-INS
712712
elif i == "B":
713713
# Get weigh-ins data
714714
display_json(

garminconnect/__init__.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def __init__(
6767
"/personalrecord-service/personalrecord/prs"
6868
)
6969
self.garmin_connect_earned_badges_url = "/badge-service/badge/earned"
70-
self.garmin_connect_adhoc_challenges_url = (
71-
"/adhocchallenge-service/adHocChallenge/historical"
70+
self.garmin_connect_available_badges_url = (
71+
"/badge-service/badge/available"
7272
)
7373
self.garmin_connect_adhoc_challenges_url = (
7474
"/adhocchallenge-service/adHocChallenge/historical"
@@ -716,14 +716,61 @@ def get_personal_record(self) -> Dict[str, Any]:
716716

717717
return self.connectapi(url)
718718

719-
def get_earned_badges(self) -> Dict[str, Any]:
719+
def get_earned_badges(self) -> List[Dict[str, Any]]:
720720
"""Return earned badges for current user."""
721721

722722
url = self.garmin_connect_earned_badges_url
723723
logger.debug("Requesting earned badges for user")
724724

725725
return self.connectapi(url)
726726

727+
def get_available_badges(self) -> list[dict]:
728+
"""Return available badges for current user."""
729+
730+
url = self.garmin_connect_available_badges_url
731+
logger.debug("Requesting available badges for user")
732+
733+
return self.connectapi(url, params={"showExclusiveBadge": "true"})
734+
735+
def get_in_progress_badges(self) -> list[dict]:
736+
"""Return in progress badges for current user."""
737+
738+
logger.debug("Requesting in progress badges for user")
739+
740+
earned_badges = self.get_earned_badges()
741+
available_badges = self.get_available_badges()
742+
743+
# Filter out badges that are not in progress
744+
def is_badge_in_progress(badge: dict) -> bool:
745+
"""Return True if the badge is in progress."""
746+
progress = badge.get("badgeProgressValue")
747+
if not progress:
748+
return False
749+
if progress == 0:
750+
return False
751+
target = badge.get("badgeTargetValue")
752+
if progress == target:
753+
if badge.get("badgeLimitCount") is None:
754+
return False
755+
return (
756+
badge.get("badgeEarnedNumber", 0)
757+
< badge["badgeLimitCount"]
758+
)
759+
return True
760+
761+
earned_in_progress_badges = list(
762+
filter(is_badge_in_progress, earned_badges)
763+
)
764+
available_in_progress_badges = list(
765+
filter(is_badge_in_progress, available_badges)
766+
)
767+
768+
combined = {b["badgeId"]: b for b in earned_in_progress_badges}
769+
combined.update(
770+
{b["badgeId"]: b for b in available_in_progress_badges}
771+
)
772+
return list(combined.values())
773+
727774
def get_adhoc_challenges(self, start, limit) -> Dict[str, Any]:
728775
"""Return adhoc challenges for current user."""
729776

0 commit comments

Comments
 (0)