diff --git a/obsidian_import/__init__.py b/obsidian_import/__init__.py index 0e8f8e9..e8cb101 100644 --- a/obsidian_import/__init__.py +++ b/obsidian_import/__init__.py @@ -7,8 +7,10 @@ import datetime from logging import getLogger from pathlib import Path from typing import Any +from collections.abc import Iterator -from personal_data.util import load_csv_file +from personal_data.csv_import import start_end, determine_possible_keys, load_csv_file +from personal_data.activity import ActivitySample, Label, RealizedActivitySample, heuristically_realize_samples from .obsidian import Event, ObsidianVault @@ -17,6 +19,32 @@ logger = getLogger(__name__) Row = dict[str, Any] Rows = list[Row] +def iterate_samples_from_rows(rows: Rows) -> Iterator[ActivitySample]: + assert len(rows) > 0 + + if True: + event_data = rows[len(rows) // 2] # Hopefully select a useful representative. + possible_keys = determine_possible_keys(event_data) + logger.info('Found possible keys: %s', possible_keys) + del event_data + + assert len(possible_keys.time_start) + len(possible_keys.time_end) >= 1 + assert len(possible_keys.image) >= 0 + + for event_data in rows: + (start_at, end_at) = start_end(event_data, possible_keys) + labels = [Label(k, event_data[k]) for k in possible_keys.misc] + + # Create event + yield ActivitySample( + labels=tuple(labels), + start_at=start_at, + end_at=end_at, + ) + + del event_data + + def import_workout_csv(vault: ObsidianVault, rows: Rows) -> int: num_updated = 0 @@ -73,33 +101,35 @@ def import_step_counts_csv(vault: ObsidianVault, rows: Rows) -> int: def import_watched_series_csv(vault: ObsidianVault, rows: Rows) -> int: - # TODO: Update to using git_time_tracker event parsing system verb = 'Watched' + samples = heuristically_realize_samples(list(iterate_samples_from_rows(rows))) + + samples_per_date: dict[datetime.date, list[RealizedActivitySample]] = {} + for sample in samples: + date: datetime.date = sample.start_at.date() + samples_per_date.setdefault(date, []) + samples_per_date[date].append(sample) + del date, sample + del rows + + def map_to_event(sample: RealizedActivitySample) -> Event: + comment = '{} Episode {}: *{}*'.format( + sample.single_label_with_category('season.name'), + sample.single_label_with_category('episode.index'), + sample.single_label_with_category('episode.name'), + ) + return Event(sample.start_at.time(), + sample.end_at.time(), + verb, + sample.single_label_with_category('series.name'), + comment, + ) + num_updated = 0 - rows_per_date = {} - for row in rows: - date = row['me.last_played_time'].date() - rows_per_date.setdefault(date, []) - rows_per_date[date].append(row) - del date, row - del rows - - def map_to_event(row: Row) -> Event: - start = ( - row['me.last_played_time'].time().replace(second=0, microsecond=0, fold=0) - ) - end = start - comment = '{} Episode {}: *{}*'.format( - row['season.name'], - row['episode.index'], - row['episode.name'], - ) - return Event(start, end, verb, row['series.name'], comment) - - for date, rows in rows_per_date.items(): - events = [map_to_event(row) for row in rows] + for date, samples in samples_per_date.items(): + events = [map_to_event(sample) for sample in samples] was_updated = vault.add_events(date, events) if was_updated: