import os import datetime import requests from fastapi import UploadFile URL_API = "https://ms.prology.net/api/v1" # def send_image(id, file: UploadFile, student_name: str, status: str): # id = str(id) # # Tạo folder theo ngày # today = datetime.datetime.now().strftime("%Y_%m_%d") # folder_path = f"./images/{today}" # if not os.path.exists(folder_path): # os.makedirs(folder_path) # # Tạo file name chuẩn # file_name = ( # f"{student_name}_" # f"{status}_at_{datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}.png" # ) # file_path = os.path.join(folder_path, file_name) # # Lưu file UploadFile xuống # with open(file_path, "wb") as f: # f.write(file.file.read()) # # Mở lại file để gửi API # with open(file_path, "rb") as image_file: # files = {"image": image_file} # data = {"id": id, "file_name": file_name} # try: # response = requests.post( # URL_API + "/admin/tracking/send-image", # data=data, # files=files # ) # response.raise_for_status() # res = response.json() # except Exception as e: # return {"status": False, "message": str(e)} # return res def send_image(id, image_bytes, student_name: str, status: str): id = str(id) 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 ("-", "_")) timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") file_name = f"{safe_student}_{safe_status}_at_{timestamp}.png" file_path = os.path.join(folder_path, file_name) # Lưu xuống with open(file_path, "wb") as f: f.write(image_bytes) # Gửi API try: with open(file_path, "rb") as image_file: response = requests.post( URL_API + "/admin/tracking/send-image", data={"id": id, "file_name": file_name}, files={"image": image_file} ) response.raise_for_status() except Exception as e: print("Send image failed:", e) def create_history(data): # Gửi yêu cầu POST với dữ liệu đã chỉ định response = requests.post(URL_API+"/admin/tracking/scan-create", data=data) res = response.json() print(res) return res