1
0

Compare commits

...

3 Commits

Author SHA1 Message Date
634bd5b03f
Ruff
All checks were successful
Run Python tests (through Pytest) / Test (push) Successful in 24s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 21s
2024-11-17 12:21:08 +01:00
6f22e8d239
Types 2024-11-17 12:20:52 +01:00
a4df23e8ff
More options for recurring import 2024-11-17 12:04:29 +01:00
2 changed files with 94 additions and 26 deletions

View File

@ -26,18 +26,42 @@ Matrix.
## Usage ## Usage
From the repository root: There are two main import patterns:
- One-off archival import: For when you have a large set of messages to import
for a service that you don't use very much anymore.
- Recurring import: For when you are still using the service, and want to
import on a recurring basis.
This program will be default not overwrite existing files, as the user might
have modified it.
Special consideration must be taking for recurring imports if you expect to be
modifying the resulting files, for example if you are inserting links
using Obsidian's unlinked mentions feature. You might want to use the
`--skip-this-period` flag to avoid importing the current period until it has
become the last period. That way you won't accidentally modify the log, because
it has been finalized.
### One-off
This is the recommended command for the one-off case:
```bash ```bash
python -m libpurple_to_markdown LOG_DIRECTORY --output OUTPUT_FOLDER python -m libpurple_to_markdown LOG_DIRECTORY --output OUTPUT_FOLDER
``` ```
It was made specifically for import into Obsidian, so it might not suite your ### Recurring
purposes, but it shouldn't be too difficult to adjust the formatting code.
This is the recommended command for the recurring import case:
```bash
python -m libpurple_to_markdown LOG_DIRECTORY --output OUTPUT_FOLDER --skip-this-period --period month
```
## TODO ## TODO
- [ ] Decode MMS parts and reconstruct image attachments. - [ ] SyncTech: Decode MMS parts and reconstruct image attachments.
""" """
import dataclasses import dataclasses

View File

