-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_messages.py
153 lines (128 loc) · 4.66 KB
/
write_messages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pdfkit, os
import db_viewer as db_v
import db_insertion_update
from datetime import datetime
from dotenv import load_dotenv
from bs4 import BeautifulSoup
# Load environnement variables from .env
dotenv_path = "variables.env"
load_dotenv(dotenv_path)
OUTPUT_HTML = os.getenv("OUTPUT_HTML")
OUTPUT_PDF = os.getenv("OUTPUT_PDF")
TITLE = os.getenv("TITLE")
CSV_TAGS = os.getenv("CSV_TAGS")
def generate_html(title, list_of_messages_obj):
"""
Generate an HTML report with the given title and list of message object.
Args:
title (str): The title of the HTML report.
list_of_messages_obj (list): A list of message objects to be included in the report.
Returns:
str: The generated HTML content as a string.
"""
# Obtenez la date et l'heure actuelles
current_datetime = datetime.now().strftime("%I:%M %p, %B %d, %Y")
# Début de la page HTML
html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{}</title>
<style>
@page {{
size: A4;
margin: 0;
}}
body {{
font-size: 11px;
margin: 0;
padding: 0;
}}
.email-container {{
max-width: 100%;
padding: 20px;
box-sizing: border-box;
}}
.email-header {{
background-color: #007bff;
color: #fff;
padding: 10px;
}}
.email-body {{
padding: 20px;
}}
</style>
</head>
<body>
<h1>{}</h1>
<p>Generated with MessageTrackPro, <a href="https://github.com/ValentinLibouton/MessageTrackPro">https://github.com/ValentinLibouton/MessageTrackPro</a>, {},</p>
""".format(title, title, current_datetime)
# Insérer les données
for i, message in enumerate(list_of_messages_obj):
# Numéro de message (optionnel)
html += "<h2>Message {}</h2>".format(i + 1)
# Ouvrir une div pour le message
html += '<div style="clear: both;">'
# Insérer les détails du message
html += "<b>Date:</b> {} {} <b>Message id:</b> {} <b>Filename:</b> {}<br>".format(
message.date, message.time, message.message_id, message.filename)
html += "<b>Sender:</b> {} {} <b>Subject:</b> {}<br>".format(
message.sender_name,
message.sender_address,
message.subject)
html += "<b>Recipient:</b> {} {} <b>Recipient type:</b> {}<br>".format(
message.recipient_name,
message.recipient_address,
message.recipient_type)
html += "<b>Attachments:</b> {}<br>".format(message.attachments)
html += "<b>Tags:</b> {}<br>".format(message.tags)
# Insérer le contenu du message
html += "<b>Body:</b><br>{}".format(message.content)
# Fermer la div pour le message
html += '</div>'
# Séparation entre les messages
if i < len(list_of_messages_obj) - 1:
html += "<hr>"
# Fin du conteneur de l'email
html += """
</div>
</div>
"""
# Pied de page avec la date et l'heure actuelles
html += """
<br>
<p>Generated with MessageTrackPro, <a href="https://github.com/ValentinLibouton/MessageTrackPro">https://github.com/ValentinLibouton/MessageTrackPro</a>, {},</p>
</body>
</html>
""".format(current_datetime)
return html
def html_to_pdf(html_filename, pdf_filename):
"""
Convert an HTML file to a PDF file using the specified filenames.
Args:
html_filename (str): The filename of the input HTML file.
pdf_filename (str): The filename of the output PDF file.
Returns:
None
"""
# Convertir HTML en PDF
pdfkit.from_file(html_filename, pdf_filename, options={'page-size': 'A4', 'no-images': None})
def write_messages(title, list_of_messages_obj):
"""
Generate an HTML report with the provided title and list of message objects, and convert it to a PDF.
Args:
title (str): The title of the HTML report.
list_of_messages_obj (list): A list of message objects to be included in the report.
Returns:
None
"""
# Generation du code HTML
html_content = generate_html(title=title, list_of_messages_obj=list_of_messages_obj)
# Écriture du contenu HTML dans un fichier
with open(OUTPUT_HTML, "w") as html_file:
html_file.write(html_content)
# Convertion en PDF
html_to_pdf(OUTPUT_HTML, OUTPUT_PDF)
if __name__ == "__main__":
pass