48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
|
|
import os
|
|
import base64
|
|
import datetime
|
|
import requests
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
HOST = os.getenv("MS_HOST", "http://10.20.2.26:3002")
|
|
AUTH_TOKEN = os.getenv("MS_AUTH_TOKEN", "")
|
|
|
|
|
|
|
|
def sync_checkin(email: str, timestamp_ms: int, image_data: bytes, student_name: str, status: str):
|
|
today = datetime.datetime.now().strftime("%Y_%m_%d")
|
|
folder_path = f"./images/{today}"
|
|
os.makedirs(folder_path, exist_ok=True)
|
|
|
|
safe_student = "".join(c for c in student_name if c.isalnum() or c in ("-", "_"))
|
|
safe_status = "".join(c for c in status if c.isalnum() or c in ("-", "_"))
|
|
ts_str = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
|
|
file_path = os.path.join(folder_path, f"{safe_student}_{safe_status}_at_{ts_str}.png")
|
|
with open(file_path, "wb") as f:
|
|
f.write(image_data)
|
|
|
|
image_b64 = "data:image/jpeg;base64," + base64.b64encode(image_data).decode("utf-8")
|
|
payload = {
|
|
"email": email,
|
|
"time": timestamp_ms,
|
|
"image": image_b64,
|
|
|
|
}
|
|
headers = {"Authorization": f"Bearer {AUTH_TOKEN}"}
|
|
print("[sync_checkin] payload:", {k: v for k, v in payload.items() if k != "image"})
|
|
try:
|
|
response = requests.post(HOST + "/api/log-time/check-in-out", json=payload, headers=headers)
|
|
response.raise_for_status()
|
|
res = response.json()
|
|
print("[sync_checkin] response:", res)
|
|
return res
|
|
except Exception as e:
|
|
print("[sync_checkin] failed:", e)
|
|
return {}
|
|
|
|
|
|
|