This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMB.py
107 lines (77 loc) · 2.77 KB
/
CMB.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import datetime
import redis
import requests
REDIS_HOST = "{{REDIS_HOST}}"
REDIS_PORT = "{{REDIS_PORT}}"
REDIS_PASSWORD = "{{REDIS_PASSWORD}}"
SERVERCHAN_TOKEN = "{{SERVERCHAN_TOKEN}}"
WXPUSHER_TOKEN = "{{WXPUSHER_TOKEN}}"
pool = redis.ConnectionPool(host=REDIS_HOST,
port=REDIS_PORT,
password=REDIS_PASSWORD)
def get_time(origin_date_str):
local_date = datetime.datetime.strptime(origin_date_str, "%Y-%m-%dT%H:%M:%S.%fZ") \
+ datetime.timedelta(hours=8)
return datetime.datetime.strftime(local_date, '%Y-%m-%d %H:%M:%S')
def set_id(id):
re = redis.StrictRedis(connection_pool=pool,decode_responses=True)
return re.set('CMB_ID', id)
def get_id():
re = redis.StrictRedis(connection_pool=pool,decode_responses=True)
re.setnx('CMB_ID', 0)
return int(re.get('CMB_ID'))
def set_json(data):
re = redis.StrictRedis(connection_pool=pool,decode_responses=True)
return re.set('CMB_JSON', str(data))
def send_msg_wxpusher(msg):
if msg == "":
return
body = requests.get(url="http://wxpusher.zjiecode.com/api/fun/wxuser"
"?appToken="+WXPUSHER_TOKEN+"&page=1&pageSize=50")
data = {
"appToken": WXPUSHER_TOKEN,
"content": msg,
"contentType": 3,
"uids": []
}
for user in body.json()['data']['records']:
data["uids"].append(user['uid'])
body = requests.post(url="http://wxpusher.zjiecode.com/api/send/message",
json=data)
return body.json()
def send_msg_serverchan(title, msg):
if title == "" or msg == "":
return
requests.post(url="https://sc.ftqq.com/"+SERVERCHAN_TOKEN+".send",
data={"text": title, "desp": msg})
return
def fetch():
headers = {
'Host': 'cmbgold-api-gateway.paas.cmbchina.com',
'encryptData': 'a80c35dd80944c723880a6b17178b0cb',
'User-Agent': 'CMBGold/38 CFNetwork/1125.2 Darwin/19.4.0',
'Connection': 'keep-alive',
'Accept': '*/*',
'Accept-Language': 'zh-tw',
'timeStamp': '1583344609',
'Cache-Control': 'no-cache',
'Accept-Encoding': 'gzip, deflate, br'
}
body = requests.get(url='https://cmbgold-api-gateway.paas.cmbchina.com/api/news/short'
, headers=headers)
return body.json()
json = fetch()
id = get_id()
msg = ""
title = ""
for data in json["data"]:
if get_id() < int(data['id']):
set_id(int(data['id']))
title = "招银汇金推送,id:" + data['id']
if id < int(data['id']):
msg += (get_time(data["time"]) + " " + data["content"] + '\n\n')
if WXPUSHER_TOKEN != "":
send_msg_wxpusher(msg)
if SERVERCHAN_TOKEN != "":
send_msg_serverchan(title, msg)
set_json(json)