1
0
libpurple-to-markdown/libpurple_to_markdown/__main__.py
Jon Michael Aanes 91ca8d66d8
All checks were successful
Run Python tests (through Pytest) / Test (push) Successful in 25s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 22s
Restructure by moving datastructures and libpurple parsing into own module
2024-10-31 19:00:07 +01:00

62 lines
1.6 KiB
Python

import argparse
import logging
from pathlib import Path
from . import (
filter_useless_messages,
format_messages,
libpurple,
merge_adjacent_messages,
)
from .data import Message
logger = logging.getLogger(__name__)
def group_messages_by_period(messages: list[Message]) -> dict[str, list[Message]]:
by_period: dict[str, list[Message]] = {}
for msg in messages:
period_key = f'{msg.sent_at.year}-{msg.sent_at.month:02}'
by_period.setdefault(period_key, []).append(msg)
del msg
return by_period
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('path', type=Path)
parser.add_argument('--output', type=Path)
return parser.parse_args()
def main():
logging.basicConfig()
logging.getLogger().setLevel('INFO')
args = parse_args()
server = args.path.parent.name
receipient = args.path.name
all_messages = libpurple.parse_messages_in_chat_folder(args.path)
all_messages = filter_useless_messages(all_messages)
all_messages = merge_adjacent_messages(all_messages)
messages_by_period = group_messages_by_period(all_messages)
for period_key, messages in messages_by_period.items():
output_file = args.output / f'{server} - {receipient} - {period_key}.md'
logger.info('Writing % 5d messages to %s', len(messages), output_file)
with open(output_file, 'w') as f:
f.write(
format_messages(
messages,
title=f'{server} - {receipient} - {period_key}',
),
)
del period_key, messages, output_file
if __name__ == '__main__':
main()