From c01ef35ffb8f50e3b4f27a3d6492fcd54071efe8 Mon Sep 17 00:00:00 2001 From: gardaholm Date: Thu, 22 Aug 2024 13:42:12 +0200 Subject: [PATCH 1/6] updated logbook functions updated logbook_entries function to fetch climb-uuid, updated the csv_output to clean the climb_uuid before export. changed auth type to allow username/password or user_id/token --- src/boardlib/__main__.py | 6 ++++-- src/boardlib/api/aurora.py | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/boardlib/__main__.py b/src/boardlib/__main__.py index 72820b4..0bff4e4 100644 --- a/src/boardlib/__main__.py +++ b/src/boardlib/__main__.py @@ -30,10 +30,12 @@ def logbook_entries(board, username, password, grade_type="font", database=None) def write_entries(output_file, entries, no_headers=False, fields=LOGBOOK_FIELDS): + cleaned_entries = [{k: v for k, v in entry.items() if k in fields} for entry in entries] writer = csv.DictWriter(output_file, fieldnames=fields) if not no_headers: writer.writeheader() - writer.writerows(entries) + writer.writerows(cleaned_entries) + def handle_database_command(args): @@ -53,7 +55,7 @@ def handle_logbook_command(args): password = os.environ.get(env_var) if not password: password = getpass.getpass("Password: ") - entries = boardlib.api.aurora.logbook_entries(args.board, args.username, password, args.grade_type, args.database) + entries = boardlib.api.aurora.logbook_entries(args.board, args.username, password, args.grade_type, db_path=args.database) if args.output: with open(args.output, "w", encoding="utf-8") as output_file: diff --git a/src/boardlib/api/aurora.py b/src/boardlib/api/aurora.py index c001ab5..14466c8 100644 --- a/src/boardlib/api/aurora.py +++ b/src/boardlib/api/aurora.py @@ -368,9 +368,9 @@ def get_bids_logbook(board, token, user_id): return sync_results["PUT"].get("bids", []) -def bids_logbook_entries(board, username, password, db_path=None): - login_info = login(board, username, password) - raw_entries = get_bids_logbook(board, login_info["token"], login_info["user_id"]) + +def bids_logbook_entries(board, token, user_id, db_path=None): + raw_entries = get_bids_logbook(board, token, user_id) for raw_entry in raw_entries: if db_path: @@ -379,9 +379,8 @@ def bids_logbook_entries(board, username, password, db_path=None): climb_name = get_climb_name(board, raw_entry["climb_uuid"]) yield { - "uuid": raw_entry["uuid"], + "climb_uuid": raw_entry["climb_uuid"], "user_id": raw_entry["user_id"], - "climb_uuid": raw_entry["climb_uuid"], "climb_name": climb_name, "angle": raw_entry["angle"], "is_mirror": raw_entry["is_mirror"], @@ -470,10 +469,13 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra (bids_summary['angle'] == ascent_angle) ] + climb_uuid = f"{ascent_climb_uuid}-{ascent_angle}" + if not bid_match.empty: bid_row = bid_match.iloc[0] total_tries = ascent_row['tries'] + bid_row['tries'] final_logbook.append({ + 'climb_uuid': climb_uuid, 'board': ascent_row['board'], 'angle': ascent_row['angle'], 'climb_name': ascent_row['name'], @@ -489,6 +491,7 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra bids_summary = bids_summary.drop(bid_match.index) else: final_logbook.append({ + 'climb_uuid': climb_uuid, 'board': ascent_row['board'], 'angle': ascent_row['angle'], 'climb_name': ascent_row['name'], @@ -503,6 +506,8 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra }) for _, bid_row in bids_summary.iterrows(): + climb_uuid = f"{bid_row['climb_uuid']}-{bid_row['angle']}" + if db_path: difficulty = get_difficulty_from_db(db_path, bid_row["climb_uuid"], bid_row["angle"]) displayed_grade = convert_difficulty_to_grade(difficulty, grades_dict, grade_type) @@ -511,6 +516,7 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra difficulty = None final_logbook.append({ + 'climb_uuid': climb_uuid, 'board': bid_row['board'], 'angle': bid_row['angle'], 'climb_name': bid_row['climb_name'], @@ -541,16 +547,17 @@ def calculate_tries_total(group): return group -def logbook_entries(board, username, password, grade_type="font", db_path=None): - login_info = login(board, username, password) - token = login_info["token"] - user_id = login_info["user_id"] +def logbook_entries(board, username=None, password=None, token=None, user_id=None, grade_type="font", db_path=None): + if not (user_id and token): + login_info = login(board, username, password) + token = login_info["token"] + user_id = login_info["user_id"] - bids_entries = list(bids_logbook_entries(board, username, password, db_path)) + bids_entries = list(bids_logbook_entries(board, token, user_id, db_path)) raw_ascents_entries = get_logbook(board, token, user_id) if not bids_entries and not raw_ascents_entries: - return pd.DataFrame(columns=['board', 'angle', 'climb_name', 'date', 'logged_grade', 'displayed_grade', 'difficulty', 'tries', 'is_mirror', 'is_ascent', 'comment']) + return pd.DataFrame(columns=['climb_uuid', 'board', 'angle', 'climb_name', 'date', 'logged_grade', 'displayed_grade', 'difficulty', 'tries', 'is_mirror', 'is_ascent', 'comment']) if bids_entries: bids_df = pd.DataFrame(bids_entries) @@ -569,7 +576,7 @@ def logbook_entries(board, username, password, grade_type="font", db_path=None): final_logbook = combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, grade_type) - full_logbook_df = pd.DataFrame(final_logbook, columns=['board', 'angle', 'climb_name', 'date', 'logged_grade', 'displayed_grade', 'difficulty', 'tries', 'is_mirror', 'is_ascent', 'comment']) + full_logbook_df = pd.DataFrame(final_logbook, columns=['climb_uuid', 'board', 'angle', 'climb_name', 'date', 'logged_grade', 'displayed_grade', 'difficulty', 'tries', 'is_mirror', 'is_ascent', 'comment']) full_logbook_df['date'] = pd.to_datetime(full_logbook_df['date']) full_logbook_df = full_logbook_df.groupby(['climb_name', 'is_mirror', 'angle']).apply(calculate_sessions_count).reset_index(drop=True) From 87cb2ab9c4873c75b5990820defe15b103a039f4 Mon Sep 17 00:00:00 2001 From: gardaholm Date: Thu, 22 Aug 2024 14:03:29 +0200 Subject: [PATCH 2/6] Renamed climb_uuid properly named climb_uuid to climb_angle_uuid to be precise, because uuid + angle are already merged --- src/boardlib/api/aurora.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/boardlib/api/aurora.py b/src/boardlib/api/aurora.py index 14466c8..077c1cf 100644 --- a/src/boardlib/api/aurora.py +++ b/src/boardlib/api/aurora.py @@ -469,13 +469,13 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra (bids_summary['angle'] == ascent_angle) ] - climb_uuid = f"{ascent_climb_uuid}-{ascent_angle}" + climb_angle_uuid = f"{ascent_climb_uuid}-{ascent_angle}" if not bid_match.empty: bid_row = bid_match.iloc[0] total_tries = ascent_row['tries'] + bid_row['tries'] final_logbook.append({ - 'climb_uuid': climb_uuid, + 'climb_angle_uuid': climb_angle_uuid, 'board': ascent_row['board'], 'angle': ascent_row['angle'], 'climb_name': ascent_row['name'], @@ -491,7 +491,7 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra bids_summary = bids_summary.drop(bid_match.index) else: final_logbook.append({ - 'climb_uuid': climb_uuid, + 'climb_angle_uuid': climb_angle_uuid, 'board': ascent_row['board'], 'angle': ascent_row['angle'], 'climb_name': ascent_row['name'], @@ -576,7 +576,7 @@ def logbook_entries(board, username=None, password=None, token=None, user_id=Non final_logbook = combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, grade_type) - full_logbook_df = pd.DataFrame(final_logbook, columns=['climb_uuid', 'board', 'angle', 'climb_name', 'date', 'logged_grade', 'displayed_grade', 'difficulty', 'tries', 'is_mirror', 'is_ascent', 'comment']) + full_logbook_df = pd.DataFrame(final_logbook, columns=['climb_angle_uuid', 'board', 'angle', 'climb_name', 'date', 'logged_grade', 'displayed_grade', 'difficulty', 'tries', 'is_mirror', 'is_ascent', 'comment']) full_logbook_df['date'] = pd.to_datetime(full_logbook_df['date']) full_logbook_df = full_logbook_df.groupby(['climb_name', 'is_mirror', 'angle']).apply(calculate_sessions_count).reset_index(drop=True) From fc24ed771b35272bbf5444864140ea0db97b8c54 Mon Sep 17 00:00:00 2001 From: gardaholm Date: Thu, 22 Aug 2024 14:31:36 +0200 Subject: [PATCH 3/6] added climb_uuid --- src/boardlib/api/aurora.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/boardlib/api/aurora.py b/src/boardlib/api/aurora.py index 077c1cf..b355fc0 100644 --- a/src/boardlib/api/aurora.py +++ b/src/boardlib/api/aurora.py @@ -476,6 +476,7 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra total_tries = ascent_row['tries'] + bid_row['tries'] final_logbook.append({ 'climb_angle_uuid': climb_angle_uuid, + 'climb_uuid': ascent_climb_uuid, 'board': ascent_row['board'], 'angle': ascent_row['angle'], 'climb_name': ascent_row['name'], @@ -492,6 +493,7 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra else: final_logbook.append({ 'climb_angle_uuid': climb_angle_uuid, + 'climb_uuid': ascent_climb_uuid, 'board': ascent_row['board'], 'angle': ascent_row['angle'], 'climb_name': ascent_row['name'], @@ -506,7 +508,7 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra }) for _, bid_row in bids_summary.iterrows(): - climb_uuid = f"{bid_row['climb_uuid']}-{bid_row['angle']}" + climb_angle_uuid = f"{bid_row['climb_uuid']}-{bid_row['angle']}" if db_path: difficulty = get_difficulty_from_db(db_path, bid_row["climb_uuid"], bid_row["angle"]) @@ -516,7 +518,8 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra difficulty = None final_logbook.append({ - 'climb_uuid': climb_uuid, + 'climb_angle_uuid': climb_angle_uuid, + 'climb_uuid': bid_row['climb_uuid'], 'board': bid_row['board'], 'angle': bid_row['angle'], 'climb_name': bid_row['climb_name'], From b1e2537c605ede4255b6e35ec12e517f258556ac Mon Sep 17 00:00:00 2001 From: gardaholm Date: Thu, 22 Aug 2024 19:32:31 +0200 Subject: [PATCH 4/6] Update src/boardlib/api/aurora.py adding comment, as suggested Co-authored-by: Luke Emery-Fertitta <6968022+lemeryfertitta@users.noreply.github.com> --- src/boardlib/api/aurora.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/boardlib/api/aurora.py b/src/boardlib/api/aurora.py index b355fc0..b339896 100644 --- a/src/boardlib/api/aurora.py +++ b/src/boardlib/api/aurora.py @@ -469,6 +469,7 @@ def combine_ascents_and_bids(ascents_df, bids_summary, db_path, grades_dict, gra (bids_summary['angle'] == ascent_angle) ] + # Used for Climbdex to uniquely identify climbs at a particular angle climb_angle_uuid = f"{ascent_climb_uuid}-{ascent_angle}" if not bid_match.empty: From 5463734da5dd2eb08d9391bac44a887fb671d381 Mon Sep 17 00:00:00 2001 From: gardaholm Date: Thu, 22 Aug 2024 19:33:08 +0200 Subject: [PATCH 5/6] Update src/boardlib/__main__.py as suggested Co-authored-by: Luke Emery-Fertitta <6968022+lemeryfertitta@users.noreply.github.com> --- src/boardlib/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/boardlib/__main__.py b/src/boardlib/__main__.py index 0bff4e4..c60c67b 100644 --- a/src/boardlib/__main__.py +++ b/src/boardlib/__main__.py @@ -30,7 +30,7 @@ def logbook_entries(board, username, password, grade_type="font", database=None) def write_entries(output_file, entries, no_headers=False, fields=LOGBOOK_FIELDS): - cleaned_entries = [{k: v for k, v in entry.items() if k in fields} for entry in entries] + cleaned_entries = ({k: v for k, v in entry.items() if k in fields} for entry in entries) writer = csv.DictWriter(output_file, fieldnames=fields) if not no_headers: writer.writeheader() From 1a218b6d67ff30b2f99563d642e78dbe2ff4a5b3 Mon Sep 17 00:00:00 2001 From: gardaholm Date: Mon, 26 Aug 2024 13:35:56 +0200 Subject: [PATCH 6/6] Moved login information Moved the fallback login information for username/passwored from logbook_entries() to handle_logbook_command() --- src/boardlib/__main__.py | 7 ++++++- src/boardlib/api/aurora.py | 7 +------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/boardlib/__main__.py b/src/boardlib/__main__.py index c60c67b..24ccf86 100644 --- a/src/boardlib/__main__.py +++ b/src/boardlib/__main__.py @@ -55,7 +55,12 @@ def handle_logbook_command(args): password = os.environ.get(env_var) if not password: password = getpass.getpass("Password: ") - entries = boardlib.api.aurora.logbook_entries(args.board, args.username, password, args.grade_type, db_path=args.database) + + login_info = boardlib.api.aurora.login(args.board, args.username, password) + token = login_info["token"] + user_id = login_info["user_id"] + + entries = boardlib.api.aurora.logbook_entries(args.board, user_id, token, args.grade_type, db_path=args.database) if args.output: with open(args.output, "w", encoding="utf-8") as output_file: diff --git a/src/boardlib/api/aurora.py b/src/boardlib/api/aurora.py index b339896..811d8e6 100644 --- a/src/boardlib/api/aurora.py +++ b/src/boardlib/api/aurora.py @@ -551,12 +551,7 @@ def calculate_tries_total(group): return group -def logbook_entries(board, username=None, password=None, token=None, user_id=None, grade_type="font", db_path=None): - if not (user_id and token): - login_info = login(board, username, password) - token = login_info["token"] - user_id = login_info["user_id"] - +def logbook_entries(board, user_id, token, grade_type="font", db_path=None): bids_entries = list(bids_logbook_entries(board, token, user_id, db_path)) raw_ascents_entries = get_logbook(board, token, user_id)