Looking to Export & Modify Single PGNs by Game Link (PYTHON)

Sort:
ninjaswat

Hey guys, was wondering if anyone could help me in doing the following task:
- take a game link as input
- grab the pgn from that game in such a format that I can modify it to do things like:

-- remove ratings,

-- change and use variables to modify usernames
then print the pgn

Preferably in python.

I've seen many posts about using many pgns or pgns of a certain user, so was wondering if this was even possible! Thanks.

ImperfectAge

This library is pretty useful for parsing, writing and probably manipulating PGNs

https://python-chess.readthedocs.io/en/latest/

jas0501

Edit: ==============

Chess.com's posting process strips out whitespace resulting if badly formatted post. The code below suffers from this. Using ChatGPT and copying the code might be easier than using the code below.

=================

Asking ChatGPT:

help me in doing the following task:
- take a game link as input
- grab the pgn from that game in such a format that I can modify it to do things like:
-- remove ratings,
-- change and use variables to modify usernames then print the pgn
Preferably in python.


Yields:

o achieve this task, you can use the python-chess library to parse and manipulate PGN (Portable Game Notation) data. First, you'll need to install the library if you haven't already......Now, you can create a Python script to take a game link, retrieve the PGN, and perform modifications as needed. Below is an example script that demonstrates this process:


import requestsimport chess.pgn

def get_pgn_from_link(game_link): # Fetch the game data from the provided link response = requests.get(game_link) game_data = response.text

# Use python-chess to parse the PGN game = chess.pgn.read_game(io.StringIO(game_data))

return game.pgn()

def modify_pgn(pgn_text, new_username="Player1", remove_ratings=True): # Load the PGN into a chess.pgn.Game object game = chess.pgn.read_game(io.StringIO(pgn_text))

# Perform modifications for node in game.mainline(): if "WhiteElo" in node: if remove_ratings: del node["WhiteElo"] else: node["WhiteElo"] = "?" node["White"] = new_username

if "BlackElo" in node: if remove_ratings: del node["BlackElo"] else: node["BlackElo"] = "?" node["Black"] = new_username

# Return the modified PGN return game.pgn()

if __name__ == "__main__": # Example game link game_link = "https://www.chess.com/game/live/1234567890"

# Get the PGN from the game link pgn = get_pgn_from_link(game_link)

# Modify the PGN (remove ratings, change username) modified_pgn = modify_pgn(pgn, new_username="MyCustomUsername", remove_ratings=True)

# Print the modified PGN print(modified_pgn)


Replace the game_link variable with the actual link to the chess game you want to retrieve. The modify_pgn function allows you to customize the modifications you want to make, such as changing usernames and removing ratings.============================================
Granted the code doesn't always work, but explaining the errors/issues to ChatGpt often resolves the issue with improved code.

I haven't run the code. Give it a try!

Omed

This is possible but you will need to parse through the users monthly archive which can be a pain to code.
What I would do in this situation is to look at the unofficial api to get info about what users played that game and the date (simple python script that does that): https://gist.github.com/Oqarshi/fae8f7d6caa20175ad310273716bc464 Then i would look at the players archive using the data (Names, Date, etc) to filter out the pgn of the game and other stuff. I will work on this later as im kinda busy rn. Hope this helps happy.

Omed

Update: managed to spare half an hour, so here is a script that gets the pgn of a game from the chess.com game link:
https://gist.github.com/Oqarshi/f0d94d126b56b0469c4977c862c2ddf6

ninjaswat

Both of these ways seem really interesting, am I correct that the second only works for games played this month?

Omed
ninjaswat wrote:

Both of these ways seem really interesting, am I correct that the second only works for games played this month?

It works for any valid game link, as long as the game link is valid. So it can work for any games played for any year and month. If the link is invalid then it returns an error message.

ninjaswat

Thanks a ton! I’ll try both methods when I have time and see which one seems to work better, this script will be run around once a week so shouldn’t be too hard on the servers happy.png