forked from greenhaha/dota2_csgo_watcher_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSGO.py
169 lines (144 loc) · 6.01 KB
/
CSGO.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import hashlib
import json
import random
from CSGO_dicts import *
import message_sender
from player import player
class CSGOHTTPError(Exception):
pass
def get_last_match_by_long_steamID(long_steamID):
url = 'https://api.wmpvp.com/api/v2/home/validUser?sign='
header = {
"Host": "api.wmpvp.com",
"Connection": "keep-alive",
"Accept": "application/json, text/plain, */*",
"Origin": "https://news.wmpvp.com",
"x-requested-with": "XMLHttpRequest",
"User-Agent": 'Mozilla/5.0 (Linux; Android 5.1.1; SM-G9750 Build/LMY49I; wv) AppleWebKit/537.36 (KHTML, '
'like Gecko) Version/4.0 Chrome/52.0.2743.100 Mobile Safari/537.36 EsportsApp Version=1.4.3.43',
"Content-Type": "application/json;charset=UTF-8",
"Referer": "https://news.wmpvp.com/csgo-matchList.html?id=" + str(long_steamID) + "&type=00",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,en-US;q=0.8",
}
salt = '1f3192e58723aed15b8e8a9dc8e760861f3192e58723aed15b8e8a9dc8e76086'
payload = '{"gameAbbr":"CSGO","steamId":"' + str(long_steamID) + \
'","accessToken":null,"lastTimeStamp":"","dataSource":0,"pageSize":1} '
sign = hashlib.md5((payload + salt).encode("utf-8")).hexdigest()
response = requests.post(url + sign, data=payload, headers=header)
if response.status_code >= 400:
if response.status_code == 401:
raise CSGOHTTPError("Unauthorized request 401. Verify API key.")
if response.status_code == 503:
raise CSGOHTTPError("The server is busy or you exceeded limits. Please wait 30s and try again.")
raise CSGOHTTPError("Failed to retrieve data: %s. URL: %s" % (response.status_code, url))
r = json.loads(response.content.decode("utf-8"))
try:
return r['data'][2]['data'][0]
except KeyError:
raise CSGOHTTPError("Response Error: Key Error")
except IndexError:
raise CSGOHTTPError("Response Error: Index Error")
# 接收某局比赛的玩家列表, 生成开黑战报
# 参数为玩家对象列表
def generate_party_message(player_list: [player]):
player_num = len(player_list)
# 队伍信息
team = player_list[0].csgo_team
win_team = player_list[0].csgo_win_team
if win_team == team:
win = True
else:
win = False
nicknames = ''
if player_num >= 3:
for i in range(player_num - 1):
nicknames += player_list[i].nickname
nicknames += ', '
else:
nicknames += player_list[0].nickname
nicknames += '和'
nicknames += player_list[player_num - 1].nickname
top_rating = 0
for i in player_list:
if i.rating > top_rating:
top_rating = i.rating
if (win and top_rating > 1) or (not win and top_rating > 1):
postive = True
elif (win and top_rating < 0.5) or (not win and top_rating < 0.5):
postive = False
else:
if random.randint(0, 1) == 0:
postive = True
else:
postive = False
print_str = ''
if win and postive:
print_str += random.choice(WIN_POSTIVE_PARTY).format(nicknames) + '\n'
elif win and not postive:
print_str += random.choice(WIN_NEGATIVE_PARTY).format(nicknames) + '\n'
elif not win and postive:
print_str += random.choice(LOSE_POSTIVE_PARTY).format(nicknames) + '\n'
else:
print_str += random.choice(LOSE_NEGATIVE_PARTY).format(nicknames) + '\n'
start_time = player_list[0].csgo_start_time
score1 = player_list[0].csgo_team_1_score
score2 = player_list[0].csgo_team_2_score
print_str += "开始时间: {}\n".format(start_time)
print_str += "比分: [{}/{}]\n".format(score1, score2 if team == 1 else score2, score1)
print_str += '游戏模式: [{}/{}]\n'.format(player_list[0].csgo_map, player_list[0].csgo_mode)
for i in player_list:
nickname = i.nickname
rating = i.csgo_rating
kills, deaths, assists = i.csgo_kill, i.csgo_death, i.csgo_assist
print_str += "{}KDA: [{}/{}/{}], rating: {}\n" \
.format(nickname, kills, deaths, assists, rating)
# print(print_str)
message_sender.message(print_str)
# 接收某局比赛的玩家信息, 生成单排战报
# 参数为玩家对象
def generate_solo_message(player_obj: player):
# 队伍信息
team = player_obj.csgo_team
win_team = player_obj.csgo_win_team
if win_team == team:
win = True
else:
win = False
nicknames = player_obj.nickname
if (win and player_obj.csgo_rating > 1) or (not win and player_obj.csgo_rating > 1):
postive = True
elif (win and player_obj.csgo_rating < 0.5) or (not win and player_obj.csgo_rating < 0.5):
postive = False
else:
if random.randint(0, 1) == 0:
postive = True
else:
postive = False
print_str = ''
if win and postive:
print_str += random.choice(WIN_POSTIVE_PARTY).format(nicknames) + '\n'
elif win and not postive:
print_str += random.choice(WIN_NEGATIVE_PARTY).format(nicknames) + '\n'
elif not win and postive:
print_str += random.choice(LOSE_POSTIVE_PARTY).format(nicknames) + '\n'
else:
print_str += random.choice(LOSE_NEGATIVE_PARTY).format(nicknames) + '\n'
start_time = player_obj.csgo_start_time
if team == 1:
score1 = player_obj.csgo_team_1_score
score2 = player_obj.csgo_team_2_score
else:
score1 = player_obj.csgo_team_2_score
score2 = player_obj.csgo_team_1_score
print_str += "开始时间: {}\n".format(start_time)
print_str += "比分: [{}:{}]\n".format(score1, score2)
print_str += '游戏模式: [{}/{}]\n'.format(player_obj.csgo_map, player_obj.csgo_mode)
print_str += "{}KDA: [{}/{}/{}], rating: {}\n"\
.format(player_obj.nickname, player_obj.csgo_kill,
player_obj.csgo_death, player_obj.csgo_assist, player_obj.csgo_rating)
# print(print_str)
message_sender.message(print_str)