Skip to content

Commit

Permalink
check json response body for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
seanbreckenridge committed Aug 28, 2023
1 parent 735bf92 commit 4ad2a87
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion chess_export/chessdotcom/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ def get_player_game_archives(username: str, user_agent_email: Optional[str] = No
"""Returns a list of monthly archive URLs for the user"""
url = BASE_URL + "/".join(("player", username, "games", "archives"))
mresp = safe_request_json(url, headers=_user_agent(user_agent_email))
if "archives" not in mresp:
raise RuntimeError(f"Unexpected response from {url}: {mresp}")
return list(mresp["archives"])


def get_player_games(username: str, user_agent_email: Optional[str] = None) -> Iterator[Json]:
"""Returns all accessible games, using the monthly archive as the source"""
for archive_url in get_player_game_archives(username, user_agent_email):
month_games = safe_request_json(archive_url, headers=_user_agent(user_agent_email))
yield from month_games["games"]
if "games" in month_games:
yield from month_games["games"]
else:
raise RuntimeError(f"Unexpected response from {archive_url}: {month_games}")

0 comments on commit 4ad2a87

Please sign in to comment.