Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
166 lines (140 loc) · 5.96 KB
/
Copy pathmain.py
File metadata and controls
166 lines (140 loc) · 5.96 KB
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
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from deep_translator import GoogleTranslator
from gtts import gTTS
import os, threading, time, uuid, base64, io
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
connected_users = {}
@app.get("/")
async def home():
return HTMLResponse(open("static/index.html", "r", encoding="utf-8").read())
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
user_id = uuid.uuid4().hex[:8]
connected_users[user_id] = {"socket": websocket, "lang": "en", "name": "Usuário"}
print(f"🔗 Usuário {user_id} conectado")
try:
while True:
data = await websocket.receive_json()
# --- Registro inicial do idioma e nome ---
if data["type"] == "register":
connected_users[user_id]["lang"] = data.get("lang", "en")
connected_users[user_id]["name"] = data.get("name", f"User-{user_id[:4]}")
await websocket.send_json({
"type": "system",
"msg": f"✅ Conectado como {connected_users[user_id]['name']} ({connected_users[user_id]['lang']})"
})
continue
# --- Mantém conexão viva ---
if data["type"] == "ping":
continue
# --- Mensagem de chat ---
if data["type"] == "message":
await handle_text_message(user_id, data["text"])
# --- Envio de arquivo ---
if data["type"] == "file":
await handle_file_message(user_id, data)
except WebSocketDisconnect:
print(f"❌ Usuário {user_id} desconectado")
except Exception as e:
print("⚠️ Erro:", e)
finally:
connected_users.pop(user_id, None)
await websocket.close()
# -------------------------------------------------------------------
# 📘 LIDA COM MENSAGENS DE TEXTO
# -------------------------------------------------------------------
async def handle_text_message(user_id, text):
sender = connected_users[user_id]
sender_lang = sender["lang"]
sender_name = sender["name"]
for uid, info in list(connected_users.items()):
if uid == user_id:
continue # não envia para si mesmo
target_ws = info["socket"]
target_lang = info["lang"]
# Tradução automática
try:
translated = GoogleTranslator(source=sender_lang, target=target_lang).translate(text)
except Exception as e:
print("Erro na tradução:", e)
translated = text
# --- Gera áudio em memória (data URL) ---
audio_data_url = None
try:
tts = gTTS(translated, lang=target_lang)
bio = io.BytesIO()
tts.write_to_fp(bio)
bio.seek(0)
audio_b64 = base64.b64encode(bio.read()).decode("utf-8")
audio_data_url = f"data:audio/mp3;base64,{audio_b64}"
except Exception as e:
print("⚠️ Erro ao gerar áudio:", e)
audio_data_url = None
# --- Mantém compatibilidade com o antigo sistema de arquivos ---
audio_file = f"static/audio_{uuid.uuid4().hex[:8]}.mp3"
try:
if audio_data_url:
with open(audio_file, "wb") as f:
header, b64 = audio_data_url.split(",", 1)
f.write(base64.b64decode(b64))
threading.Thread(
target=lambda f=audio_file: (time.sleep(30), os.remove(f) if os.path.exists(f) else None)
).start()
except Exception as e:
print("Erro ao salvar áudio:", e)
audio_file = None
# --- Envia tradução + áudio ---
await target_ws.send_json({
"type": "translation",
"from": sender_lang,
"original": text,
"translated": translated,
"audio": f"/{audio_file}" if audio_file else None,
"audio_data": audio_data_url,
"name": sender_name
})
# -------------------------------------------------------------------
# 📁 LIDA COM ENVIO DE ARQUIVOS (IMAGENS, VÍDEOS, ETC.)
# -------------------------------------------------------------------
# -------------------------------------------------------------------
# 📁 LIDA COM ENVIO DE ARQUIVOS (IMAGENS, VÍDEOS, ÁUDIOS, ETC.) — com id
# -------------------------------------------------------------------
async def handle_file_message(user_id, data):
sender = connected_users[user_id]
sender_name = sender["name"]
# aceitar os campos enviados pelo JS
file_name = data.get("fileName", f"arquivo_{uuid.uuid4().hex[:6]}")
mime_type = data.get("mimeType", "application/octet-stream")
base64_data = data.get("file") # vem como dataURL: data:image/png;base64,...
# criar pasta e salvar
os.makedirs("static/uploads", exist_ok=True)
file_ext = os.path.splitext(file_name)[1] or ".bin"
file_path = f"static/uploads/{uuid.uuid4().hex[:8]}{file_ext}"
try:
header, encoded = base64_data.split(",", 1)
with open(file_path, "wb") as f:
f.write(base64.b64decode(encoded))
except Exception as e:
print("Erro ao salvar arquivo:", e)
return
# id único para o arquivo (para deduplicação)
msg_id = uuid.uuid4().hex
# Envia para todos os outros (não para o remetente)
for uid, info in list(connected_users.items()):
try:
if uid == user_id:
continue
await info["socket"].send_json({
"type": "file",
"id": msg_id,
"name": sender_name,
"file": f"/{file_path}",
"fileName": file_name,
"mimeType": mime_type
})
except Exception as e:
print("Erro ao enviar arquivo para", uid, ":", e)