-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathapi.py
270 lines (237 loc) · 13.2 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
266
267
268
269
270
import asyncio
import json
import logging
from collections.abc import AsyncIterator
from typing import Any
import aiohttp
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((aiohttp.ClientError, TimeoutError)),
'wait': wait_fixed(5.0),
'after': after_log(logger, logging.DEBUG)}
JSON_RETRY_CONDITIONS = {'retry': retry_if_exception_type((aiohttp.ClientError, json.JSONDecodeError, TimeoutError)),
'wait': wait_fixed(5.0),
'after': after_log(logger, logging.DEBUG)}
GAME_STREAM_RETRY_CONDITIONS = {'retry': retry_if_exception_type((aiohttp.ClientError,
json.JSONDecodeError,
TimeoutError)),
'wait': wait_fixed(1.0),
'after': after_log(logger, logging.DEBUG)}
MOVE_RETRY_CONDITIONS = {'retry': retry_if_exception_type((aiohttp.ClientError, TimeoutError)),
'wait': wait_fixed(1.0),
'after': after_log(logger, logging.DEBUG)}
class API:
def __init__(self, config: Config) -> None:
self.lichess_session = aiohttp.ClientSession(config.url, headers={'Authorization': f'Bearer {config.token}',
'User-Agent': f'BotLi/{config.version}'},
timeout=aiohttp.ClientTimeout(total=5.0))
self.external_session = aiohttp.ClientSession(headers={'User-Agent': f'BotLi/{config.version}'})
def append_user_agent(self, username: str) -> None:
self.lichess_session.headers['User-Agent'] += f' user:{username}'
self.external_session.headers['User-Agent'] += f' user:{username}'
async def close(self) -> None:
await self.lichess_session.close()
await self.external_session.close()
@retry(**BASIC_RETRY_CONDITIONS)
async def abort_game(self, game_id: str) -> bool:
try:
async with self.lichess_session.post(f'/api/bot/game/{game_id}/abort') as response:
response.raise_for_status()
return True
except aiohttp.ClientResponseError as e:
print(e)
return False
@retry(**BASIC_RETRY_CONDITIONS)
async def accept_challenge(self, challenge_id: str) -> bool:
try:
async with self.lichess_session.post(f'/api/challenge/{challenge_id}/accept') as response:
response.raise_for_status()
return True
except aiohttp.ClientResponseError as e:
if not 400 <= e.status <= 499:
print(e)
return False
@retry(**BASIC_RETRY_CONDITIONS)
async def cancel_challenge(self, challenge_id: str) -> bool:
try:
async with self.lichess_session.post(f'/api/challenge/{challenge_id}/cancel') as response:
response.raise_for_status()
return True
except aiohttp.ClientResponseError as e:
print(e)
return False
async def create_challenge(self,
challenge_request: Challenge_Request
) -> AsyncIterator[API_Challenge_Reponse]:
try:
async with self.lichess_session.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=aiohttp.ClientTimeout(sock_read=60.0)) as response:
if response.status == 429:
yield API_Challenge_Reponse(has_reached_rate_limit=True)
return
async for line in response.content:
if not line.strip():
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 (aiohttp.ClientError, json.JSONDecodeError) as e:
yield API_Challenge_Reponse(error=str(e))
except TimeoutError:
yield API_Challenge_Reponse(error='Connection timed out.')
@retry(**BASIC_RETRY_CONDITIONS)
async def decline_challenge(self, challenge_id: str, reason: Decline_Reason) -> bool:
try:
async with self.lichess_session.post(f'/api/challenge/{challenge_id}/decline',
data={'reason': reason.value}) as response:
response.raise_for_status()
return True
except aiohttp.ClientResponseError as e:
print(e)
return False
@retry(**JSON_RETRY_CONDITIONS)
async def get_account(self) -> dict[str, Any]:
async with self.lichess_session.get('/api/account') as response:
json_response = await 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, timeout: int) -> dict[str, Any] | None:
try:
async with self.external_session.get('http://www.chessdb.cn/cdb.php',
params={'action': 'queryall',
'board': fen,
'json': 1},
timeout=aiohttp.ClientTimeout(total=timeout)) as response:
response.raise_for_status()
return await response.json()
except (aiohttp.ClientError, json.JSONDecodeError) as e:
print(f'ChessDB: {e}')
except TimeoutError:
print(f'ChessDB: Timed out after {timeout} second(s).')
async def get_cloud_eval(self, fen: str, variant: Variant, timeout: int) -> dict[str, Any] | None:
try:
async with self.lichess_session.get('/api/cloud-eval', params={'fen': fen,
'variant': variant.value},
timeout=aiohttp.ClientTimeout(total=timeout)) as response:
response.raise_for_status()
return await response.json()
except (aiohttp.ClientError, json.JSONDecodeError) as e:
print(f'Cloud: {e}')
except TimeoutError:
print(f'Cloud: Timed out after {timeout} second(s).')
async def get_egtb(self, fen: str, variant: str, timeout: int) -> dict[str, Any] | None:
try:
async with self.external_session.get(f'https://tablebase.lichess.ovh/{variant}',
params={'fen': fen},
timeout=aiohttp.ClientTimeout(total=timeout)) as response:
response.raise_for_status()
return await response.json()
except (aiohttp.ClientError, json.JSONDecodeError) as e:
print(f'EGTB: {e}')
except TimeoutError:
print(f'EGTB: Timed out after {timeout} second(s).')
@retry(**JSON_RETRY_CONDITIONS)
async def get_event_stream(self, queue: asyncio.Queue[dict[str, Any]]) -> None:
async with self.lichess_session.get('/api/stream/event',
timeout=aiohttp.ClientTimeout(sock_read=9.0)) as response:
async for line in response.content:
if line.strip():
await queue.put(json.loads(line))
@retry(**GAME_STREAM_RETRY_CONDITIONS)
async def get_game_stream(self, game_id: str, queue: asyncio.Queue[dict[str, Any]]) -> None:
async with self.lichess_session.get(f'/api/bot/game/stream/{game_id}',
timeout=aiohttp.ClientTimeout(sock_read=9.0)) as response:
async for line in response.content:
await queue.put(json.loads(line) if line.strip() else {'type': 'ping'})
@retry(**JSON_RETRY_CONDITIONS)
async def get_online_bots(self) -> list[dict[str, Any]]:
async with self.lichess_session.get('/api/bot/online',
timeout=aiohttp.ClientTimeout(sock_read=9.0)) as response:
return [json.loads(line) async for line in response.content if line.strip()]
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 self.external_session.get('https://explorer.lichess.ovh/player',
params={'player': username, 'variant': variant.value,
'fen': fen, 'color': color, 'speeds': speeds,
'modes': 'rated', 'recentGames': 0},
timeout=aiohttp.ClientTimeout(total=timeout)) as response:
response.raise_for_status()
async for line in response.content:
if line.strip():
return json.loads(line)
except (aiohttp.ClientError, json.JSONDecodeError) as e:
print(f'Explore: {e}')
except TimeoutError:
print(f'Explore: Timed out after {timeout} second(s).')
@retry(**JSON_RETRY_CONDITIONS)
async def get_token_scopes(self, token: str) -> str:
async with self.lichess_session.post('/api/token/test', data=token) as response:
json_response = await response.json()
return json_response[token]['scopes']
@retry(**JSON_RETRY_CONDITIONS)
async def get_user_status(self, username: str) -> dict[str, Any]:
async with self.lichess_session.get('/api/users/status', params={'ids': username}) as response:
json_response = await response.json()
return json_response[0]
@retry(**BASIC_RETRY_CONDITIONS)
async def resign_game(self, game_id: str) -> bool:
try:
async with self.lichess_session.post(f'/api/bot/game/{game_id}/resign') as response:
response.raise_for_status()
return True
except aiohttp.ClientResponseError as e:
print(e)
return False
async def send_chat_message(self, game_id: str, room: str, text: str) -> bool:
try:
async with self.lichess_session.post(f'/api/bot/game/{game_id}/chat',
data={'room': room, 'text': text},
timeout=aiohttp.ClientTimeout(total=1.0)) as response:
response.raise_for_status()
return True
except (aiohttp.ClientError, TimeoutError):
return False
@retry(**MOVE_RETRY_CONDITIONS)
async def send_move(self, game_id: str, uci_move: str, offer_draw: bool) -> bool:
try:
async with self.lichess_session.post(f'/api/bot/game/{game_id}/move/{uci_move}',
params={'offeringDraw': str(offer_draw).lower()},
timeout=aiohttp.ClientTimeout(total=1.0)) as response:
response.raise_for_status()
return True
except aiohttp.ClientResponseError as e:
if 500 <= e.status <= 599:
raise
if e.status != 400:
print(e)
return False
@retry(**BASIC_RETRY_CONDITIONS)
async def upgrade_account(self) -> bool:
try:
async with self.lichess_session.post('/api/bot/account/upgrade') as response:
response.raise_for_status()
return True
except aiohttp.ClientResponseError as e:
print(e)
return False