我正在编写与Lichess交互的应用程序。但是,当我使用API获取游戏状态时,必须使用
for event in client.bots.stream_game_state(game_id):
由文档提供。实际上,这正是他们在示例中使用的代码。但是,出现以下错误:
HTTPError: 400 Client Error: Bad Request for url: https://lichess.org/api/bot/game/stream/8Kjolz4k
这是我的代码:
# Type "!pip install berserk" in Console to get berserk module
import berserk
import time
import threading
# Initiate a session with my personal API token
token = "***************"
session = berserk.TokenSession(token)
client = berserk.Client(session)
# Stream whats happening and continue when we are challenged
in_game = False
while(not in_game):
time.sleep(0.5)
for event in client.bots.stream_incoming_events():
if event['type'] == 'gameStart':
game_id = event['game']['id']
in_game = True
break
elif event['type'] == 'challenge':
game_id = event['challenge']['id']
client.bots.accept_challenge(game_id)
in_game = True
print("The game has started!")
# Stream the events of the game and respond accordingly
playing = True
while(playing):
for event in client.bots.stream_game_state(game_id):
if event['type'] == 'gameFull':
if client.account.get()['username'] == event['white']['id']:
client.bots.post_message(game_id, "I got first, nerd!")
else:
client.bots.post_message(game_id, "You got first, nerd!")
我不知道为什么您的代码当时失败了。可以这样捕获400响应(例如abort call的示例):