我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第1篇文章,点击查看活动详情
前言
之前在欧易充值了几百块钱,想捞一波结果被套了,好几个月没有登录进去查看账户余额,今天登上去发现还是充值时的账户余额,并且错过了大半个月前的牛市,想写一个监控脚本来监控账户余额,一分钟请求一次,设置一个阈值,超过阈值就告诉我自己要去抛售了。
因为可能内容有一些敏感就不介绍那么多,直接对照文档开干。
申请API
一定要记住密码。写API的时候需要使用下面这个密码
创建API完成后会用到 ApiKey,ApiSecret,上面说的密码(下文中的Passphrase)
申请企业微信机器人
需要事先创建一个群组最少需要3个人,再添加机器人
然后点开详情就看到调用的webhook地址了:
编写代码
这个代码需要调用两个接口,一个是获取余额,欧易的验签还是有一点复杂,需要对时间进行格式化成一个特殊的格式,而且sign生成也有些复杂,我就把它封装了。第二个是企业微信发送消息比较简单
import base64
import datetime
import hmac
import requests
import json
from hashlib import sha256
APIKey = "*************************"
SecretKey = "***************************"
Passphrase= "************************"
threshold = 1000
def get_sign(data, key):
key = key.encode('utf-8')
message = data.encode('utf-8')
sign = base64.b64encode(hmac.new(key, message, digestmod=sha256).digest())
sign = str(sign, 'utf-8')
return sign
# 通知机器人
def send_msg(msg):
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=*************************&debug=1'
datas = {
"msgtype": "text",
"text": {"content": msg}
}
headers = {'Content-Type': 'application/json'}
request = requests.post(url=url, headers=headers, data=json.dumps(datas))
print(request.text)
timestamp = datetime.datetime.utcnow().isoformat("T", "milliseconds") + "Z"
data = timestamp + 'GET' + '/api/v5/asset/asset-valuation?ccy=CNY'
sign = get_sign(data, SecretKey)
headers = {
"OK-ACCESS-KEY": APIKey,
"OK-ACCESS-SIGN": sign,
"OK-ACCESS-TIMESTAMP": timestamp,
"OK-ACCESS-PASSPHRASE": Passphrase,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
}
url = 'https://www.okx.com/api/v5/asset/asset-valuation'
params = {
"ccy": "CNY"
}
request = requests.get(url=url, headers=headers, params=params)
request.close()
content = request.json()
if content['code'] !='0':
print(content)
send_msg('API错误,请检查')
account_balance = content['data'][0]['details']['funding']
if float(account_balance) > threshold:
send_msg('当前账户余额为%s元,请尽快卖出!'%account_balance)
运行效果:
部署
之前做过一个天气监控,可以参考之前的教程:夏天来了 ,给自己做一个高温,下雨预警微信定时任务提醒~
需要注意:需要安装requests包依赖,运行的服务器能访问欧易的网站
把代码放在指定目录,使用chmod 755添加执行权限,进入宝塔添加计划任务,每分钟执行一次:
任务完成,现在就可以及时通知我卖出啦,哈哈,方便简单有用!