-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.py
49 lines (45 loc) · 1.51 KB
/
post.py
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
import requests
from main import read_config_file
import base64
import os
import file_operations
folder = "post_img" # 保存图片的文件夹
if not os.path.exists(folder):
os.makedirs(folder)
else:
file_operations.delete_all_files(folder)
config = read_config_file("config.yaml")
api_ip , api_port = config['api_ip'], config['api_port']
# 定义要发送的JSON数据
data_to_send = {
"start_datetime": "2023-08-21 22:09:09",
"limit": 4
}
url = "http://{}:{}/query/recent-messages".format(api_ip, api_port)
response = requests.post(url, json=data_to_send)
if response.status_code == 200:
data = response.json()
# 解码Base64编码的图片
for row in data['data']:
row['image_data'] = base64.b64decode(row['image_data'])
# 对每一条数据进行处理
for row in data['data']:
ID = row['id']
camera = row['camera']
type = row['type']
date_time = row['date_time']
image_data = row['image_data']
# 打印数据
print(f"ID: {ID}")
print(f"Camera: {camera}")
print(f"Type: {type}")
print(f"Date Time: {date_time}")
print("------------------------")
# 保存图片
data_time_str = str(date_time)
formatted_date_time = data_time_str.replace(":", "_")
image_filename = os.path.join(folder, f"camera{camera}_{formatted_date_time}_{type}.jpg")
with open(image_filename, "wb") as img_file:
img_file.write(image_data)
else:
print("Error:", response.json())