-
Notifications
You must be signed in to change notification settings - Fork 0
/
review.py
345 lines (286 loc) · 11.1 KB
/
review.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# python 3.6+
from dateutil import parser, relativedelta, tz
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
from lh3.api import *
from datetime import datetime
from bs4 import BeautifulSoup
import pendulum
from jinja2 import Environment, FileSystemLoader
import os
import datetime
import logging
from uuid import uuid4
import pandas as pd
from htmldocx import HtmlToDocx
from pprint import pprint as print
new_parser = HtmlToDocx()
logging.basicConfig(format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %I:%M:%S %p")
root = os.path.dirname(os.path.abspath(__file__))
templates_dir = os.path.join(root, "templates")
env = Environment(loader=FileSystemLoader(templates_dir))
template = env.get_template("index.html")
def write_html_to_template(output, filePath):
"""create an HTML file using the default HTML template
Args:
output ([string]): HTML content
"""
# if file exist
print("write_html_to_template")
if os.path.exists(filePath):
os.remove(filePath)
with open(filePath, "w", encoding="utf-8") as file:
file.write(str(output))
else:
with open(filePath, "w", encoding="utf-8") as file:
file.write(str(output))
def retrieve_transcript(transcript_metadata, chat_id):
"""Return a Transcript (dict) containing metadata
The 'message' is the raw Transcript
Args:
transcript_metadata (dict): the Chat Transcript from LibraryH3lp
chat_id (int): The chat ID
Returns:
dict: Return a Transcript (dict) containing metadata
"""
print("retrieve_transcript")
queue_id = transcript_metadata["queue_id"]
guest = transcript_metadata["guest"].get("jid")
get_transcript = (
transcript_metadata["transcript"] or "<div>No transcript found</div>"
)
soup = BeautifulSoup(get_transcript, "html.parser")
divs = soup.find_all("div")
transcript = list()
counter = 1
for div in divs[1::]:
try:
transcript.append(
{
"chat_id": chat_id,
"message": str(div),
"counter": counter,
"chat_standalone_url": "https://ca.libraryh3lp.com/dashboard/queues/{0}/calls/REDACTED/{1}".format(
queue_id, chat_id
),
"guest": guest,
}
)
counter += 1
except:
pass
return transcript
def get_transcript(chat_id):
"""Get the chat info from LibraryH3lp, then retrieve the Transcript out of the Chat.
Args:
chat_id (int): A single Chat ID
Returns:
list(dict): trancript + metadata
"""
print("get_transcript")
client = Client()
chat_id = int(chat_id)
transcript_metadata = client.one("chats", chat_id).get()
transcript = retrieve_transcript(transcript_metadata, chat_id)
queue_name = transcript_metadata.get("queue").get("name")
started_date = parse(transcript_metadata.get("started")).strftime("%Y-%m-%d")
try:
logging.info("Retrieve transcript for {}".format(str(chat_id)))
print("Retrieve transcript for {}".format(str(chat_id)))
except:
print("error on print")
return transcript
def get_wait_and_duration(this_chat, started):
"""from the Chat time related metadata...
Args:
this_chat (int): Chat metadata
Returns:
list: return the Wait Time and Duration Time of the Chat
"""
print("get_wait_and_duration")
ended = None
accepted = None
wait = None
duration = None
try:
ended = pendulum.parse(this_chat.get("ended"))
except:
pass
try:
accepted = pendulum.parse(this_chat.get("accepted"))
except:
pass
try:
wait = accepted - started
except:
pass
try:
duration = ended - accepted
except:
pass
return [wait, duration]
def get_chat_metadata_for_header(transcript, duration, wait, this_queue):
"""Will generate the section that comes before the transcript on the HTML page.
It contains metadata information such as Duration and Wait Time of the chat
Args:
transcript ([type]): [description]
duration ([type]): [description]
wait ([type]): [description]
Returns:
string: returning HTML
"""
print("get_chat_metadata_for_header")
client = Client()
if "txt" in this_queue:
category = "Texting"
elif "proactive" in this_queue:
category = "Proactive"
elif "lavardez" in this_queue:
category = "Clavardez (fr)"
else:
category = "Web"
try:
duration_in_second = str(datetime.timedelta(0, duration.seconds))
except:
duration_in_second = 0
try:
wait_in_second = str(datetime.timedelta(0, wait.seconds))
except:
wait_in_second = 0
metadata_html = """
<div class="container" style="float: right;overflow:hidden;">
<ul style="overflow:hidden;">
<li>Duration : <em style="font-weight: 700;">{0}</em> </li>
<li>Wait : <em style="font-weight: 700;">{1}</em> </li>
<li><a href="{2}" target="_blank"> {2} </a></li>
<li><a href="https://forms.office.com/r/DnFj5jjG0h" target="_blank"><em>Form</em></a></li>
<li>Queue: <em style="font-weight: 700;">{3}</em> </li>
<li>Format: <em style="font-weight: 700;">{4}</em> </li>
</ul>
</div>
""".format(
duration_in_second,
wait_in_second,
transcript[0].get("chat_standalone_url"),
this_queue,
category,
)
return metadata_html
def line_by_line(transcript, previous_timestamp, operator, html_template, this_queue):
"""Parse each line of the Transcript
Args:
transcript (string): [description]
previous_timestamp (string): [description]
operator (string): [description]
html_template (string): [description]
this_queue (string): [description]
Returns:
[type]: HTML Template that will be written to the final file.
"""
print("line_by_line")
for message in transcript:
line_to_add = message.get("message")
try:
if previous_timestamp == None:
previous_timestamp = datetime.time.fromisoformat(
(line_to_add[5:10]).strip()
)
current_timestamp = datetime.time.fromisoformat((line_to_add[5:10]).strip())
previous_timestamp = datetime.datetime(
2011, 11, 11, previous_timestamp.hour, previous_timestamp.minute
)
current_timestamp = datetime.datetime(
2011, 11, 11, current_timestamp.hour, current_timestamp.minute
)
timedelta_obj = relativedelta(previous_timestamp, current_timestamp)
except:
pass
if timedelta_obj.minutes >= 5:
line_to_add = line_to_add.replace(
current_timestamp,
"""<b style="color:white; background-color:tomato">{0}} [+5] </b>""".format(
current_timestamp
),
)
if operator:
line_to_add = line_to_add.replace(operator, "operator", 1)
# import sys; sys.exit()
# print(line_to_add)
line_to_add = line_to_add.replace(
this_queue, "[email protected]", 1
)
logging.info("Adding a line")
html_template.append("<tr><td>" + line_to_add + "</td></tr>")
#print(html_template)
return html_template
def generate_html_template_from_transcript(chat_ids, filePath, chat_per_page):
print("generate_html_template_from_transcript")
html_template = []
tracking_guest_id = []
batch_number = 1
for index, chat in enumerate(chat_ids):
try:
client = Client()
this_chat = client.one("chats", chat).get()
operator = this_chat.get("operator", {}).get("name", "None")
if operator:
pass
else:
break
guest_id = this_chat.get("guest", {}).get("jid")
chat_id = this_chat.get("guest", {}).get("id")
tracking_guest_id.append({"guestID": guest_id[0:6], "chat_id": chat_id})
this_queue = f"{this_chat.get('queue', {}).get('name')}@chat.ca.libraryh3lp.com"
started = pendulum.parse(this_chat.get("started"))
wait, duration = get_wait_and_duration(this_chat, started)
if duration is None:
continue
previous_timestamp = None
transcript = get_transcript(chat)
metadata_html = get_chat_metadata_for_header(transcript, duration, wait, this_chat.get("queue", {}).get("name"))
html_template.extend([
f"<div class='row {'bg-light-blue' if batch_number % 2 == 0 else 'bg-light'}'><h2 class='text-center'> Chat #{index + 1}</h2>",
metadata_html,
'<div class="table-responsive"><table class="table mb-0 table-hover">'
])
html_template = line_by_line(transcript, previous_timestamp, operator, html_template, this_queue)
html_template.append("</table></div></div><div style='background-color: rgb(209, 241, 231); padding:20px 0px; margin:50px 0'></div>")
# Check if it's time to write out this batch
if (index + 1) % chat_per_page == 0 or (index + 1) == len(chat_ids):
html = "".join(html_template)
output = template.render(transcript=html)
filename = f"{filePath}-{str(uuid4())[:3]}-{batch_number}"
write_html_to_template(output, f"{filename}.html")
pd.DataFrame(tracking_guest_id).to_excel(f"{filename}.xlsx")
# Reset for the next batch
html_template = []
tracking_guest_id = []
batch_number += 1
except Exception as e:
print(f"error - generate_html_template_from_transcript: {e}")
def get_chats_for_this_time_range():
client = Client()
chats = client.chats()
chats = chats.list_day(
year=2022, month=9, day=6, to="2023-08-31"
)
return chats
if __name__ == "__main__":
#Getting chats from a date range
chats = get_chats_for_this_time_range()
#Remove anunswered chats
answered_chats = [chat for chat in chats if chat.get("accepted") is not None]
#Remove practice chats
chats = [chat for chat in answered_chats if not "practice" in chat.get("queue")]
#Transform to DataFrame
df = pd.DataFrame(chats)
#Keep only id
df = df[['id']]
#If using an excel file from LibraryH3lp Chat History
# This file contains Unanswered Chats AND Practice Chats also
#df = pd.read_excel("chatID_for_academic_year_2022-2023.xlsx")
sampled_df = df.sample(n=605, random_state=1) # Ensure df is defined and has the 'id' column
chat_ids = sampled_df['id'].tolist() # Assuming the column name is 'id'
filePath = "./output/ask"
chat_per_page = 121
generate_html_template_from_transcript(chat_ids, filePath, chat_per_page)