1
1
import requests
2
2
import os
3
3
from dotenv import load_dotenv
4
+ import datetime
5
+ import aiohttp
6
+ import logging
4
7
5
8
class APIClient :
6
9
def __init__ (self , base_url , api_token ):
@@ -11,28 +14,38 @@ def __init__(self, base_url, api_token):
11
14
"Connection" : "keep-alive" ,
12
15
"Content-Type" : "application/json"
13
16
})
17
+ self .api_version = "unknown"
14
18
15
19
def login (self , username , password ):
16
20
url = f'{ self .base_url } /api/login'
17
21
data = {'username' : username , 'password' : password }
18
22
response = self .session .post (url , json = data )
19
23
if response .status_code != 200 :
20
- return False
21
- return True
22
-
24
+ return None
25
+ return response . json ()
26
+
23
27
def is_logged_in (self ):
24
28
check_url = f"{ self .base_url } /api/is_logged_in"
25
29
response = self .session .get (check_url )
26
30
return response .json ().get ('result' , False )
27
31
28
- def do_perma_ban (self , player , steam_id_64 , reason , by ):
29
- ban_url = f"{ self .base_url } /api/do_perma_ban"
30
- payload = {
31
- 'player' : player ,
32
- 'steam_id_64' : steam_id_64 ,
33
- 'reason' : reason ,
34
- 'by' : by
35
- }
32
+ def do_perma_ban (self , player_name , player_id , reason , by ):
33
+ if self .api_version .startswith ("v10" ):
34
+ ban_url = f"{ self .base_url } /api/perma_ban"
35
+ payload = {
36
+ 'player_name' : player_name ,
37
+ 'player_id' : player_id ,
38
+ 'reason' : reason ,
39
+ 'by' : by
40
+ }
41
+ else :
42
+ ban_url = f"{ self .base_url } /api/do_perma_ban"
43
+ payload = {
44
+ 'player' : player_name ,
45
+ 'steam_id_64' : player_id ,
46
+ 'reason' : reason ,
47
+ 'by' : by
48
+ }
36
49
try :
37
50
response = self .session .post (ban_url , json = payload )
38
51
print (f"do_perma_ban response: { response .status_code } , { response .text } " )
@@ -41,15 +54,25 @@ def do_perma_ban(self, player, steam_id_64, reason, by):
41
54
print (f"Fehler beim Aufrufen von do_perma_ban: { e } " )
42
55
return False
43
56
44
- def do_temp_ban (self , player , steam_id_64 , duration_hours , reason , by ):
45
- temp_ban_url = f"{ self .base_url } /api/do_temp_ban"
46
- payload = {
47
- 'player' : player ,
48
- 'steam_id_64' : steam_id_64 ,
49
- 'duration_hours' : duration_hours ,
50
- 'reason' : reason ,
51
- 'by' : by
52
- }
57
+ def do_temp_ban (self , player_name , player_id , duration_hours , reason , by ):
58
+ if self .api_version .startswith ("v10" ):
59
+ temp_ban_url = f"{ self .base_url } /api/temp_ban"
60
+ payload = {
61
+ 'player_name' : player_name ,
62
+ 'player_id' : player_id ,
63
+ 'duration_hours' : duration_hours ,
64
+ 'reason' : reason ,
65
+ 'by' : by
66
+ }
67
+ else :
68
+ temp_ban_url = f"{ self .base_url } /api/do_temp_ban"
69
+ payload = {
70
+ 'player' : player_name ,
71
+ 'steam_id_64' : player_id ,
72
+ 'duration_hours' : duration_hours ,
73
+ 'reason' : reason ,
74
+ 'by' : by
75
+ }
53
76
try :
54
77
response = self .session .post (temp_ban_url , json = payload )
55
78
print (f"do_temp_ban response: { response .status_code } , { response .text } " )
@@ -58,39 +81,82 @@ def do_temp_ban(self, player, steam_id_64, duration_hours, reason, by):
58
81
print (f"Fehler beim Aufrufen von do_temp_ban: { e } " )
59
82
return False
60
83
61
- def do_unban (self , steam_id ):
62
- unban_url = f"{ self .base_url } /api/do_unban"
63
- response = self .session .post (unban_url , json = {'steam_id_64' : steam_id })
64
- return response .ok
84
+ def do_unban (self , player_id ):
85
+ if self .api_version .startswith ("v10" ):
86
+ unban_url = f"{ self .base_url } /api/unban"
87
+ payload = {'player_id' : player_id }
88
+ else :
89
+ unban_url = f"{ self .base_url } /api/do_unban"
90
+ payload = {'steam_id_64' : player_id }
91
+ try :
92
+ response = self .session .post (unban_url , json = payload )
93
+ return response .ok
94
+ except Exception as e :
95
+ print (f"Fehler beim Aufrufen von do_unban: { e } " )
96
+ return False
65
97
66
- def unblacklist_player (self , steam_id ):
67
- unban_url = f"{ self .base_url } /api/unblacklist_player"
68
- response = self .session .post (unban_url , json = {'steam_id_64' : steam_id })
69
- return response .ok
98
+ def unblacklist_player (self , player_id ):
99
+ unblacklist_url = f"{ self .base_url } /api/unblacklist_player"
100
+ payload = {'player_id' : player_id }
101
+ try :
102
+ response = self .session .post (unblacklist_url , json = payload )
103
+ return response .ok
104
+ except Exception as e :
105
+ print (f"Fehler beim Aufrufen von unblacklist_player: { e } " )
106
+ return False
70
107
71
- def do_blacklist_player (self , steam_id_64 , name , reason , by ):
72
- blacklist_url = f"{ self .base_url } /api/blacklist_player"
73
- payload = {
74
- 'steam_id_64' : steam_id_64 ,
75
- 'name' : name ,
76
- 'reason' : reason ,
77
- 'by' : by
78
- }
108
+ def do_blacklist_player (self , player_id , player_name , reason , by ):
109
+ if self .api_version .startswith ("v10" ):
110
+ blacklist_url = f"{ self .base_url } /api/add_blacklist_record"
111
+ payload = {
112
+ 'player_id' : player_id ,
113
+ 'reason' : reason ,
114
+ 'admin_name' : by ,
115
+ 'blacklist_id' : 0 , # Default blacklist
116
+ 'sync' : 'kick_only' ,
117
+ 'servers' : None
118
+ }
119
+ else :
120
+ blacklist_url = f"{ self .base_url } /api/blacklist_player"
121
+ payload = {
122
+ 'steam_id_64' : player_id ,
123
+ 'name' : player_name ,
124
+ 'reason' : reason ,
125
+ 'by' : by
126
+ }
79
127
try :
80
128
response = self .session .post (blacklist_url , json = payload )
81
129
print (f"do_blacklist_player response: { response .status_code } , { response .text } " )
82
- return response .ok
130
+ if response .status_code == 200 :
131
+ response_data = response .json ()
132
+ if not response_data .get ("failed" , False ):
133
+ return True
134
+ else :
135
+ logging .error (f"Fehler beim Blacklisting: { response_data } " )
136
+ return False
137
+ return False
83
138
except Exception as e :
84
139
print (f"Fehler beim Aufrufen von do_blacklist_player: { e } " )
140
+ logging .error (f"Fehler beim Aufrufen von do_blacklist_player: { e } " )
85
141
return False
86
142
87
- def do_watch_player (self , player , steam_id_64 , reason ):
88
- watchlist_url = f"{ self .base_url } /api/do_watch_player"
89
- payload = {
90
- 'player' : player ,
91
- 'steam_id_64' : steam_id_64 ,
92
- 'reason' : reason
93
- }
143
+
144
+ def do_watch_player (self , player_name , player_id , reason , by ):
145
+ if self .api_version .startswith ("v10" ):
146
+ watchlist_url = f"{ self .base_url } /api/watch_player"
147
+ payload = {
148
+ 'player_id' : player_id ,
149
+ 'reason' : reason ,
150
+ 'by' : by ,
151
+ 'player_name' : player_name # Hinzufügen des player_name Feldes
152
+ }
153
+ else :
154
+ watchlist_url = f"{ self .base_url } /api/do_watch_player"
155
+ payload = {
156
+ 'player' : player_name ,
157
+ 'steam_id_64' : player_id ,
158
+ 'reason' : reason
159
+ }
94
160
try :
95
161
response = self .session .post (watchlist_url , json = payload )
96
162
print (f"do_watch_player response: { response .status_code } , { response .text } " )
@@ -99,12 +165,19 @@ def do_watch_player(self, player, steam_id_64, reason):
99
165
print (f"Fehler beim Aufrufen von do_watch_player: { e } " )
100
166
return False
101
167
102
- def do_unwatch_player (self , player , steam_id_64 ):
103
- unwatch_url = f"{ self .base_url } /api/do_unwatch_player"
104
- payload = {
105
- 'player' : player ,
106
- 'steam_id_64' : steam_id_64
107
- }
168
+ def do_unwatch_player (self , player_name , player_id ):
169
+ if self .api_version .startswith ("v10" ):
170
+ unwatch_url = f"{ self .base_url } /api/unwatch_player"
171
+ payload = {
172
+ 'player_id' : player_id ,
173
+ 'player_name' : player_name # Hinzufügen des player_name Feldes
174
+ }
175
+ else :
176
+ unwatch_url = f"{ self .base_url } /api/do_unwatch_player"
177
+ payload = {
178
+ 'player' : player_name ,
179
+ 'steam_id_64' : player_id
180
+ }
108
181
try :
109
182
response = self .session .post (unwatch_url , json = payload )
110
183
print (f"do_unwatch_player response: { response .status_code } , { response .text } " )
@@ -113,12 +186,20 @@ def do_unwatch_player(self, player, steam_id_64):
113
186
print (f"Fehler beim Aufrufen von do_unwatch_player: { e } " )
114
187
return False
115
188
116
- def post_player_comment (self , steam_id , comment ):
117
- post_comment_url = f"{ self .base_url } /api/post_player_comment"
118
- payload = {
119
- 'steam_id_64' : steam_id ,
120
- 'comment' : comment # Hier ändern wir message auf comment
121
- }
189
+ def post_player_comment (self , player_id , comment ):
190
+ if self .api_version .startswith ("v10" ):
191
+ post_comment_url = f"{ self .base_url } /api/post_player_comment"
192
+ payload = {
193
+ 'player_id' : player_id ,
194
+ 'comment' : comment
195
+ }
196
+ else :
197
+ post_comment_url = f"{ self .base_url } /api/post_player_comment"
198
+ payload = {
199
+ 'steam_id_64' : player_id ,
200
+ 'comment' : comment
201
+ }
202
+
122
203
try :
123
204
response = self .session .post (post_comment_url , json = payload )
124
205
print (f"post_player_comment response: { response .status_code } , { response .text } " )
@@ -132,4 +213,21 @@ def post_player_comment(self, steam_id, comment):
132
213
return False
133
214
except Exception as e :
134
215
print (f"Fehler beim Aufrufen von post_player_comment: { e } " )
135
- return False
216
+ return False
217
+
218
+
219
+ async def report_api_version (client_id , version ):
220
+ url = "https://api.1bv.eu/update_client_version" # Überprüfen Sie diese URL
221
+ data = {
222
+ 'client_id' : client_id ,
223
+ 'api_version' : version
224
+ }
225
+ async with aiohttp .ClientSession () as session :
226
+ async with session .post (url , json = data ) as response :
227
+ response_text = await response .text ()
228
+ if response .status == 200 :
229
+ logging .info (f"API-Version { version } erfolgreich gemeldet für Client { client_id } " )
230
+ else :
231
+ logging .error (f"Fehler beim Melden der API-Version { version } für Client { client_id } " )
232
+ logging .error (f"Antwort vom Server: { response_text } " )
233
+
0 commit comments