Skip to content

Instantly share code, notes, and snippets.

@ahaxu
Last active May 21, 2024 02:46
Show Gist options
  • Save ahaxu/50ffd9ceaa01bc6a560d7ffd23d3e949 to your computer and use it in GitHub Desktop.
Save ahaxu/50ffd9ceaa01bc6a560d7ffd23d3e949 to your computer and use it in GitHub Desktop.
htr-mua-he-ruc-ro-2024.py
def f(activities):
import math
from datetime import datetime
act_by_date = dict()
for act in activities:
if act.get('type') != "Run":
continue
km = round(act["distance"]/1000, 2)
if km < 2:
continue
gap_time_in_minutes = abs(
act.get('elapsed_time', 0) - act.get('moving_time', 0))/60
if gap_time_in_minutes > 30:
continue
dt_str = act.get('start_date_local') # 2021-02-12T20:40:00Z
dt_obj = datetime.strptime(dt_str, '%Y-%m-%dT%H:%M:%SZ')
# key for ac_by_date dict
dt_ymd = int("{}{}{}".format(dt_obj.year, dt_obj.month, dt_obj.day))
# custom for act before 6 7 8 of may
# avg pace of tracklog
if dt_obj.month == 5 and dt_obj.day in [6, 7, 8]:
avg_pace = round((1000/act.get('average_speed', 0))/60, 1)
if 3 <= avg_pace <= 15:
km_per_block = 1
block = km/km_per_block
point = km
act["block"] = block
act["km"] = km
act["point"] = point
print("debug km: {}, block: {}, point: {}".format(
km, block, point))
if dt_ymd in act_by_date:
if act_by_date.get(dt_ymd).get('km') < km:
act_by_date[dt_ymd] = act
else:
act_by_date[dt_ymd] = act
else:
print('sync acts for days not in days: 6 7 8 of May')
print('act id {} {} {}'.format(
act.get('id'), act.get('name'), dt_str))
if act.get('laps') is None or len(act.get("laps")) == 0:
print('no laps for this act {}'.format(act.get('id')))
continue
valid_pace_dict = dict()
for lap in act.get('laps'):
print("lap distance {}".format(round(lap.get("distance")/1000, 2)))
if round(lap.get("distance")/1000) == 1:
avg_lap_pace = round((1000/lap.get('average_speed', 0))/60, 2)
else:
avg_lap_pace = round((round(lap.get("distance")/1000, 1)/lap.get('average_speed', 0))/60, 5)
print("avg_lap_pace in minutes", avg_lap_pace)
# some watch not correct with lap distance, so we need or condition
if (3 <= avg_lap_pace <= 12) or (avg_lap_pace < 1 and round(lap.get("distance")/1000) == 0):
valid_pace_dict[lap.get('id')] = True
print('debug len valid_pace_dict: {} - act.get.laps.len {}'.format(
len(valid_pace_dict),
len(act.get('laps'))))
if len(valid_pace_dict) == len(act.get("laps")):
km_per_block = 1
block = km/km_per_block
point = km
act["block"] = block
act["km"] = km
act["point"] = point
print("debug km: {}, block: {}, point: {}".format(
km, block, point))
if dt_ymd in act_by_date:
if act_by_date.get(dt_ymd).get('km') < km:
act_by_date[dt_ymd] = act
else:
act_by_date[dt_ymd] = act
return list(act_by_date.values())
acts = f(activities)
@ahaxu
Copy link
Author

ahaxu commented May 20, 2024

rule with white_list activity

