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