1
0
personal-data/obsidian_import/__init__.py

42 lines
1.2 KiB
Python
Raw Normal View History

2024-10-03 21:23:47 +00:00
"""Obsidian Import.
Sub-module for importing time-based data into Obsidian.
"""
from pathlib import Path
from .obsidian import ObsidianVault
from personal_data.util import load_csv_file
import datetime
from logging import getLogger
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))
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)
if unit:
v = str(v) + ' ' + unit
if v:
was_updated |= vault.add_statistic(date, output_key, v)
del input_key, output_key, unit, v
if was_updated:
num_updated += 1
del row, date
logger.info('Updated %d files', num_updated)