# 
# check_avg_pace_in_lap 
# need to be set in event
#
def f(activities):
    import math
    from datetime import datetime

    white_list_acts = [
        11448424729,
        11443873790,
        11443848827,
        11442008938,
    ]
    act_by_date = dict()
    for act in activities:
        if act.get('type') != "Run":
            continue

        km = round(act["distance"]/1000, 2)
        if km < 2:
            continue

        gap_time_in_minutes = abs(
                act.get('elapsed_time', 0) - act.get('moving_time', 0))/60
        if gap_time_in_minutes > 30 and (act.get('id') not in white_list_acts):
            print("act_id {}: over 30 mins".format(act.get('id')))
            continue

        dt_str = act.get('start_date_local')  # 2021-02-12T20:40:00Z
        dt_obj = datetime.strptime(dt_str, '%Y-%m-%dT%H:%M:%SZ')
        # key for ac_by_date dict
        dt_ymd = int("{}{}{}".format(dt_obj.year, dt_obj.month, dt_obj.day))

        # custom for act before 6 7 8 of may
        # avg pace of tracklog

        # temp no need to check pace per lap
        if True or (dt_obj.month == 5 and dt_obj.day in [6, 7, 8]):
            avg_pace = round((1000/act.get('average_speed', 0))/60, 1)
            if 3 <= avg_pace <= 15 or (act.get('id') in white_list_acts):
                km_per_block = 1
                block = km/km_per_block
                point = km

                act["block"] = block
                act["km"] = km
                act["point"] = point

                print("debug - act: {}, km: {}, block: {}, point: {}".format(
                    act.get('id'), km, block, point))

                if dt_ymd in act_by_date:
                    if act_by_date.get(dt_ymd).get('km') < km:
                        act_by_date[dt_ymd] = act
                else:
                    act_by_date[dt_ymd] = act

    return list(act_by_date.values())

acts = f(activities)

@ahaxu
Copy link
Author

ahaxu commented May 20, 2024

Newest version with pace per lap (1km) checking

def f(activities):
    import math
    from datetime import datetime

    white_list_acts = [
        11448424729,
        11443873790,
        11443848827,
        11442008938,
    ]

    act_by_date = dict()
    for act in activities:
        dt_str = act.get('start_date_local')  # 2021-02-12T20:40:00Z
        dt_obj = datetime.strptime(dt_str, '%Y-%m-%dT%H:%M:%SZ')
        # key for ac_by_date dict
        dt_ymd = int("{}{}{}".format(dt_obj.year, dt_obj.month, dt_obj.day))

        if act.get('id') in white_list_acts:
            act_by_date[dt_ymd] = act
            continue

        if act.get('type') != "Run":
            continue

        km = round(act["distance"] / 1000, 2)
        if km < 2:
            continue

        gap_time_in_minutes = abs(
                act.get('elapsed_time', 0) - act.get('moving_time', 0)) / 60
        if gap_time_in_minutes > 30:
            print("act_id {}: over 30 mins".format(act.get('id')))
            continue

        # custom for act before 6 7 8 of may
        # avg pace of tracklog
        if dt_obj.month == 5 and dt_obj.day in [6, 7, 8]:
            print('before updated new pace by km rule')
            avg_pace = round((1000 / act.get('average_speed', 0)) / 60, 1)
            if 3 <= avg_pace <= 15:
                km_per_block = 1
                block = km / km_per_block
                point = km

                act["block"] = block
                act["km"] = km
                act["point"] = point

                print("debug km: {}, block: {}, point: {}".format(
                    km, block, point))

                if dt_ymd in act_by_date:
                    if act_by_date.get(dt_ymd).get('km') < km:
                        act_by_date[dt_ymd] = act
                else:
                    act_by_date[dt_ymd] = act
        else:
            print('sync acts for days not in days: 6 7 8 of May')
            print('act id {} {} {}'.format(
                act.get('id'), act.get('name'), dt_str))

            if act.get('laps') is None or len(act.get("laps")) == 0:
                print('no laps for this act {}'.format(act.get('id')))
                continue

            valid_pace_dict = dict()
            for lap in act.get('laps'):
                print("lap distance {}".format(round(lap.get("distance")/1000, 2)))

                if round(lap.get("distance") / 1000) == 1:
                    avg_lap_pace = round((1000 / lap.get('average_speed', 0)) / 60, 2)
                else:
                    avg_lap_pace = round((round(lap.get("distance")/1000, 1)/lap.get('average_speed', 0))/60, 5)

                print("act_id {} - avg_lap_pace {} in minutes".format(act.get('id'), avg_lap_pace))

                # some watch not correct with lap distance, so we need or condition
                if (avg_lap_pace <= 12):
                    valid_pace_dict[lap.get('id')] = True

            print('debug act id {} - len valid_pace_dict: {} - act.get.laps.len {}'.format(
                    act.get('id'),
                    len(valid_pace_dict),
                    len(act.get('laps'))))

            if len(valid_pace_dict) == len(act.get("laps")):
                km_per_block = 1
                block = km / km_per_block
                point = km

                act["block"] = block
                act["km"] = km
                act["point"] = point

                print("debug km: {}, block: {}, point: {}".format(
                    km, block, point))

                if dt_ymd in act_by_date:
                    if act_by_date.get(dt_ymd).get('km') < km:
                        act_by_date[dt_ymd] = act
                else:
                    act_by_date[dt_ymd] = act
    return list(act_by_date.values())

