From c74920e478098caed97526552dfb367e1aad4b71 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Tue, 8 Oct 2024 21:22:18 +0200 Subject: [PATCH] Step counts --- obsidian_import/__init__.py | 45 +++++++++++++++++++++++++++++++------ obsidian_import/obsidian.py | 3 ++- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/obsidian_import/__init__.py b/obsidian_import/__init__.py index 061f941..e6377db 100644 --- a/obsidian_import/__init__.py +++ b/obsidian_import/__init__.py @@ -6,6 +6,7 @@ Sub-module for importing time-based data into Obsidian. import datetime from logging import getLogger from pathlib import Path +from typing import Any from personal_data.util import load_csv_file @@ -13,13 +14,7 @@ from .obsidian import ObsidianVault logger = getLogger(__name__) - -def import_data(obsidian_path: Path, dry_run=True): - vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None) - - data_path = Path('/home/jmaa/Notes/workout.csv') - rows = load_csv_file(data_path) - logger.info('Loaded CSV with %d lines', len(rows)) +def import_workout_csv(vault: ObsidianVault, rows: list[dict[str,Any]]) -> int: num_updated = 0 for row in rows: date = row['Date'] @@ -43,5 +38,41 @@ def import_data(obsidian_path: Path, dry_run=True): if was_updated: num_updated += 1 del row, date + return num_updated + +def import_step_counts_csv(vault: ObsidianVault, rows: list[dict[str,Any]]) -> int: + MINIMUM = 300 + + num_updated = 0 + + rows_per_day = {} + for row in rows: + date = row['Start'].date() + rows_per_day.setdefault(date, []) + rows_per_day[date].append(row) + del date, row + + + steps_per_day = { date: sum(row['Steps'] for row in rows) for date, rows in rows_per_day.items()} + + for date, steps in steps_per_day.items(): + if steps < MINIMUM: + continue + was_updated = vault.add_statistic(date, 'Steps', steps) + if was_updated: + num_updated += 1 + del date, steps, was_updated + + return num_updated + +def import_data(obsidian_path: Path, dry_run=True): + vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None) + + #data_path = Path('/home/jmaa/Notes/workout.csv') + data_path = Path('/home/jmaa/personal-archive/misc-data/step_counts_2023-07-26_to_2024-09-21.csv') + rows = load_csv_file(data_path) + logger.info('Loaded CSV with %d lines', len(rows)) + #num_updated = import_workout_csv(vault, rows) + num_updated = import_step_counts_csv(vault, rows) logger.info('Updated %d files', num_updated) diff --git a/obsidian_import/obsidian.py b/obsidian_import/obsidian.py index 311435b..fd82344 100644 --- a/obsidian_import/obsidian.py +++ b/obsidian_import/obsidian.py @@ -41,9 +41,10 @@ class ObsidianVault: ) -> bool: if self.read_only == 'silent': logger.info( - 'Real only ObsidianVault ignoring add_statistic(%s, "%s", ?)', + 'Real only ObsidianVault ignoring add_statistic(%s, "%s", %s)', date, statistic_key, + amount, ) return False