1
0

Emit to folder

This commit is contained in:
Jon Michael Aanes 2024-10-26 14:08:22 +02:00
parent 4afb0f1364
commit 0722dc96e5
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -1,13 +1,23 @@
import argparse import argparse
import logging import logging
from pathlib import Path from pathlib import Path
import logging
from . import format_messages, merge_adjacent_messages, parse_messages_in_chat_folder from . import (format_messages, merge_adjacent_messages, parse_messages_in_chat_folder, Message)
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(): def parse_args():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('path', type=Path) parser.add_argument('path', type=Path)
parser.add_argument('--output', type=Path)
return parser.parse_args() return parser.parse_args()
@ -16,9 +26,21 @@ def main():
logging.getLogger().setLevel('INFO') logging.getLogger().setLevel('INFO')
args = parse_args() args = parse_args()
messages = parse_messages_in_chat_folder(args.path) server = args.path.parent.name
messages = merge_adjacent_messages(messages) receipient = args.path.name
print(format_messages(messages))
all_messages = parse_messages_in_chat_folder(args.path)
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 to %s", output_file)
with open(output_file, 'w') as f:
f.write(format_messages(messages))
del year, messages, output_file
if __name__ == '__main__': if __name__ == '__main__':