74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
import datetime
|
|
import requests
|
|
import json
|
|
import yaml
|
|
import uuid
|
|
import time
|
|
|
|
|
|
with open('config.yaml', 'r') as file:
|
|
configData = yaml.safe_load(file)
|
|
|
|
tokenResult = {}
|
|
getTokenUrl = configData['dataConfig']['getTokenUrl']
|
|
vod_channelNo = configData['video_config']['v1_channelNo']
|
|
# 告警信息url
|
|
putMessageUrl = configData['dataConfig']['putMessageUrl']
|
|
|
|
def get_token(tokenResult):
|
|
if 'token' in tokenResult and 'current_time' in tokenResult:
|
|
token_time = datetime.datetime.strptime(tokenResult['current_time'],
|
|
"%Y-%m-%d %H:%M:%S")
|
|
current_time = datetime.datetime.now()
|
|
time_diff = current_time - token_time
|
|
if time_diff.total_seconds() > 20 * 60:
|
|
# 过期重新请求 token
|
|
# print("token 已过期")
|
|
response = requests.post(getTokenUrl)
|
|
if response.status_code == 200:
|
|
data = json.loads(response.text)
|
|
if 'retCode' in data and data['retCode'] == '200':
|
|
token = data['responseBody']['token']
|
|
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
tokenResult['token'] = token
|
|
tokenResult['current_time'] = current_time
|
|
else:
|
|
tokenResult['error'] = data['errorDesc']
|
|
else:
|
|
tokenResult['error'] = response.status_code
|
|
token = tokenResult['token']
|
|
return token
|
|
|
|
|
|
def send_post_request(url, token, msg, picUrl, videoUrl):
|
|
payload = {
|
|
"tenantCode": "8",
|
|
"channelNo": vod_channelNo,
|
|
"alarmContent": msg,
|
|
"alarmTime": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
"picInfo": [
|
|
{"url": picUrl}
|
|
],
|
|
"videoInfo": [
|
|
{"url": videoUrl}
|
|
]
|
|
}
|
|
headers = {
|
|
'X-Access-Token': token,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
# print(url)
|
|
# print(headers)
|
|
# print(payload)
|
|
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
|
print(response)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
token = get_token()
|
|
print("token: ", token)
|
|
|
|
uuid_str = str(uuid.uuid4())[:6] + str(int(time.time())) # 生成UUID的前6位
|
|
|
|
upload_http_url_img = configData['minioConfig']['bucket_name'] + f'/{uuid_str}_{vod_channelNo}_.jpg'
|
|
send_post_request(putMessageUrl, token, "消息内容", upload_http_url_img, '') |