Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding import_pgn endpoints #71

Merged
merged 6 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

To be released
--------------

* Added ``studies.import_pgn`` to import PGN to study

v0.13.2 (2023-12-04)
--------------------

Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Most of the API is available:
client.studies.export_chapter
client.studies.export
client.studies.export_by_username
client.studies.import_pgn

client.tablebase.look_up
client.tablebase.standard
Expand Down
2 changes: 2 additions & 0 deletions berserk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
BroadcastPlayer,
Team,
LightUser,
ChapterIdName,
OnlineLightUser,
OpeningStatistic,
PaginatedTeams,
Expand All @@ -35,6 +36,7 @@
__all__ = [
"ArenaResult",
"BroadcastPlayer",
"ChapterIdName",
"Client",
"JSON",
"JSON_LIST",
Expand Down
33 changes: 32 additions & 1 deletion berserk/clients/studies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

from typing import Iterator
from typing import cast, List, Iterator

from ..formats import PGN
from ..types.common import Color, Variant
from ..types import ChapterIdName
from .base import BaseClient


Expand Down Expand Up @@ -35,3 +37,32 @@ def export_by_username(self, username: str) -> Iterator[str]:
return:iterator over all chapters as PGN"""
path = f"/study/by/{username}/export.pgn"
yield from self._r.get(path, fmt=PGN, stream=True)

def import_pgn(
self,
study_id: str,
chapter_name: str,
pgn: str,
orientation: Color = "white",
variant: Variant = "standard",
) -> List[ChapterIdName]:
"""Imports arbitrary PGN into an existing study.
Creates a new chapter in the study.

If the PGN contains multiple games (separated by 2 or more newlines) then multiple chapters will be created within the study.

Note that a study can contain at most 64 chapters.

return: List of the chapters {id, name}"""
# https://lichess.org/api/study/{studyId}/import-pgn
path = f"/api/study/{study_id}/import-pgn"
payload = {
"name": chapter_name,
"pgn": pgn,
"orientation": orientation,
"variant": variant,
}
# The return is of the form:
return cast(
List[ChapterIdName], self._r.post(path, data=payload).get("chapters", [])
)
2 changes: 2 additions & 0 deletions berserk/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
OpeningStatistic,
Speed,
)
from .studies import ChapterIdName
from .team import PaginatedTeams, Team
from .tournaments import ArenaResult, CurrentTournaments, SwissResult, SwissInfo

Expand All @@ -21,6 +22,7 @@
"BulkPairing",
"BulkPairingGame",
"Challenge",
"ChapterIdName",
"ClockConfig",
"CurrentTournaments",
"ExternalEngine",
Expand Down
8 changes: 8 additions & 0 deletions berserk/types/studies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from typing_extensions import TypedDict


class ChapterIdName(TypedDict):
id: str
name: str