0%

github | github action 监控币种价格

朋友托我搞一个 zks 价格监控,用来套利,下面说一下具体的步骤。

这里默认你对 github action 的基础知识已经非常了解了,所以,看代码应该是没问题。


参考资料



发邮件原理


这里是把 163 邮箱作为一个中转站,然后发送到目标邮箱,首先注册一个 163 邮箱。

然后点击设置 -> POP3/SMTP/IMAP

新增授权码,然后把这个码复制下来。

下面说一下需要的参数。

  • secrets.MAIL_USERNAME
    • 163 邮箱
  • secrets.MAIL_PASSWORD
    • 上面申请的码
  • secrets.RECEIVER
    • 目标邮箱「随便」

通过开源轮子来发送邮件


这个方法比较优雅,但是,也有缺点,如果一天执行一次或者没有判断条件的可以用这个方法。

先说一下结构目录

  • .github
    • workflows
      • zksrobot.yaml
  • main.py
  • main.sh
  • requirements.txt

requirements.txt

1
requests

zksrobot.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
name: 'GitHub ZKS Robot'

on:
schedule:
- cron: '*/6 * * * *'

jobs:
zks-emailbot:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: 'Set up Python'
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: 'Install requirements'
run: pip install -r ./requirements.txt
- name: 'Working'
run: bash ./main.sh
- name: 'Send mail'
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.163.com
server_port: 465
username: ${{ secrets.MAIL_USERNAME }}
password: ${{ secrets.MAIL_PASSWORD }}
subject: CSDN Report
body: file://email.txt
to: ${{ secrets.RECEIVER }}
from: GitHub Actions

main.sh

1
python ./main.py ${{secrets.MAIL_USERNAME}} ${{secrets.MAIL_PASSWORD}} ${{secrets.RECEIVER}}

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests
import json

url_tokens = 'https://api.zks.app/1/tokens'
url_price = 'https://api.zks.app/1/tokens/price'


def get_token():
data = requests.get(url_tokens)
return json.loads(data.text)['data']


def get_price(result):
# id_symbols = {}
# # id 对应 symbol
# for r in result:
# id_symbols[r['id']] = r['symbol']
id = 1
for r in result:
if r['symbol'] == "ZKS":
id = r['id']
data = json.loads(requests.get(url_price).text)['data']
for d in data:
if d['id'] == id:
return d['price']


def saveText(path, info):
with open(path, 'w', encoding="utf-8") as f:
f.writelines(info)


if __name__ == '__main__':
result = get_token()
price = get_price(result)
email_path = "email.txt"
saveText(email_path, price)

ps: 这里用的开源轮子是

注意一点是,这个开源项目有的时候会更新,导致上面的代码不能用,所以,要跟进项目更新。


使用 python 做邮件转发


虽然上面可以进行邮箱转发,也很优雅,但是,有一个致命的缺点,就是不能判断。

比如,我们想价格达到套利条件的时候,再发送邮件。上面的是没办法做到的,所以,我们需要使用 python 来写发送邮件的逻辑。

这里直接给代码了。

文件目录如下

  • .github
    • workflows
      • zksrobot.yaml
  • main.py
  • requirements.txt

requirements.txt

1
requests

zksrobot.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
name: 'GitHub ZKS Robot'

on:
push:
branches:
- master
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '*/6 * * * *'

jobs:
zks-emailbot:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: 'Set up Python'
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: 'Install requirements'
run: pip install -r ./requirements.txt
- name: 'Working'
run: |
sprice=1.5
bprice=1.8
python ./main.py ${{ secrets.MAIL_USERNAME }} ${{ secrets.MAIL_PASSWORD }} ${{ secrets.RECEIVER }} $sprice $bprice

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
import smtplib
import sys
from email.mime.text import MIMEText

import requests

url_tokens = 'https://api.zks.app/1/tokens'
url_price = 'https://api.zks.app/1/tokens/price'

From = sys.argv[1]
PW = sys.argv[2]
To = sys.argv[3]
s_price = float(sys.argv[4])
b_price = float(sys.argv[5])


def get_token():
data = requests.get(url_tokens)
return json.loads(data.text)['data']


def get_price(result):
id = 1
for r in result:
if r['symbol'] == "ZKS":
id = r['id']
data = json.loads(requests.get(url_price).text)['data']
for d in data:
if d['id'] == id:
return d['price']


def sendEmail(price):
if float(price) < s_price or float(price) > b_price:
smtpObj = smtplib.SMTP_SSL('smtp.163.com', 465)
# 登录 SMTP 服务器
smtpObj.login(From, PW)
# 发送邮件
message = MIMEText(price, 'plain', 'utf-8')
message['From'] = From
message['To'] = To
message['Subject'] = "zks 价格"
smtpObj.sendmail(From, To, message.as_string())


if __name__ == '__main__':
result = get_token()
price = get_price(result)
sendEmail(price)

其他


  • github action 的定时任务并不会如期进行
    • 只是按时开始排队,根据平台资源的拥堵情况,可能等待几分钟或更久才被执行。
    • 我定时每 6 分钟执行一次,但是,一个小时只能执行 3 - 4
请我喝杯咖啡吧~