import requests
import requests_mock
import time
import datetime
import json
json_data_1 = {
"links": [
{
"time": "2021-01-01 17:00:00",
"cameraID": "123",
"name": "北京",
"status": "1",
"details": "有垃圾,发"
},
{
"time": "2021-01-01 17:04:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,存起来,待发"
},
{
"time": "2021-01-01 17:06:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,判断时间 < 3 ,不发"
},
{
"time": "2021-01-01 17:10:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,判断时间 6 > 3 ,发"
},
{
"time": "2021-01-01 17:11:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,都不发"
},
{
"time": "2021-01-01 17:16:00",
"cameraID": "123",
"name": "北京",
"status": "1",
"details": "有垃圾,发"
},
{
"time": "2021-01-01 17:20:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,存起来,待发"
},
{
"time": "2021-01-01 17:23:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,比较时间 >= 3,发"
},
{
"time": "2021-01-01 17:26:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,都不发"
},
{
"time": "2021-01-01 17:29:00",
"cameraID": "123",
"name": "北京",
"status": "1",
"details": "有垃圾,发"
},
{
"time": "2021-01-01 17:33:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,存起来,待发"
},
{
"time": "2021-01-01 17:35:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,比较时间 < 3 ,不发"
},
{
"time": "2021-01-01 17:37:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,比较时间 > 3 ,发"
},
{
"time": "2021-01-01 17:38:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,都不发了"
},
{
"time": "2021-01-01 17:46:00",
"cameraID": "123",
"name": "北京",
"status": "1",
"details": "有垃圾,发"
},
{
"time": "2021-01-01 17:48:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,存起来,待发"
},
{
"time": "2021-01-01 17:52:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,比较时间 > 3 ,发"
},
{
"time": "2021-01-01 17:56:00",
"cameraID": "123",
"name": "北京",
"status": "0",
"details": "无垃圾,都不发"
}
]
}
def send_message(msg):
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print('send message {} ---------> {}'.format(current_time, msg))
@requests_mock.Mocker()
def func1(m):
m.get('htt://www.abc.com', json=json_data_1)
return requests.get('htt://www.abc.com').text
def minute_interval(before_time, after_time):
time_fmt = '%Y-%m-%d %H:%M:%S'
b_time = datetime.datetime.strptime(before_time, time_fmt)
a_time = datetime.datetime.strptime(after_time, time_fmt)
delta = a_time - b_time
return delta.seconds/60
_dict = dict()
if __name__ == '__main__':
last_result = None
save_result = None
result = func1()
first_json = json.loads(result)
for idx in first_json['links']:
print("idx=", idx)
name = idx['name']
cameraID = idx['cameraID']
if idx['status'] == '1':
send_message(name)
_dict[cameraID] = True
if last_result is not None:
if last_result['status'] == '1' and idx['status'] == '0':
_dict[cameraID] = True
save_result = idx
if save_result is not None and idx['status'] == '0':
b_time = save_result['time']
a_time = idx['time']
interval = minute_interval(b_time, a_time)
if interval >= 3 and _dict.get(cameraID):
print('{}'.format(save_result))
send_message('时间间隔 = {}'.format(interval))
_dict[cameraID] = False
print('=='*20)
last_result = idx
result
idx= {'time': '2021-01-01 17:00:00', 'cameraID': '123', 'name': '北京', 'status': '1', 'details': '有垃圾,发'}
send message 2021-11-06 13:46:48 ---------> 北京
========================================
idx= {'time': '2021-01-01 17:04:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
========================================
idx= {'time': '2021-01-01 17:06:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,判断时间 < 3 ,不发'}
========================================
idx= {'time': '2021-01-01 17:10:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,判断时间 6 > 3 ,发'}
{'time': '2021-01-01 17:04:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
send message 2021-11-06 13:46:48 ---------> 时间间隔 = 6.0
========================================
idx= {'time': '2021-01-01 17:11:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,都不发'}
========================================
idx= {'time': '2021-01-01 17:16:00', 'cameraID': '123', 'name': '北京', 'status': '1', 'details': '有垃圾,发'}
send message 2021-11-06 13:46:48 ---------> 北京
========================================
idx= {'time': '2021-01-01 17:20:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
========================================
idx= {'time': '2021-01-01 17:23:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,比较时间 >= 3,发'}
{'time': '2021-01-01 17:20:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
send message 2021-11-06 13:46:48 ---------> 时间间隔 = 3.0
========================================
idx= {'time': '2021-01-01 17:26:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,都不发'}
========================================
idx= {'time': '2021-01-01 17:29:00', 'cameraID': '123', 'name': '北京', 'status': '1', 'details': '有垃圾,发'}
send message 2021-11-06 13:46:48 ---------> 北京
========================================
idx= {'time': '2021-01-01 17:33:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
========================================
idx= {'time': '2021-01-01 17:35:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,比较时间 < 3 ,不发'}
========================================
idx= {'time': '2021-01-01 17:37:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,比较时间 > 3 ,发'}
{'time': '2021-01-01 17:33:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
send message 2021-11-06 13:46:48 ---------> 时间间隔 = 4.0
========================================
idx= {'time': '2021-01-01 17:38:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,都不发了'}
========================================
idx= {'time': '2021-01-01 17:46:00', 'cameraID': '123', 'name': '北京', 'status': '1', 'details': '有垃圾,发'}
send message 2021-11-06 13:46:48 ---------> 北京
========================================
idx= {'time': '2021-01-01 17:48:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
========================================
idx= {'time': '2021-01-01 17:52:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,比较时间 > 3 ,发'}
{'time': '2021-01-01 17:48:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,存起来,待发'}
send message 2021-11-06 13:46:48 ---------> 时间间隔 = 4.0
========================================
idx= {'time': '2021-01-01 17:56:00', 'cameraID': '123', 'name': '北京', 'status': '0', 'details': '无垃圾,都不发'}
========================================
|