-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathapi.py
265 lines (235 loc) · 12.5 KB
/
api.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
import asyncio
import json
import logging
from collections.abc import AsyncIterator
from typing import Any
import httpx
from tenacity import after_log, retry, retry_if_exception_type, wait_fixed
from botli_dataclasses import API_Challenge_Reponse, Challenge_Request
from config import Config
from enums import Decline_Reason, Variant
logger = logging.getLogger(__name__)
BASIC_RETRY_CONDITIONS = {'retry': retry_if_exception_type(httpx.RequestError),
'wait': wait_fixed(5),
'after': after_log(logger, logging.DEBUG)}
JSON_RETRY_CONDITIONS = {'retry': retry_if_exception_type((httpx.RequestError, json.JSONDecodeError)),
'wait': wait_fixed(5),
'after': after_log(logger, logging.DEBUG)}
MOVE_RETRY_CONDITIONS = {'retry': retry_if_exception_type(httpx.HTTPError),
'wait': wait_fixed(1),
'after': after_log(logger, logging.DEBUG)}
class API:
def __init__(self, config: Config) -> None:
self.lichess_client = httpx.AsyncClient(base_url=config.url,
headers={'Authorization': f'Bearer {config.token}',
'User-Agent': f'BotLi/{config.version}'})
self.external_client = httpx.AsyncClient(headers={'User-Agent': f'BotLi/{config.version}'})
def set_user_agent(self, version: str, username: str) -> None:
self.lichess_client.headers.update({'User-Agent': f'BotLi/{version} user:{username}'})
self.external_client.headers.update({'User-Agent': f'BotLi/{version} user:{username}'})
@retry(**BASIC_RETRY_CONDITIONS)
async def abort_game(self, game_id: str) -> bool:
try:
response = await self.lichess_client.post(f'/api/bot/game/{game_id}/abort')
response.raise_for_status()
return True
except httpx.HTTPStatusError as e:
print(e)
return False
@retry(**BASIC_RETRY_CONDITIONS)
async def accept_challenge(self, challenge_id: str) -> bool:
try:
response = await self.lichess_client.post(f'/api/challenge/{challenge_id}/accept')
response.raise_for_status()
return True
except httpx.HTTPStatusError as e:
if not e.response.is_client_error:
print(e)
return False
@retry(**BASIC_RETRY_CONDITIONS)
async def cancel_challenge(self, challenge_id: str) -> bool:
try:
response = await self.lichess_client.post(f'/api/challenge/{challenge_id}/cancel')
response.raise_for_status()
return True
except httpx.HTTPStatusError as e:
print(e)
return False
async def create_challenge(self,
challenge_request: Challenge_Request
) -> AsyncIterator[API_Challenge_Reponse]:
try:
async with self.lichess_client.stream('POST', f'/api/challenge/{challenge_request.opponent_username}',
data={'rated': str(challenge_request.rated).lower(),
'clock.limit': challenge_request.initial_time,
'clock.increment': challenge_request.increment,
'color': challenge_request.color.value,
'variant': challenge_request.variant.value,
'keepAliveStream': 'true'},
timeout=60.0) as response:
if response.status_code == httpx.codes.TOO_MANY_REQUESTS:
yield API_Challenge_Reponse(has_reached_rate_limit=True)
return
async for line in response.aiter_lines():
if not line:
continue
data: dict[str, Any] = json.loads(line)
yield API_Challenge_Reponse(data.get('id', None),
data.get('done') == 'accepted',
data.get('error'),
data.get('done') == 'declined',
'clock.limit' in data,
'clock.increment' in data)
except (httpx.RequestError, json.JSONDecodeError) as e:
yield API_Challenge_Reponse(error=str(e))
@retry(**BASIC_RETRY_CONDITIONS)
async def decline_challenge(self, challenge_id: str, reason: Decline_Reason) -> bool:
try:
response = await self.lichess_client.post(f'/api/challenge/{challenge_id}/decline',
data={'reason': reason.value})
response.raise_for_status()
return True
except httpx.HTTPStatusError as e:
print(e)
return False
@retry(**JSON_RETRY_CONDITIONS)
async def get_account(self) -> dict[str, Any]:
response = await self.lichess_client.get('/api/account')
json_response = response.json()
if 'error' in json_response:
raise RuntimeError(f'Account error: {json_response["error"]}')
return json_response
async def get_chessdb_eval(self, fen: str, best_move: bool, timeout: int) -> dict[str, Any] | None:
try:
async with asyncio.timeout(timeout):
response = await self.external_client.get('http://www.chessdb.cn/cdb.php',
params={'action': 'querypv',
'board': fen,
'stable': int(best_move),
'json': 1},
timeout=None)
response.raise_for_status()
return response.json()
except (httpx.HTTPError, json.JSONDecodeError, TimeoutError) as e:
print(e)
async def get_cloud_eval(self, fen: str, variant: Variant, timeout: int) -> dict[str, Any] | None:
try:
async with asyncio.timeout(timeout):
response = await self.lichess_client.get('/api/cloud-eval', params={'fen': fen,
'variant': variant.value},
timeout=None)
return response.json()
except (httpx.HTTPError, json.JSONDecodeError, TimeoutError) as e:
print(e)
async def get_egtb(self, fen: str, variant: str, timeout: int) -> dict[str, Any] | None:
try:
async with asyncio.timeout(timeout):
response = await self.external_client.get(f'https://tablebase.lichess.ovh/{variant}',
params={'fen': fen},
timeout=None)
response.raise_for_status()
return response.json()
except (httpx.HTTPError, json.JSONDecodeError, TimeoutError) as e:
print(e)
async def get_event_stream(self) -> AsyncIterator[dict[str, Any]]:
while True:
try:
async with self.lichess_client.stream('GET', '/api/stream/event', timeout=9.0) as response:
async for line in response.aiter_lines():
if line:
yield json.loads(line)
except (httpx.RequestError, json.JSONDecodeError):
print('Event stream lost connection. Next connection attempt in 5 seconds.')
await asyncio.sleep(5.0)
async def get_game_stream(self, game_id: str) -> AsyncIterator[dict[str, Any]]:
while True:
try:
async with self.lichess_client.stream('GET',
f'/api/bot/game/stream/{game_id}',
timeout=9.0) as response:
async for line in response.aiter_lines():
yield json.loads(line) if line else {'type': 'ping'}
except (httpx.RequestError, json.JSONDecodeError):
print(f'Game stream {game_id} lost connection. Next connection attempt in 1 second.')
await asyncio.sleep(1.0)
async def get_online_bots_stream(self) -> AsyncIterator[dict[str, Any]]:
while True:
try:
async with self.lichess_client.stream('GET', '/api/bot/online', timeout=9.0) as response:
async for line in response.aiter_lines():
if line:
yield json.loads(line)
return
except (httpx.RequestError, json.JSONDecodeError):
await asyncio.sleep(5.0)
async def get_opening_explorer(self,
username: str,
fen: str,
variant: Variant,
color: str,
speeds: str,
timeout: int
) -> dict[str, Any] | None:
try:
async with asyncio.timeout(timeout):
async with self.external_client.stream('GET', 'https://explorer.lichess.ovh/player',
params={'player': username, 'variant': variant.value,
'fen': fen, 'color': color, 'speeds': speeds,
'modes': 'rated', 'recentGames': 0},
timeout=None) as response:
response.raise_for_status()
async for line in response.aiter_lines():
if line:
return json.loads(line)
except (httpx.HTTPError, json.JSONDecodeError, TimeoutError) as e:
print(e)
@retry(**JSON_RETRY_CONDITIONS)
async def get_token_scopes(self, token: str) -> str:
response = await self.lichess_client.post('/api/token/test', content=token)
return response.json()[token]['scopes']
@retry(**JSON_RETRY_CONDITIONS)
async def get_user_status(self, username: str) -> dict[str, Any]:
response = await self.lichess_client.get('/api/users/status', params={'ids': username})
return response.json()[0]
@retry(**BASIC_RETRY_CONDITIONS)
async def resign_game(self, game_id: str) -> bool:
try:
response = await self.lichess_client.post(f'/api/bot/game/{game_id}/resign')
response.raise_for_status()
return True
except httpx.HTTPStatusError as e:
print(e)
return False
async def send_chat_message(self, game_id: str, room: str, text: str) -> bool:
try:
response = await self.lichess_client.post(f'/api/bot/game/{game_id}/chat',
data={'room': room, 'text': text},
timeout=1.0)
response.raise_for_status()
return True
except httpx.HTTPError as e:
print(e)
return False
@retry(**MOVE_RETRY_CONDITIONS)
async def send_move(self, game_id: str, uci_move: str, offer_draw: bool) -> bool:
try:
response = await self.lichess_client.post(f'/api/bot/game/{game_id}/move/{uci_move}',
params={'offeringDraw': str(offer_draw).lower()},
timeout=1.0)
response.raise_for_status()
return True
except httpx.HTTPStatusError as e:
if e.response.is_server_error:
raise
if e.response.status_code != httpx.codes.BAD_REQUEST:
print(e)
return False
@retry(**BASIC_RETRY_CONDITIONS)
async def upgrade_account(self) -> bool:
try:
response = await self.lichess_client.post('/api/bot/account/upgrade')
response.raise_for_status()
return True
except httpx.HTTPStatusError as e:
print(e)
return False