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 collections.abc import Iterator
from personal_data.csv_import import start_end, determine_possible_keys, 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 personal_data.activity import (ActivitySample, Label,
RealizedActivitySample, heuristically_realize_samples, merge_adjacent_samples
)
from .obsidian import Event, ObsidianVault from .obsidian import Event, ObsidianVault
@ -111,9 +113,13 @@ class EventContent:
comment: str 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))) 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]] = {} samples_per_date: dict[datetime.date, list[RealizedActivitySample]] = {}
for sample in samples: for sample in samples:
date: datetime.date = sample.start_at.date() 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 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) rows = load_csv_file(data_path)
logger.info('Loaded CSV with %d lines (%s)', len(rows), 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) logger.info('Updated %d files', num_updated)
def map_watched_series_content(sample: RealizedActivitySample) -> EventContent: 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: def map_games_played_content(sample: RealizedActivitySample) -> EventContent:
subject = sample.single_label_with_category('game.name') subject = sample.single_label_with_category('game.name')
comment = '![]({})'.format( comment = ''
sample.single_label_with_category('trophy.icon')
)
return EventContent( return EventContent(
verb='Played', verb='Played',
subject=subject, 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: def import_played_games_csv_from_file(vault: ObsidianVault) -> int:
data_path = Path('output/games_played_playstation.csv') 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): def import_data(obsidian_path: Path, dry_run=True):
vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None) vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None)

View File

@ -108,14 +108,14 @@ class ObsidianVault:
return True return True
def add_events(self, date: datetime.date, events: list[Event]) -> bool: 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': if self.read_only == 'silent':
logger.info( logger.info(
'Read-only ObsidianVault ignoring add_event(%s, "%s", ?)', 'Read-only ObsidianVault ignoring add_event(%s, "%s", ?)',
date, date,
events, events,
) )
if not self.read_only:
self._create_date_if_not_present(date)
contents = self._get_date_contents(date) contents = self._get_date_contents(date)
if contents is None: if contents is None:
@ -179,6 +179,7 @@ class ObsidianVault:
logger.info('File "%s" doesn\'t exist, creating...', date) logger.info('File "%s" doesn\'t exist, creating...', date)
with open(self._daily_template_path()) as f: with open(self._daily_template_path()) as f:
template_text = f.read() template_text = f.read()
date_file.parent.mkdir(exist_ok=True, parents=True)
with open(date_file, 'w') as f: with open(date_file, 'w') as f:
f.write(template_text) f.write(template_text)
@ -234,8 +235,8 @@ def format_event_string(event: Event) -> str:
buf.append(event.verb) buf.append(event.verb)
buf.append(' [[') buf.append(' [[')
buf.append(event.subject) buf.append(event.subject)
buf.append(']]. ') buf.append(']].')
buf.append(event.comment.strip()) buf.append((' ' + event.comment).strip())
return ''.join(buf) return ''.join(buf)