48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
"""Obsidian Import.
|
|
|
|
Sub-module for importing time-based data into Obsidian.
|
|
"""
|
|
|
|
import datetime
|
|
from logging import getLogger
|
|
from pathlib import Path
|
|
|
|
from personal_data.util import load_csv_file
|
|
|
|
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))
|
|
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 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)
|
|
del input_key, output_key, unit, v
|
|
|
|
if was_updated:
|
|
num_updated += 1
|
|
del row, date
|
|
|
|
logger.info('Updated %d files', num_updated)
|