-
Notifications
You must be signed in to change notification settings - Fork 0
/
meet.py
151 lines (118 loc) · 4.06 KB
/
meet.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
import sqlite3
from flask import Flask, jsonify, request, redirect, Response
from datetime import datetime
import os
from dotenv import load_dotenv
from aristote import get_enrichment_version, get_transcript, request_enrichment
from minio import Minio
import requests
load_dotenv(".env")
DATABASE_URL = os.environ["DATABASE_URL"]
MINIO_ACCESS_KEY = os.environ["MINIO_ACCESS_KEY"]
MINIO_SECRET_KEY = os.environ["MINIO_SECRET_KEY"]
MINIO_URL = os.environ["MINIO_URL"]
MINIO_BUCKET = os.environ["MINIO_BUCKET"]
MEET_URL = os.environ["MEET_URL"]
MEET_SECRET = os.environ["MEET_SECRET"]
app = Flask(__name__)
def get_filename_by_enrichment_id(
conn: sqlite3.Connection, enrichment_id: str
) -> str | None:
cursor = conn.cursor()
cursor.execute(
"SELECT filename FROM enrichment_requests WHERE enrichment_id = ?", (enrichment_id,)
)
row = cursor.fetchone()
if row:
return row[0]
return None
def update_status_by_filename(conn: sqlite3.Connection, filename: str, status: str):
cursor = conn.cursor()
cursor.execute(
"""
UPDATE enrichment_requests
SET status = ?
WHERE filename = ?
""",
(status, filename),
)
conn.commit()
def add_line(filename: str, enrichment_id: str):
conn = sqlite3.connect(DATABASE_URL)
cursor = conn.cursor()
request_sent_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
status = "PENDING"
cursor.execute(
"""
INSERT INTO enrichment_requests (filename, enrichment_id, request_sent_at, status)
VALUES (?, ?, ?, ?)
""",
(filename, enrichment_id, request_sent_at, status),
)
conn.commit()
conn.close()
@app.route("/webhook/minio", methods=["POST"])
def minio_webhook():
data = request.get_json()
record = data["Records"][0]
s3 = record['s3']
bucket = s3['bucket']
bucket_name = bucket['name']
object = s3['object']
filename = object['key']
if bucket_name != MINIO_BUCKET or object['contentType'] != 'audio/ogg':
return "Not interested in this bucket"
if object['contentType'] != 'audio/ogg':
return "Not interested in this file type"
client = Minio(MINIO_URL,
access_key=MINIO_ACCESS_KEY,
secret_key=MINIO_SECRET_KEY,
)
url = client.presigned_get_object(MINIO_BUCKET, object_name=filename)
enrichment_id = request_enrichment(url)
add_line(filename, enrichment_id)
return ""
@app.route("/webhook/aristote", methods=["POST"])
def aristote_webhook():
data = request.get_json()
enrichment_id = data["id"]
conn = sqlite3.connect(DATABASE_URL)
filename = get_filename_by_enrichment_id(conn=conn, enrichment_id=enrichment_id)
room_id = filename.split("_")[2].split(".")[0]
if data["status"] == "SUCCESS":
initial_version_id = data["initialVersionId"]
if filename:
update_status_by_filename(conn=conn, filename=filename, status="SUCCESS")
enrichment_version = get_enrichment_version(
enrichment_id, initial_version_id
)
headers = {"Content-Type": "application/json", "Accept": "application/json"}
json = {
"transcript": enrichment_version["transcript"]["text"],
"summary": enrichment_version["notes"],
"secret": MEET_SECRET
}
requests.post(MEET_URL.replace("{room_id}", room_id), json=json, headers=headers)
return ""
elif data["status"] == "FAILURE":
update_status_by_filename(conn=conn, filename=filename, status="FAILURE")
return ""
def initiate_database():
conn = sqlite3.connect(DATABASE_URL)
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS enrichment_requests (
filename TEXT PRIMARY KEY,
enrichment_id TEXT,
request_sent_at DATETIME,
enrichment_notification_received_at DATETIME,
status TEXT
)
"""
)
conn.commit()
conn.close()
if __name__ == "__main__":
initiate_database()
app.run(host="0.0.0.0", debug=True)