import argparse import logging from pathlib import Path from collections.abc import Iterable from . import ( filter_useless_messages, format_messages, libpurple, merge_adjacent_messages, synctech_sms, ) from .data import Message logger = logging.getLogger(__name__) def group_messages_by_chat_id(messages: Iterable[Message]) -> dict[str, list[Message]]: by_period: dict[str, list[Message]] = {} for msg in messages: by_period.setdefault(msg.chat_id, []).append(msg) del msg return by_period def group_messages_by_period(messages: Iterable[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('--purple', type=Path, dest='purple_folder') parser.add_argument('--synctech', type=Path, dest='synctech_sms_backup_file') parser.add_argument('--output', type=Path) return parser.parse_args() def main(): logging.basicConfig() logging.getLogger().setLevel('INFO') args = parse_args() if args.purple_folder: all_messages = libpurple.parse_messages_in_chat_folder(args.purple_folder) elif args.synctech_sms_backup_file: all_messages = synctech_sms.parse_messages_in_backup_xml_file(args.synctech_sms_backup_file) else: logger.fatal('No input file given!') return all_messages = list(all_messages) logger.info('%d messages after loading', len(all_messages)) all_messages = list(filter_useless_messages(all_messages)) logger.info('%d messages after filtering', len(all_messages)) messages_by_chat_id = group_messages_by_chat_id(all_messages) logger.info('%d message groups', len(messages_by_chat_id)) del all_messages for chat_id, messages_in_chat_original in messages_by_chat_id.items(): messages_in_chat = merge_adjacent_messages(messages_in_chat_original ) messages_by_period = group_messages_by_period(messages_in_chat) for period_key, messages in messages_by_period.items(): output_file = args.output / f'{chat_id} - {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'{chat_id} - {period_key}', ), ) del period_key, messages, output_file del chat_id, messages_in_chat_original, messages_in_chat if __name__ == '__main__': main()