1
0
libpurple-to-markdown/libpurple_to_markdown/__main__.py
2024-10-26 14:53:31 +02:00

50 lines
1.4 KiB
Python

import argparse
import logging
from pathlib import Path
import logging
from . import (format_messages, merge_adjacent_messages,
parse_messages_in_chat_folder, Message, filter_useless_messages)
logger = logging.getLogger(__name__)
def group_messages_by_year(messages: list[Message]) -> dict[int, list[Message]]:
by_year = {}
for msg in messages:
by_year.setdefault(msg.sent_at.year, []).append(msg)
del msg
return by_year
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 = parse_messages_in_chat_folder(args.path)
all_messages = filter_useless_messages(all_messages)
all_messages = merge_adjacent_messages(all_messages)
messages_by_year = group_messages_by_year(all_messages)
for year, messages in messages_by_year.items():
output_file = args.output / f'{server} - {receipient} - {year}.md'
logger.info("Writing %d messages to %s", len(messages), output_file)
with open(output_file, 'w') as f:
f.write(format_messages(messages, title = f'{server} - {receipient} - {year}'))
del year, messages, output_file
if __name__ == '__main__':
main()