59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
|
|
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 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 |