Last active
November 21, 2024 14:47
-
-
Save ahaxu/a34a9e5a1dad79871623e98c8bfc1b59 to your computer and use it in GitHub Desktop.
goya_rong_da_nang_2024.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def f(activities): | |
| import math | |
| import requests | |
| from datetime import datetime | |
| 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 = math.floor(act["distance"]/1000) | |
| point = 0 | |
| block = 0 | |
| avg_pace = round((1000/act.get('average_speed', 0))/60, 1) | |
| gap_time_in_minutes = abs( | |
| act.get('elapsed_time', 0) - act.get('moving_time', 0))/60 | |
| print('avg_pace', avg_pace) | |
| print('type', act.get('type')) | |
| print("start_latlng", act.get("start_latlng")) | |
| print("end_latlng", act.get("end_latlng")) | |
| print("gap_time_in_minutes", gap_time_in_minutes) | |
| # check if latlng is in Da nang | |
| reverse_geo_url = 'https://api.mapbox.com/search/geocode/v6/reverse?latitude={}&longitude={}&access_token={}' | |
| mb_key = 'pk.eyJ1IjoibG9uZ2thIiwiYSI6ImNsemh4a3RiNTA5ZHEyaXM0bTBuangzbHkifQ.Pfj0tBVDgvoDM-VNHFPNWg' | |
| # check start latlng | |
| start_latlng = act.get('start_latlng') | |
| start_loc = requests.get(reverse_geo_url.format( | |
| start_latlng[0], start_latlng[1], mb_key)).json() | |
| is_valid_start_loc = False | |
| if start_loc.get('features')[0].get('properties').get('context').get('region').get('region_code_full') == "VN-DN" \ | |
| or start_loc.get('features')[0].get('properties').get('context').get('region').get('name') == "Da Nang": | |
| is_valid_start_loc = True | |
| if is_valid_start_loc is False: | |
| print("not valid start loc") | |
| print(start_loc.get('features')[0].get( | |
| 'properties').get('context').get('region')) | |
| continue | |
| # check end latlng | |
| end_latlng = act.get('end_latlng') | |
| end_loc = requests.get(reverse_geo_url.format( | |
| end_latlng[0], end_latlng[1], mb_key)).json() | |
| is_valid_end_loc = False | |
| if end_loc.get('features')[0].get('properties').get('context').get('region').get('region_code_full') == "VN-DN" \ | |
| or end_loc.get('features')[0].get('properties').get('context').get('region').get('name') == "Da Nang": | |
| is_valid_end_loc = True | |
| if is_valid_end_loc is False: | |
| print("not valid end loc") | |
| print(end_loc.get('features')[0].get( | |
| 'properties').get('context').get('region')) | |
| continue | |
| if act.get('type') == "Run" \ | |
| and is_valid_start_loc and is_valid_end_loc \ | |
| and (4 <= avg_pace <= 9) \ | |
| and len(act.get("start_latlng")) > 0 \ | |
| and gap_time_in_minutes <= 30: | |
| km_per_block = 1 | |
| point_per_block = 1 | |
| block = math.floor(km/km_per_block) | |
| act["block"] = block | |
| act["km"] = km | |
| act["point"] = km | |
| # x2 | |
| if dt_ymd in ["20241124", "20241127"]: | |
| act["km"] = km * 2 | |
| act["point"] = point * 2 | |
| print("debug act by date x2 {}: {} {}".format( | |
| dt_ymd, act.get('km'), act.get('id'))) | |
| if dt_ymd in act_by_date: | |
| # if exists, append act to list | |
| act_by_date[dt_ymd].append(act) | |
| else: | |
| # if no act in dict yet | |
| act_by_date[dt_ymd] = [act] | |
| valid_acts = [] | |
| for d in sorted(act_by_date): | |
| print("debug act by date d: {}".format(d)) | |
| print("debug act by date len: {}".format(len(act_by_date.get(d)))) | |
| if len(act_by_date.get(d)) > 2: | |
| acts = sorted(act_by_date.get(d), key=lambda x: x.get('km'), reverse=True)[:2] | |
| else: | |
| acts = act_by_date.get(d) | |
| for a in acts: | |
| valid_acts.append(a) | |
| return valid_acts | |
| acts = f(activities) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment