-
Notifications
You must be signed in to change notification settings - Fork 3
/
gzbot.py
313 lines (291 loc) · 13.6 KB
/
gzbot.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import asyncio
import requests
import urllib3
import argparse
import json, time, sys
from datetime import datetime, timezone, timedelta
urllib3.disable_warnings()
URL = ""
GROUP_NOTICE_ID = 0
GROUP_EVENTS_ID = 0
MATCH_ID = 0
CQ_PORT = 0
# Total Announcement Lens
noticeLen = 0
# Match Announcement Lens
normalListLen = 0
# New Challenge Lens
newchallengeListLen = 0
# New Hint Lens
newhintLen = 0
# First Blood Lens
firstbloodLen = 0
# Second Blood Lens
secondbloodLen = 0
# Third Blood Lens
thirdbloodLen = 0
noticeList = []
def processTime(t):
t_truncated = t[:26] + t[26:].split('+')[0]
input_time = datetime.fromisoformat(t_truncated)
input_time_utc = input_time.replace(tzinfo=timezone.utc)
beijing_timezone = timezone(timedelta(hours=8))
beijing_time = input_time_utc.astimezone(beijing_timezone)
return beijing_time.strftime("%Y-%m-%d %H:%M:%S")
def getNotice(URL, MATCH_ID):
request = requests.session()
dic = {
'platform-notice': URL + '/api/game/{0}/notices'.format(MATCH_ID),
'platform-events': URL + '/api/game/{0}/events?hideContainer=false&count=2&skip=0'.format(MATCH_ID)
}
try:
res = request.get(dic['platform-notice'], verify=False)
if res.text == '[]':
sys.exit()
allList = json.loads(res.text)
return allList
except:
sys.exit("\033[31m[%s] [ERROR]: Please add a new challenge and use it! :(\033[0m" % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))))
# Match Announcement Information
def getNormalInfo(URL, MATCH_ID):
global noticeLen
tmpList = getNotice(URL, MATCH_ID)
tmpListLen = len(tmpList)
if (noticeLen < tmpListLen):
interval = tmpListLen - noticeLen
normal_list = []
for i in range(0, interval):
if (tmpList[i]['type'] == 'Normal'):
notices = {
"id": tmpList[i]['id'],
"time": tmpList[i]['time'],
"content": tmpList[i]['content']
}
normal_list.append(notices)
sorted(normal_list, key=lambda notices: notices['id'])
return normal_list
# New Challenge Information
def getNewChallengeInfo(URL, MATCH_ID):
global noticeLen
tmpList = getNotice(URL, MATCH_ID)
tmpListLen = len(tmpList)
if (noticeLen < tmpListLen):
interval = tmpListLen - noticeLen
newchallenge_list = []
for i in range(0, interval):
if (tmpList[i]['type'] == 'NewChallenge'):
notices = {
"id": tmpList[i]['id'],
"time": tmpList[i]['time'],
"content": tmpList[i]['content']
}
newchallenge_list.append(notices)
sorted(newchallenge_list, key=lambda notices: notices['id'])
return newchallenge_list
# New Hint Information
def getNewHintInfo(URL, MATCH_ID):
global noticeLen
tmpList = getNotice(URL, MATCH_ID)
tmpListLen = len(tmpList)
if (noticeLen < tmpListLen):
interval = tmpListLen - noticeLen
newhint_list = []
for i in range(0, interval):
if (tmpList[i]['type'] == 'NewHint'):
notices = {
"id": tmpList[i]['id'],
"time": tmpList[i]['time'],
"content": tmpList[i]['content']
}
newhint_list.append(notices)
sorted(newhint_list, key=lambda notices: notices['id'])
return newhint_list
# First Blood Information
def getFirstBloodInfo(URL, MATCH_ID):
global noticeLen
tmpList = getNotice(URL, MATCH_ID)
tmpListLen = len(tmpList)
if (noticeLen < tmpListLen):
interval = tmpListLen - noticeLen
firstblood_list = []
for i in range(0, interval):
if (tmpList[i]['type'] == 'FirstBlood'):
notices = {
"id": tmpList[i]['id'],
"time": tmpList[i]['time'],
"content": tmpList[i]['content']
}
firstblood_list.append(notices)
sorted(firstblood_list, key=lambda notices: notices['id'])
return firstblood_list
# Second Blood Information
def getSecondBloodInfo(URL, MATCH_ID):
global noticeLen
tmpList = getNotice(URL, MATCH_ID)
tmpListLen = len(tmpList)
if (noticeLen < tmpListLen):
interval = tmpListLen - noticeLen
secondblood_list = []
for i in range(0, interval):
if (tmpList[i]['type'] == 'SecondBlood'):
notices = {
"id": tmpList[i]['id'],
"time": tmpList[i]['time'],
"content": tmpList[i]['content']
}
secondblood_list.append(notices)
sorted(secondblood_list, key=lambda notices: notices['id'])
return secondblood_list
# Third Blood Information
def getThirdBloodInfo(URL, MATCH_ID):
global noticeLen
tmpList = getNotice(URL, MATCH_ID)
tmpListLen = len(tmpList)
if (noticeLen < tmpListLen):
interval = tmpListLen - noticeLen
thirdblood_list = []
for i in range(0, interval):
if (tmpList[i]['type'] == 'ThirdBlood'):
notices = {
"id": tmpList[i]['id'],
"time": tmpList[i]['time'],
"content": tmpList[i]['content']
}
thirdblood_list.append(notices)
sorted(thirdblood_list, key=lambda notices: notices['id'])
return thirdblood_list
# Sending Message
def sendMessage(msg, CQ_PORT, GROUP_NOTICE_ID):
try:
request = requests.Session()
r = request.get("http://127.0.0.1:%s/send_group_msg?group_id=%s&message=%s"
% (
str(CQ_PORT),
str(GROUP_NOTICE_ID),
str(msg))
)
print('\033[32m[%s] [SEND] Sending message to %s\033[0m' % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))), GROUP_NOTICE_ID))
except requests.exceptions.ConnectionError as e:
sys.exit("\033[31m[%s] [ERROR] Error sending message to %s, Connection error possible port or address error\033[0m" % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))), GROUP_NOTICE_ID))
async def sendMessageNotice(URL, MATCH_ID, CQ_PORT, GROUP_NOTICE_ID):
global normalListLen, newchallengeListLen, newhintLen, firstbloodLen, secondbloodLen, thirdbloodLen
try:
while True:
message = ""
# Normal List
tmpNormalList = getNormalInfo(URL, MATCH_ID)
tmpNormalListLen = len(getNormalInfo(URL, MATCH_ID))
# New Challenge
tmpNewChallengeInfo = getNewChallengeInfo(URL, MATCH_ID)
tmpNewChallengeInfoLen = len(getNewChallengeInfo(URL, MATCH_ID))
# New Hint
tmpNewHintInfo = getNewHintInfo(URL, MATCH_ID)
tmpNewHintInfoLen = len(getNewHintInfo(URL, MATCH_ID))
# First Blood
tmpFirstBloodInfo = getFirstBloodInfo(URL, MATCH_ID)
tmpFirstBloodInfoLen = len(getFirstBloodInfo(URL, MATCH_ID))
# Second Blood
tmpSecondBloodInfo = getSecondBloodInfo(URL, MATCH_ID)
tmpSecondBloodInfoLen = len(getSecondBloodInfo(URL, MATCH_ID))
# Third Blood
tmpThirdBloodInfo = getThirdBloodInfo(URL, MATCH_ID)
tmpThirdBloodInfoLen = len(getThirdBloodInfo(URL, MATCH_ID))
print('\033[33m[%s] [INFO]: Waiting data...\033[0m' % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))))
if (normalListLen < tmpNormalListLen):
message = "【比赛公告】\n内容:%s\n时间:%s" % (tmpNormalList[0]['content'], processTime(tmpNormalList[0]['time']))
print("\033[32m[%s] [ANNOUNCEMENTS] Data on receipt of Announcements %s\033[0m" % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))), tmpNormalList[0]['content']))
sendMessage(msg=message, CQ_PORT=CQ_PORT, GROUP_NOTICE_ID=GROUP_NOTICE_ID)
normalListLen = tmpNormalListLen
else:
normalListLen = tmpNormalListLen
if (newchallengeListLen < tmpNewChallengeInfoLen):
message = "【新增题目】\n%s\n时间:%s" % (tmpNewChallengeInfo[0]['content'], processTime(tmpNewChallengeInfo[0]['time']))
print("\033[32m[%s] [NEW CHALLENGE] Data on receipt of New Challenge %s\033[0m" % (str(), tmpNewChallengeInfo[0]['content']))
sendMessage(msg=message, CQ_PORT=CQ_PORT, GROUP_NOTICE_ID=GROUP_NOTICE_ID)
newchallengeListLen = tmpNewChallengeInfoLen
else:
newchallengeListLen = tmpNewChallengeInfoLen
if (newhintLen < tmpNewHintInfoLen):
message = "【题目提示】\n%s\n时间:%s" % (tmpNewHintInfo[0]['content'], processTime(tmpNewHintInfo[0]['time']))
print("\033[32m[%s] [NEW HINT] Data on receipt of New Hint %s\033[0m" % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))), tmpNewHintInfo[0]['content']))
sendMessage(msg=message, CQ_PORT=CQ_PORT, GROUP_NOTICE_ID=GROUP_NOTICE_ID)
newhintLen = tmpNewHintInfoLen
else:
newhintLen = tmpNewHintInfoLen
if (firstbloodLen < tmpFirstBloodInfoLen):
message = "【一血播报】\n%s\n时间:%s" % (tmpFirstBloodInfo[0]['content'], processTime(tmpFirstBloodInfo[0]['time']))
print("\033[32m[%s] [FIRST BLOOD] Data on receipt of First Blood %s\033[0m" % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))), tmpFirstBloodInfo[0]['content']))
sendMessage(msg=message, CQ_PORT=CQ_PORT, GROUP_NOTICE_ID=GROUP_NOTICE_ID)
firstbloodLen = tmpFirstBloodInfoLen
else:
firstbloodLen = tmpFirstBloodInfoLen
if (secondbloodLen < tmpSecondBloodInfoLen):
message = "【二血播报】\n%s\n时间:%s" % (tmpSecondBloodInfo[0]['content'], processTime(tmpSecondBloodInfo[0]['time']))
print("\033[32m[%s] [SECOND BLOOD] Data on receipt of First Blood %s\033[0m" % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))), tmpSecondBloodInfo[0]['content']))
sendMessage(msg=message, CQ_PORT=CQ_PORT, GROUP_NOTICE_ID=GROUP_NOTICE_ID)
secondbloodLen = tmpSecondBloodInfoLen
else:
secondbloodLen = tmpSecondBloodInfoLen
if (thirdbloodLen < tmpThirdBloodInfoLen):
message = "【三血播报】\n%s\n时间:%s" % (tmpThirdBloodInfo[0]['content'], processTime(tmpThirdBloodInfo[0]['time']))
print("\033[32m[%s] [THIRD BLOOD] Data on receipt of First Blood %s\033[0m" % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))), tmpThirdBloodInfo[0]['content']))
sendMessage(msg=message, CQ_PORT=CQ_PORT, GROUP_NOTICE_ID=GROUP_NOTICE_ID)
thirdbloodLen = tmpThirdBloodInfoLen
else:
thirdbloodLen = tmpThirdBloodInfoLen
await asyncio.sleep(3)
except KeyboardInterrupt as e:
print('\033[31m[%s] [ERROR]: Trying to exit !!!\033[0m' % (str(processTime(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))))
async def runner():
tasks = [
asyncio.create_task(sendMessageNotice(
URL=URL,
GROUP_NOTICE_ID=GROUP_NOTICE_ID,
MATCH_ID=MATCH_ID,
CQ_PORT=CQ_PORT
)),
]
normalListLen = len(getNormalInfo(URL=URL, MATCH_ID=MATCH_ID))
newchallengeListLen = len(getNewChallengeInfo(URL=URL, MATCH_ID=MATCH_ID))
newhintLen = len(getNewHintInfo(URL=URL, MATCH_ID=MATCH_ID))
firstbloodLen = len(getFirstBloodInfo(URL=URL, MATCH_ID=MATCH_ID))
secondbloodLen = len(getSecondBloodInfo(URL=URL, MATCH_ID=MATCH_ID))
thirdbloodLen = len(getThirdBloodInfo(URL=URL, MATCH_ID=MATCH_ID))
await asyncio.gather(*tasks)
def main():
global URL, GROUP_NOTICE_ID, MATCH_ID, CQ_PORT
BANNER = """\033[01;34m\
_____ ______ _____ ___________ ______ _
| __ \|___ /_ _/ __ \_ _| ___| | ___ \ | |
| | \/ / /(_|_) / \/ | | | |_ | |_/ / ___ | |_
| | __ / / | | | | | _| | ___ \/ _ \| __|
| |_\ \./ /____ _| \__/\ | | | | | |_/ / (_) | |_ \033[0m\033[4;37m%s\033[0m
\____/\_____(_|_)\____/ \_/ \_| \____/ \___/ \__| \033[0m\033[4;37m%s\033[0m\n
""" % ("Author: IceCliffs", "Version: v0.0.2")
print(BANNER)
parser = argparse.ArgumentParser(description="GZ::CTF QQ Bot")
parser.add_argument('--url',
required=True,
help="platform url")
parser.add_argument('--notice',
required=True,
help="qq notice Group")
parser.add_argument('--id',
required=True,
help="race id")
parser.add_argument('--port',
required=True,
help="cq port")
parser.add_argument('--events',
help="qq detail group (optional)")
parser.add_argument('--cookie',
help="administrator cookie")
args = parser.parse_args()
if args.url and args.notice and args.id and args.port:
URL = args.url
GROUP_NOTICE_ID = args.notice
MATCH_ID = args.id
CQ_PORT = args.port
asyncio.run(runner())
if __name__ == "__main__":
main()