@ -1,7 +1,8 @@
import argparse import argparse
import dataclasses import dataclasses
import datetime
import logging import logging
from collections.abc import Iterable, Iterator from collections.abc import Callable, Iterable, Iterator, Mapping
from pathlib import Path from pathlib import Path
from . import ( from . import (
@ -16,16 +17,19 @@ from .markdown import format_messages
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def group_messages(messages: Iterable[Message], key) -> dict[str, list[Message]]: def group_messages(
messages: Iterable[Message],
key_fn: Callable[[Message], str],
) -> dict[str, list[Message]]:
by_key: dict[str, list[Message]] = {} by_key: dict[str, list[Message]] = {}
for msg in messages: for msg in messages:
by_key.setdefault(key(msg), []).append(msg) by_key.setdefault(key_fn(msg), []).append(msg)
del msg del msg
return by_key return by_key
def group_messages_by_chat_id(messages: Iterable[Message]) -> dict[str, list[Message]]: def group_messages_by_chat_id(messages: Iterable[Message]) -> dict[str, list[Message]]:
return group_messages(messages, key=lambda msg: msg.chat_id) return group_messages(messages, key_fn=lambda msg: msg.chat_id)
def year_and_month_period_key(msg: Message): def year_and_month_period_key(msg: Message):
@ -43,24 +47,38 @@ def year_quarter_period_key(msg: Message):
MAX_AVERAGE_MESSAGES_PER_PERIOD = 120 MAX_AVERAGE_MESSAGES_PER_PERIOD = 120
TOO_FEW_MESSAGES_TO_CARE = 2
def group_messages_by_period(messages: Iterable[Message]) -> dict[str, list[Message]]: PERIOD_KEYS_BY_NAME: Mapping[str, Callable[[Message], str]] = {
possible_period_keys = [ 'full': (lambda msg: 'full'),
(lambda msg: 'full'), 'year': year_period_key,
year_period_key, 'quarter': year_quarter_period_key,
year_quarter_period_key, 'month': year_and_month_period_key,
year_and_month_period_key, }
]
for period_key in possible_period_keys:
grouped = group_messages(messages, key=period_key) def group_messages_by_period(
messages: Iterable[Message],
period_key: str | None = None,
) -> tuple[dict[str, list[Message]], Callable[[Message], str]]:
# Determine key function
possible_period_keys: Iterable[Callable[[Message], str]] = (
PERIOD_KEYS_BY_NAME.values()
)
if period_key is not None:
possible_period_keys = [PERIOD_KEYS_BY_NAME[period_key]]
del period_key
# Group by key
for period_key_fn in possible_period_keys:
grouped = group_messages(messages, key_fn=period_key_fn)
average_num_messages = sum(len(grouped[k]) for k in grouped) / len(grouped) average_num_messages = sum(len(grouped[k]) for k in grouped) / len(grouped)
if average_num_messages <= MAX_AVERAGE_MESSAGES_PER_PERIOD: if average_num_messages <= MAX_AVERAGE_MESSAGES_PER_PERIOD:
break break
del period_key, average_num_messages del average_num_messages
return grouped return grouped, period_key_fn
def replace_myself(messages: Iterable[Message], myself: str) -> Iterator[Message]: def replace_myself(messages: Iterable[Message], myself: str) -> Iterator[Message]:
@ -77,6 +95,17 @@ def parse_args():
parser.add_argument('--synctech', type=Path, dest='synctech_sms_backup_file') parser.add_argument('--synctech', type=Path, dest='synctech_sms_backup_file')
parser.add_argument('--output', type=Path) parser.add_argument('--output', type=Path)
parser.add_argument('--myself', type=str, default='Myself') parser.add_argument('--myself', type=str, default='Myself')
parser.add_argument('--overwrite', action='store_true', dest='overwrite_files')
parser.add_argument(
'--period',
dest='period_key',
choices=list(PERIOD_KEYS_BY_NAME.keys()),
)
parser.add_argument(
'--skip-this-period',
action='store_true',
dest='skip_this_period',
)
return parser.parse_args() return parser.parse_args()
@ -108,11 +137,14 @@ def main():
for chat_id, messages_in_chat_original in messages_by_chat_id.items(): for chat_id, messages_in_chat_original in messages_by_chat_id.items():
messages_in_chat = merge_adjacent_messages(messages_in_chat_original) messages_in_chat = merge_adjacent_messages(messages_in_chat_original)
if len(messages_in_chat) <= 2: if len(messages_in_chat) <= TOO_FEW_MESSAGES_TO_CARE:
logger.info(' "%s": Skipped due to too few messages', chat_id) logger.info(' "%s": Skipped due to too few messages', chat_id)
continue continue
messages_by_period = group_messages_by_period(messages_in_chat) messages_by_period, period_key_fn = group_messages_by_period(
messages_in_chat,
args.period_key,
)
logger.info( logger.info(
' "%s": %d messages, %d periods (%d msg/period avg)', ' "%s": %d messages, %d periods (%d msg/period avg)',
chat_id, chat_id,
@ -121,22 +153,34 @@ def main():
len(messages_in_chat_original) / len(messages_by_period), len(messages_in_chat_original) / len(messages_by_period),
) )
for period_key, messages in messages_by_period.items(): this_period_name = period_key_fn(Message(datetime.datetime.now(), '', '', ''))
for period_key_name, messages in messages_by_period.items():
file_escaped_chat_id = chat_id.replace(' ', '-') file_escaped_chat_id = chat_id.replace(' ', '-')
output_file = ( output_file = (
args.output / chat_id / f'{file_escaped_chat_id}-{period_key}.md' args.output / chat_id / f'{file_escaped_chat_id}-{period_key_name}.md'
) )
output_file.parent.mkdir(exist_ok=True)
logger.info('Writing % 5d messages to %s', len(messages), output_file) logger.info('Writing % 5d messages to %s', len(messages), output_file)
if this_period_name == period_key_name:
logger.info('Skipping due to --skip-this-period: %s', output_file)
continue
if output_file.exists() and not args.overwrite_files:
logger.info('Skipping existing file: %s', output_file)
continue
# Create folders and file
output_file.parent.mkdir(exist_ok=True, parents=True)
with open(output_file, 'w') as f: with open(output_file, 'w') as f:
f.write( f.write(
format_messages( format_messages(
messages, messages,
title=f'{chat_id} - {period_key}', title=f'{chat_id} - {period_key_name}',
), ),
) )
del period_key, messages, output_file del period_key_name, messages, output_file
del chat_id, messages_in_chat_original, messages_in_chat del chat_id, messages_in_chat_original, messages_in_chat