acts = f(activities)


@ahaxu
Copy link
Author

ahaxu commented May 21, 2024

newest

def f(activities):
    import math
    from datetime import datetime

    white_list_acts = [
        11448424729,
        11443873790,
        11443848827,
        11442008938,
    ]

    act_by_date = dict()
    for act in activities:
        dt_str = act.get('start_date_local')  # 2021-02-12T20:40:00Z
        dt_obj = datetime.strptime(dt_str, '%Y-%m-%dT%H:%M:%SZ')
        # key for ac_by_date dict
        dt_ymd = int("{}{}{}".format(dt_obj.year, dt_obj.month, dt_obj.day))

        km = round(act["distance"] / 1000, 2)
        if km < 2:
            continue

        if act.get('id') in white_list_acts:
            km_per_block = 1
            block = km / km_per_block
            point = km

            act["block"] = block
            act["km"] = km
            act["point"] = point

            act_by_date[dt_ymd] = act
            print("whitelist act id {} - name {}".format(act.get('id'), act.get('name')))

            continue

        if act.get('type') != "Run":
            continue


        gap_time_in_minutes = abs(
                act.get('elapsed_time', 0) - act.get('moving_time', 0)) / 60
        if gap_time_in_minutes > 30:
            print("act_id {}: over 30 mins".format(act.get('id')))
            continue

        # custom for act before 6 7 8 of may
        # avg pace of tracklog
        if dt_obj.month == 5 and dt_obj.day in [6, 7, 8]:
            print('before updated new pace by km rule')
            avg_pace = round((1000 / act.get('average_speed', 0)) / 60, 1)
            if 3 <= avg_pace <= 15:
                km_per_block = 1
                block = km / km_per_block
                point = km

                act["block"] = block
                act["km"] = km
                act["point"] = point

                print("debug km: {}, block: {}, point: {}".format(
                    km, block, point))

                if dt_ymd in act_by_date:
                    if act_by_date.get(dt_ymd).get('km') < km:
                        act_by_date[dt_ymd] = act
                else:
                    act_by_date[dt_ymd] = act
        else:
            print('sync acts for days not in days: 6 7 8 of May')
            print('act id {} {} {}'.format(
                act.get('id'), act.get('name'), dt_str))

            if act.get('laps') is None or len(act.get("laps")) == 0:
                print('no laps for this act {}'.format(act.get('id')))
                continue

            valid_pace_dict = dict()
            for lap in act.get('laps'):
                print("lap distance {}".format(round(lap.get("distance")/1000, 2)))

                if round(lap.get("distance") / 1000) == 1:
                    avg_lap_pace = round((1000 / lap.get('average_speed', 0)) / 60, 2)
                else:
                    avg_lap_pace = round((round(lap.get("distance")/1000, 1)/lap.get('average_speed', 0))/60, 5)

                print("act_id {} - avg_lap_pace {} in minutes".format(act.get('id'), avg_lap_pace))

                # some watch not correct with lap distance, so we need or condition
                if (avg_lap_pace <= 12):
                    valid_pace_dict[lap.get('id')] = True

            print('debug act id {} - len valid_pace_dict: {} - act.get.laps.len {}'.format(
                    act.get('id'),
                    len(valid_pace_dict),
                    len(act.get('laps'))))

            if len(valid_pace_dict) == len(act.get("laps")):
                km_per_block = 1
                block = km / km_per_block
                point = km

                act["block"] = block
                act["km"] = km
                act["point"] = point

                print("debug km: {}, block: {}, point: {}".format(
                    km, block, point))

                if dt_ymd in act_by_date:
                    if act_by_date.get(dt_ymd).get('km') < km:
                        act_by_date[dt_ymd] = act
                else:
                    act_by_date[dt_ymd] = act
    return list(act_by_date.values())

acts = f(activities)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment