1
0

Games played import works

This commit is contained in:
Jon Michael Aanes 2024-10-22 00:04:34 +02:00
parent a872ed1e85
commit be0a30298d
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 20 additions and 12 deletions

View File

@ -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)

View File

@ -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)
@ -235,7 +236,7 @@ def format_event_string(event: Event) -> str:
buf.append(' [[')
buf.append(event.subject)
buf.append(']].')
buf.append(event.comment.strip())
buf.append((' ' + event.comment).strip())
return ''.join(buf)