57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
|
|
import os
|
|
import datetime
|
|
import requests
|
|
from fastapi import UploadFile
|
|
|
|
URL_API = "http://172.16.6.38:8000/api/v1"
|
|
|
|
|
|
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
|
|
|
|
|
|
def users(params):
|
|
# Gửi yêu cầu POST với dữ liệu đã chỉ định
|
|
response = requests.get(URL_API+"/admin/timekeeping", params=params, headers={"authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL21zLnByb2xvZ3kubmV0L2FwaS92MS9hZG1pbi9sb2dpbiIsImlhdCI6MTc1Njg2MDQ1OSwiZXhwIjoxNzg4Mzk2NDU5LCJuYmYiOjE3NTY4NjA0NTksImp0aSI6IkRrb0NLbHBKV1pkNnZCN0QiLCJzdWIiOiIxNSIsInBydiI6ImQyZmYyOTMzOWE4YTNlODJjMzU4MmE1YThlNzM5ZGYxNzg5YmIxMmYifQ.DoHqHeAGGxpvzlNQ9dAZjZf2Yl573XCgNBT8ZiSx5N4"})
|
|
res = response.json()
|
|
|
|
return res
|