test / _backend /utils /time_funcs.py
aiioe's picture
Automated build & deploy
e454383 verified
Raw History Blame Contribute Delete
2.37 kB
import re
import json
from urllib.parse import urlencode, quote_plus
def extract_timestamps(transcript, docs) -> list[dict]:
"""
Extracts timestamps in HH:MM:SS format from the transcript.
Returns a list of tuples (timestamp_str, session_id, transcript_text).
"""
pattern = r'(\d{1,2}):(\d{2}):(\d{2})'
matches = re.finditer(pattern, transcript)
timestamps = []
timestamps_list = []
doc_payloads = [doc["payload"] for doc in docs]
for match in matches:
time_str = match.group(0)
start = match.start()
# Checking if timestamp is already present in the timestamps_list
if time_str in timestamps_list:
continue
transcript_text = ""
session_id = None
# Adding the data for the supporting documents
for doc in doc_payloads:
if time_str in doc["timestamp"]:
transcript_text = f'{doc["speaker"]} @ {time_str}: {doc["text"]}'
session_id = doc["filename"] # Get session name from document
break
if transcript_text and session_id:
timestamps.append(
{
"timestamp": time_str,
"timestamp_seconds": convert_time_to_seconds(time_str),
"transcript_text": transcript_text,
"filename": session_id,
}
)
timestamps_list.append(time_str)
return timestamps
def convert_time_to_seconds(time_str):
"""
Converts a time string HH:MM:SS to total seconds.
"""
h, m, s = map(int, time_str.split(':'))
return h * 3600 + m * 60 + s
def get_embed_url(start_time, uid):
"""
Constructs the embed URL with the specified start time.
"""
base_url = "https://ifad.sharepoint.com/sites/IFAD-CLEIFAD11andIFAD12/_layouts/15/embed.aspx"
params = {
"UniqueId": uid,
"nav": json.dumps({
"playbackOptions": {
"startTimeInSeconds": start_time
}
}),
"embed": json.dumps({
"hvm": True,
"ust": True,
"hv": "CopyEmbedCode"
}),
"referrer": "StreamWebApp",
"referrerScenario": "EmbedDialog.Create"
}
query_string = urlencode(params, quote_via=quote_plus)
return f"{base_url}?{query_string}"