1
0
libpurple-to-markdown/libpurple_to_markdown/__main__.py

62 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-10-25 23:12:30 +00:00
import argparse
2024-10-25 23:30:51 +00:00
import logging
from pathlib import Path
2024-10-26 12:08:22 +00:00
2024-10-26 13:17:46 +00:00
from . import (
Message,
filter_useless_messages,
format_messages,
merge_adjacent_messages,
parse_messages_in_chat_folder,
)
2024-10-26 12:08:22 +00:00
logger = logging.getLogger(__name__)
2024-10-26 11:47:39 +00:00
2024-10-26 13:17:46 +00:00
2024-10-26 13:05:12 +00:00
def group_messages_by_period(messages: list[Message]) -> dict[str, list[Message]]:
2024-10-26 13:17:46 +00:00
by_period: dict[str, list[Message]] = {}
2024-10-26 12:08:22 +00:00
for msg in messages:
2024-10-26 13:05:12 +00:00
period_key = f'{msg.sent_at.year}-{msg.sent_at.month:02}'
by_period.setdefault(period_key, []).append(msg)
2024-10-26 12:08:22 +00:00
del msg
2024-10-26 13:05:12 +00:00
return by_period
2024-10-26 13:17:46 +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-26 11:47:39 +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-26 12:08:22 +00:00
server = args.path.parent.name
receipient = args.path.name
all_messages = parse_messages_in_chat_folder(args.path)
2024-10-26 12:53:31 +00:00
all_messages = filter_useless_messages(all_messages)
2024-10-26 12:08:22 +00:00
all_messages = merge_adjacent_messages(all_messages)
2024-10-26 13:05:12 +00:00
messages_by_period = group_messages_by_period(all_messages)
2024-10-26 12:08:22 +00:00
2024-10-26 13:05:12 +00:00
for period_key, messages in messages_by_period.items():
output_file = args.output / f'{server} - {receipient} - {period_key}.md'
2024-10-26 13:17:46 +00:00
logger.info('Writing % 5d messages to %s', len(messages), output_file)
2024-10-26 12:08:22 +00:00
with open(output_file, 'w') as f:
2024-10-26 13:17:46 +00:00
f.write(
format_messages(
messages,
title=f'{server} - {receipient} - {period_key}',
),
)
2024-10-26 12:08:22 +00:00
2024-10-26 13:05:12 +00:00
del period_key, messages, output_file
2024-10-26 11:47:39 +00:00
if __name__ == '__main__':
main()