|
| 1 | +import html |
| 2 | +from string import Template |
| 3 | + |
| 4 | +from models import Message |
| 5 | + |
| 6 | + |
| 7 | +def chats_to_txt(chats: list, directory_path: str): |
| 8 | + for chat in chats: |
| 9 | + messages = "\n".join([str(message) for message in chat.messages]) |
| 10 | + with open(f"{directory_path}/{chat.key_remote_jid}.txt", "w", encoding="utf-8") as file: |
| 11 | + file.write(chat.title + "\n" + messages) |
| 12 | + |
| 13 | + |
| 14 | +def chats_to_html(chats: list, filepath: str): |
| 15 | + chat_contents = "" |
| 16 | + chats_list = "" |
| 17 | + for chat in chats: |
| 18 | + # Hide empty chats |
| 19 | + if len(chat.messages) == 0: |
| 20 | + continue |
| 21 | + |
| 22 | + # Add the chat contents to the HTML |
| 23 | + t = Template("""<div class="chat" data-chatid="$chat_id">$messages</div>""") |
| 24 | + chat_contents += t.substitute( |
| 25 | + chat_id=_esc(chat.key_remote_jid), |
| 26 | + messages="".join([_message_to_html(m) for m in chat.messages]) |
| 27 | + ) |
| 28 | + |
| 29 | + # Add a row to the list of chats |
| 30 | + t = Template("""<div class="chat-partner"><a href="#$id" title="$phone_number">$title<div>$preview</div></a></div>""") |
| 31 | + last_message = chat.messages[-1].get_content() |
| 32 | + preview = last_message[0:55] if last_message is not None else "" |
| 33 | + chats_list += t.substitute( |
| 34 | + id=_esc(chat.key_remote_jid), |
| 35 | + phone_number=_esc(chat.phone_number), |
| 36 | + title=_esc(chat.title), |
| 37 | + preview=_esc(preview) |
| 38 | + ) |
| 39 | + |
| 40 | + _save_to_html_file(chat_contents, chats_list, filepath) |
| 41 | + |
| 42 | + |
| 43 | +def _message_to_html(m: Message) -> str: |
| 44 | + direction_class = " sent" if m.key_from_me else "" |
| 45 | + sender = _esc(m.get_sender_name()) |
| 46 | + content = _esc(m.get_content()) |
| 47 | + time = _esc(m.received_timestamp_str) |
| 48 | + |
| 49 | + if m.remote_resource: |
| 50 | + t = Template("""<div class="message$direction_class"><div class="sender">$sender</div>$content<div class="time">$time</div></div>""") |
| 51 | + return t.substitute(direction_class=direction_class, sender=sender, content=content, time=time) |
| 52 | + else: |
| 53 | + t = Template("""<div class="message$direction_class">$content<div class="time">$time</div></div>""") |
| 54 | + return t.substitute(direction_class=direction_class, content=content, time=time) |
| 55 | + |
| 56 | + |
| 57 | +def _esc(content) -> str: |
| 58 | + return html.escape(str(content)) |
| 59 | + |
| 60 | + |
| 61 | +def _load_file_content(filepath: str) -> str: |
| 62 | + with open(filepath, "r") as file: |
| 63 | + return file.read() |
| 64 | + |
| 65 | + |
| 66 | +def _save_to_html_file(chat_contents: str, chats_list: str, filepath: str): |
| 67 | + # Load the HTML template from file and populate it with data |
| 68 | + t = Template(_load_file_content("resources/template.html")) |
| 69 | + html_output = t.substitute( |
| 70 | + js_code=_load_file_content("resources/main.js"), |
| 71 | + css_code=_load_file_content("resources/styles.css"), |
| 72 | + chats_list=chats_list, |
| 73 | + chat_contents=chat_contents |
| 74 | + ) |
| 75 | + |
| 76 | + with open(filepath, "w", encoding="utf-8") as file: |
| 77 | + file.write(html_output) |
0 commit comments