Skip to content

Commit 519dc28

Browse files
committed
#6 Add Games.export_bookmarks
Implement GET /api/games/export/bookmarks: export the authenticated user's bookmarked games as a stream (PGN or NDJSON). Requires OAuth2.
1 parent 26b6821 commit 519dc28

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44
To be released
55
--------------
66

7+
* Implemented ``/api/games/export/bookmarks`` under ``client.games.export_bookmarks``.
78

89
* Deprecate Python 3.9 support - minimum required version is now Python 3.10+. This does not mean the library will not work with Python 3.9, but it will not be tested against it anymore.
910

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Most of the API is available:
158158
client.games.export
159159
client.games.export_ongoing_by_player
160160
client.games.export_by_player
161+
client.games.export_bookmarks
161162
client.games.export_multi
162163
client.games.export_imported
163164
client.games.get_among_players

berserk/clients/games.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,74 @@ def export_by_player(
184184
converter=models.Game.convert,
185185
)
186186

187+
def export_bookmarks(
188+
self,
189+
as_pgn: bool | None = None,
190+
since: int | None = None,
191+
until: int | None = None,
192+
max: int | None = None,
193+
moves: bool | None = None,
194+
pgn_in_json: bool | None = None,
195+
tags: bool | None = None,
196+
clocks: bool | None = None,
197+
evals: bool | None = None,
198+
accuracy: bool | None = None,
199+
opening: bool | None = None,
200+
division: bool | None = None,
201+
literate: bool | None = None,
202+
last_fen: bool | None = None,
203+
sort: str | None = None,
204+
) -> Iterator[str] | Iterator[Dict[str, Any]]:
205+
"""Export your bookmarked games.
206+
207+
Requires OAuth2 authorization. Games are sorted by reverse chronological
208+
order (most recent first). We recommend streaming the response.
209+
210+
:param as_pgn: whether to return games in PGN format
211+
:param since: download games bookmarked since this timestamp (ms)
212+
:param until: download games bookmarked until this timestamp (ms)
213+
:param max: maximum number of bookmarked games to download
214+
:param moves: whether to include the PGN moves
215+
:param pgn_in_json: include the full PGN within JSON response
216+
:param tags: whether to include the PGN tags
217+
:param clocks: whether to include clock comments in the PGN moves
218+
:param evals: whether to include analysis evaluation comments when available
219+
:param accuracy: whether to include accuracy percent of each player (JSON only)
220+
:param opening: whether to include the opening name
221+
:param division: whether to include middlegame/endgame ply boundaries (JSON only)
222+
:param literate: whether to include textual annotations in the PGN
223+
:param last_fen: whether to include X-FEN of the last position (JSON only)
224+
:param sort: sort order (e.g. ``dateAsc``, ``dateDesc``)
225+
:return: iterator over the exported games, as JSON or PGN
226+
"""
227+
path = "/api/games/export/bookmarks"
228+
params = {
229+
"since": since,
230+
"until": until,
231+
"max": max,
232+
"moves": moves,
233+
"pgnInJson": pgn_in_json,
234+
"tags": tags,
235+
"clocks": clocks,
236+
"evals": evals,
237+
"accuracy": accuracy,
238+
"opening": opening,
239+
"division": division,
240+
"literate": literate,
241+
"lastFen": last_fen,
242+
"sort": sort,
243+
}
244+
if self._use_pgn(as_pgn):
245+
yield from self._r.get(path, params=params, fmt=PGN, stream=True)
246+
else:
247+
yield from self._r.get(
248+
path,
249+
params=params,
250+
fmt=NDJSON,
251+
stream=True,
252+
converter=models.Game.convert,
253+
)
254+
187255
def export_multi(
188256
self,
189257
*game_ids: str,

tests/clients/test_games.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests_mock
2+
3+
from berserk import Client
4+
5+
6+
class TestGamesExportBookmarks:
7+
"""Test client-side behavior for export_bookmarks (auth required)."""
8+
9+
def test_export_bookmarks_path_and_params(self):
10+
"""Request hits correct path and query params are passed."""
11+
with requests_mock.Mocker() as m:
12+
m.get(
13+
"https://lichess.org/api/games/export/bookmarks?max=10&since=1609459200000&sort=dateDesc&until=1640995200000",
14+
content=b'{"id":"abc123"}\n',
15+
)
16+
list(
17+
Client().games.export_bookmarks(
18+
since=1609459200000,
19+
until=1640995200000,
20+
max=10,
21+
sort="dateDesc",
22+
)
23+
)

0 commit comments

Comments
 (0)