diff --git a/obsidian_import/__init__.py b/obsidian_import/__init__.py index d53b9b5..30dca4b 100644 --- a/obsidian_import/__init__.py +++ b/obsidian_import/__init__.py @@ -11,7 +11,9 @@ from typing import Any from collections.abc import Iterator 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 personal_data.activity import (ActivitySample, Label, + RealizedActivitySample, heuristically_realize_samples, merge_adjacent_samples +) from .obsidian import Event, ObsidianVault @@ -111,9 +113,13 @@ class EventContent: comment: str -def import_activity_sample_csv(vault: ObsidianVault, rows: Rows, content_mapper) -> int: +def import_activity_sample_csv(vault: ObsidianVault, rows: Rows, + content_mapper, group_category: str | None = None) -> int: samples = heuristically_realize_samples(list(iterate_samples_from_rows(rows))) + if group_category is not None: + samples = merge_adjacent_samples(list(samples), group_category) + samples_per_date: dict[datetime.date, list[RealizedActivitySample]] = {} for sample in samples: date: datetime.date = sample.start_at.date() @@ -144,10 +150,11 @@ def import_activity_sample_csv(vault: ObsidianVault, rows: Rows, content_mapper) return num_updated -def import_activity_sample_csv_from_file(vault: ObsidianVault, data_path: Path, content_mapper) -> int: +def import_activity_sample_csv_from_file(vault: ObsidianVault, data_path: Path, + content_mapper, **kwargs) -> int: rows = load_csv_file(data_path) logger.info('Loaded CSV with %d lines (%s)', len(rows), data_path) - num_updated = import_activity_sample_csv(vault, rows, content_mapper) + num_updated = import_activity_sample_csv(vault, rows, content_mapper, **kwargs) logger.info('Updated %d files', num_updated) def map_watched_series_content(sample: RealizedActivitySample) -> EventContent: @@ -165,9 +172,7 @@ def map_watched_series_content(sample: RealizedActivitySample) -> EventContent: def map_games_played_content(sample: RealizedActivitySample) -> EventContent: subject = sample.single_label_with_category('game.name') - comment = '![]({})'.format( - sample.single_label_with_category('trophy.icon') - ) + comment = '' return EventContent( verb='Played', subject=subject, @@ -180,7 +185,9 @@ def import_watched_series_csv_from_file(vault: ObsidianVault) -> int: def import_played_games_csv_from_file(vault: ObsidianVault) -> int: data_path = Path('output/games_played_playstation.csv') - return import_activity_sample_csv_from_file(vault, data_path, map_games_played_content) + return import_activity_sample_csv_from_file(vault, data_path, + map_games_played_content, + group_category='game.name') def import_data(obsidian_path: Path, dry_run=True): vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None) diff --git a/obsidian_import/obsidian.py b/obsidian_import/obsidian.py index 71e3501..6b5a11f 100644 --- a/obsidian_import/obsidian.py +++ b/obsidian_import/obsidian.py @@ -108,14 +108,14 @@ class ObsidianVault: return True def add_events(self, date: datetime.date, events: list[Event]) -> bool: - if not self.read_only: - self._create_date_if_not_present(date) if self.read_only == 'silent': logger.info( 'Read-only ObsidianVault ignoring add_event(%s, "%s", ?)', date, events, ) + if not self.read_only: + self._create_date_if_not_present(date) contents = self._get_date_contents(date) if contents is None: @@ -179,6 +179,7 @@ class ObsidianVault: logger.info('File "%s" doesn\'t exist, creating...', date) with open(self._daily_template_path()) as f: template_text = f.read() + date_file.parent.mkdir(exist_ok=True, parents=True) with open(date_file, 'w') as f: f.write(template_text) @@ -234,8 +235,8 @@ def format_event_string(event: Event) -> str: buf.append(event.verb) buf.append(' [[') buf.append(event.subject) - buf.append(']]. ') - buf.append(event.comment.strip()) + buf.append(']].') + buf.append((' ' + event.comment).strip()) return ''.join(buf)