2024-10-03 21:23:47 +00:00
|
|
|
"""Obsidian Import.
|
|
|
|
|
|
|
|
Sub-module for importing time-based data into Obsidian.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
from logging import getLogger
|
2024-10-03 21:24:12 +00:00
|
|
|
from pathlib import Path
|
2024-10-08 19:22:18 +00:00
|
|
|
from typing import Any
|
2024-10-03 21:24:12 +00:00
|
|
|
|
|
|
|
from personal_data.util import load_csv_file
|
|
|
|
|
2024-10-08 20:57:41 +00:00
|
|
|
from .obsidian import ObsidianVault, Event
|
2024-10-03 21:24:12 +00:00
|
|
|
|
2024-10-03 21:23:47 +00:00
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2024-10-08 19:22:18 +00:00
|
|
|
def import_workout_csv(vault: ObsidianVault, rows: list[dict[str,Any]]) -> int:
|
2024-10-03 21:23:47 +00:00
|
|
|
num_updated = 0
|
|
|
|
for row in rows:
|
|
|
|
date = row['Date']
|
|
|
|
was_updated = False
|
|
|
|
mapping = {
|
|
|
|
'Cycling (mins)': ('Cycling (Duration)', 'minutes'),
|
|
|
|
'Cycling (kcals)': ('Cycling (kcals)', ''),
|
|
|
|
'Weight (Kg)': ('Weight (Kg)', ''),
|
|
|
|
}
|
|
|
|
|
|
|
|
for input_key, (output_key, unit) in mapping.items():
|
|
|
|
v = row.get(input_key)
|
2024-10-03 21:32:30 +00:00
|
|
|
if v is not None:
|
|
|
|
if unit:
|
|
|
|
v = str(v) + ' ' + unit
|
|
|
|
was_updated |= vault.add_statistic(date, output_key, v)
|
|
|
|
if input_key != output_key:
|
|
|
|
was_updated |= vault.add_statistic(date, input_key, None)
|
2024-10-03 21:23:47 +00:00
|
|
|
del input_key, output_key, unit, v
|
|
|
|
|
|
|
|
if was_updated:
|
|
|
|
num_updated += 1
|
|
|
|
del row, date
|
2024-10-08 19:22:18 +00:00
|
|
|
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
|
|
|
|
|
2024-10-08 20:57:41 +00:00
|
|
|
def import_played_dates_csv(vault: ObsidianVault, rows: list[dict[str,Any]]) -> int:
|
|
|
|
date = datetime.date(2024,10,9)
|
|
|
|
event = Event(datetime.time(12,00), datetime.time(12,00), 'Tested', 'Obsidian Import')
|
|
|
|
updated = vault.add_event(date, event)
|
|
|
|
|
|
|
|
return updated and 1 or 0
|
|
|
|
|
2024-10-08 19:22:18 +00:00
|
|
|
def import_data(obsidian_path: Path, dry_run=True):
|
|
|
|
vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None)
|
|
|
|
|
2024-10-08 20:57:41 +00:00
|
|
|
if False:
|
|
|
|
data_path = Path('/home/jmaa/Notes/workout.csv')
|
|
|
|
rows = load_csv_file(data_path)
|
|
|
|
logger.info('Loaded CSV with %d lines', len(rows))
|
|
|
|
num_updated = import_workout_csv(vault, rows)
|
|
|
|
logger.info('Updated %d files', num_updated)
|
|
|
|
|
|
|
|
if False:
|
|
|
|
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_step_counts_csv(vault, rows)
|
|
|
|
logger.info('Updated %d files', num_updated)
|
|
|
|
|
|
|
|
if True:
|
|
|
|
num_updated = import_played_dates_csv(vault, []) # TODO
|
|
|
|
logger.info('Updated %d files', num_updated)
|
|
|
|
|
2024-10-03 21:23:47 +00:00
|
|
|
|