2024-10-25 23:12:30 +00:00
|
|
|
import argparse
|
2024-10-25 23:30:51 +00:00
|
|
|
import logging
|
2024-10-25 22:34:53 +00:00
|
|
|
from pathlib import Path
|
2024-10-26 12:08:22 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from . import (format_messages, merge_adjacent_messages, parse_messages_in_chat_folder, Message)
|
2024-10-25 22:34:53 +00:00
|
|
|
|
2024-10-26 12:08:22 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2024-10-26 11:47:39 +00:00
|
|
|
|
2024-10-26 12:08:22 +00:00
|
|
|
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
|
2024-10-25 22:34:53 +00:00
|
|
|
|
2024-10-25 23:12:30 +00:00
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('path', type=Path)
|
2024-10-26 12:08:22 +00:00
|
|
|
parser.add_argument('--output', type=Path)
|
2024-10-25 23:12:30 +00:00
|
|
|
return parser.parse_args()
|
2024-10-25 22:34:53 +00:00
|
|
|
|
2024-10-26 11:47:39 +00:00
|
|
|
|
2024-10-25 22:34:53 +00:00
|
|
|
def main():
|
2024-10-25 23:30:51 +00:00
|
|
|
logging.basicConfig()
|
|
|
|
logging.getLogger().setLevel('INFO')
|
2024-10-25 23:12:30 +00:00
|
|
|
args = parse_args()
|
2024-10-25 22:34:53 +00:00
|
|
|
|
2024-10-26 12:08:22 +00:00
|
|
|
server = args.path.parent.name
|
|
|
|
receipient = args.path.name
|
|
|
|
|
|
|
|
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
|
2024-10-25 22:34:53 +00:00
|
|
|
|
2024-10-26 11:47:39 +00:00
|
|
|
|
2024-10-25 22:34:53 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|