kefu/tools/kefu_robot.py
aozhiwei 2ef5c50d49 1
2020-05-06 18:59:42 +08:00

134 lines
5.5 KiB
Python

# -*- coding: utf-8 -*-
import sys
sys.path.append('../local_packages')
import q7
import f7
import os
import json
import time
import datetime
import hashlib
import urllib.request
import tornado.ioloop
import tornado.web
def md5Sign(params, secret, timestamp, connstr = '&', secret_connstr = ':'):
params_str = ''
for key in sorted(params.keys()):
params_str = params_str + key + '=' + str(params[key]) + connstr
if params_str != '' and connstr != '':
params_str = params_str[0:-1]
str1 = params_str + secret_connstr + str(timestamp) + secret
try:
m5 = hashlib.md5()
m5.update(str1.encode('utf-8'))
return m5.hexdigest()
except Exception as e:
print('md5Sign error: ' + str(e), flush=True)
def getDaySeconds(time_val, incdays):
time_zone = 8
dayseconds = int((time_val + time_zone * 3600)/3600/24 + incdays) * 3600 * 24 - 3600 * time_zone;
print(dayseconds, flush=True)
return dayseconds
def sendNotify(conf, sendtime):
try:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%s"), flush=True)
print('sendNotify start', flush=True)
params = {'key' : 'kingsome'}
secret = 'fc38349c5d084e920925e614c420be9f'
timestamp = time.time()
md5signstr = md5Sign(params, secret, timestamp)
url = conf['notify_url'] + '&timestamp=' + str(timestamp) + '&sign=' + md5signstr
req = urllib.request.Request(url)
data = urllib.request.urlopen(req).read()
print('sendNotify end', flush=True)
except Exception as e:
print('sendNotifu error: ' + str(e), flush=True)
#进入下一次循环
def _sendNotify(conf, sendtime):
if f7.app.ext['sendNotify']:
f7.timer.callAt(getDaySeconds(time.time(), 1) + sendtime,
lambda : _sendNotify(conf, sendtime))
return
def donekefu_callback():
f7.app.ext['sendNotify'] = False
f7.timer.callAt(getDaySeconds(time.time(), 1) + sendtime,
lambda : _sendNotify(conf, sendtime))
f7.app.ext['sendNotify'] = True
f7.app.createAsyncTask(donekefu_callback, sendNotify(conf, sendtime), (conf, sendtime))
def sendOrderNotify(conf, sendtime):
try:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%s"), flush=True)
print('sendOrderNotify start', flush=True)
params = {'key' : 'kingsome'}
secret = 'fc38349c5d084e920925e614c420be9f'
timestamp = time.time()
md5signstr = md5Sign(params, secret, timestamp)
url = conf['subscribe_notify_url'] + '&timestamp=' + str(timestamp) + '&sign=' + md5signstr
req = urllib.request.Request(url)
data = urllib.request.urlopen(req).read()
print('sendOrderNotify end', flush=True)
except Exception as e:
print('sendOrderNotify error: ' + str(e), flush=True)
#进入下一次循环
def _sendOrderNotify(conf, sendtime):
if f7.app.ext['sendOrder']:
f7.timer.callAt(getDaySeconds(time.time(), 1) + sendtime,
lambda : _sendOrderNotify(conf, sendtime))
return
def doneorder_callback():
f7.app.ext['sendOrder'] = False
f7.timer.callAt(getDaySeconds(time.time(), 1) + sendtime,
lambda : _sendOrderNotify(conf, sendtime))
f7.app.ext['sendOrder'] = True
f7.app.createAsyncTask(doneorder_callback, sendOrderNotify, (conf, sendtime))
def test(request):
conf = json.loads(open('../config/kefu_robot.json', 'r').read())
_sendOrderNotify(conf, conf['signtime'])
return '{"error_code":0, "error_msg":""}'
if __name__ == "__main__":
print('start!', flush=True)
q7.xPrint('pid %d' % os.getpid())
f7.app.init('/data/logs/kefu_robot/logs')
f7.udplog.info('kefu_robot start pid:' + str(os.getpid()))
f7.app.ext['sendOrder'] = False
f7.app.ext['sendNotify'] = False
conf = json.loads(open('../config/kefu_robot.json', 'r').read())
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['sendtime1'],
lambda : _sendNotify(conf, conf['sendtime1']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['sendtime2'],
lambda : _sendNotify(conf, conf['sendtime2']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['sendtime3'],
lambda : _sendNotify(conf, conf['sendtime3']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['signtime'],
lambda : _sendOrderNotify(conf, conf['signtime']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['boxtime'],
lambda : _sendOrderNotify(conf, conf['boxtime']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['diamondtime1'],
lambda : _sendOrderNotify(conf, conf['diamondtime1']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['diamondtime2'],
lambda : _sendOrderNotify(conf, conf['diamondtime2']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['diamondtime3'],
lambda : _sendOrderNotify(conf, conf['diamondtime3']))
f7.timer.callAt(getDaySeconds(time.time(), 1) + conf['seasontime'],
lambda : _sendOrderNotify(conf, conf['seasontime']))
f7.app.listen(conf['listen_port'])
f7.app.registerHandler('Ops', 'test', test)
f7.app.start()