利用钉钉机器人API发送东方财富交易信息

Python
335
0
0
2022-04-22

利用空余时间完成了一个对当日东方财富交易跟踪的程序,可以将当日完成的交易利用钉钉机器人API发送至聊天。

import json
import requests
from datetime import datetime

year = datetime.now().year
month = datetime.now().month
day = datetime.now().day

url = 'https://jywg.18.cn/Search/GetDealData?validatekey=e2fbd767-57a7-4ae1-84b7-7048f9f874d6'
while datetime(year, month, day, 9, 25) < datetime.now() < datetime(year, month, day, 11, 30) or \
        datetime(year, month, day, 13, 0) < datetime.now() < datetime(year, month, day, 18, 0):

    d = json.loads(requests.post(url, ).content.decode())['Data']
    message = "当前时间为:{},".format(str(datetime.now())[:19])
    for i in d:
        cjsj = i['Cjsj'][:2] + ":" + i['Cjsj'][2:4]
        if i['Mmsm'] == '证券买入' and str(datetime.now())[11:16] == cjsj:
            message += '本次买入的股票代码是:{},买入的时间是:{}。 '.format(i['Zqdm'], cjsj)
        elif i['Mmsm'] == '证券卖出' and str(datetime.now())[11:16] == cjsj:
            message += '本次卖出的股票代码是:{},卖出的时间是:{}。 '.format(i['Zqdm'], cjsj)
    if len(message) > 30:
        print(message)
        try_count = 0 
        while True:
            try_count += 1 
            try:
                msg = {
                    "msgtype": "text",
                    "text": {"content": 'Msg:交易信号:' + message + '\n' + datetime.now().strftime("%m-%d %H:%M:%S")}}
                headers = {"Content-Type": "application/json;charset=utf-8"}
                url = 'https://oapi.dingtalk.com/robot/send?access_token='
                body = json.dumps(msg)
                requests.post(url, data=body, headers=headers)
                print('钉钉已发送')
                break 
            except Exception as e:
                if try_count:
                    print("发送钉钉失败:", e)
                    break 
                else:
                    print("发送钉钉报错,重试:", e)