Downloading pgn files?

Sort:
Avatar of gambit-man

I would like to be able to download pgn files of thematic tournaments i've played in, such as this one http://www.chess.com/tournament/10th-chesscom-thematic-tournament---caro-kann-1801-2000

When i go to the list of games, i can access them all with no problems, but so far as i can see, can only download a single game at a time.

Anyone know how to download them quicker? ie, a batch or even the whole lot in one go

Avatar of gambit-man

Cheers Ron, but it wasn't the "get pgn" at the side of the board during a game i was talking about...

Say for instance you look at a player's finished games, (mine's is here http://www.chess.com/home/my_archive?show=echess), you can download a batch of 50 in one go.

If you look at the games list from the tournament i mentioned in the first post http://www.chess.com/tournaments/games?id=27528 there doesn't seem to be a way to download a batch. It seems to me the only way is to open each game then download the pgn file for that game only...

A bit of a long and monotonous task if there are 1750+ games

Avatar of Ruby-Fischer

At the bottom of the page there should be a select all option and a de-select all?

Avatar of gambit-man
Ruby-Fischer wrote:

At the bottom of the page there should be a select all option and a de-select all?

There is on the list of games by any player, but not on a list of games from a tournament

Avatar of Ruby-Fischer

Oh yes, sorry, you must need to do it one by one. Bit of a pain.

Avatar of gambit-man

ok, thanks guys.

We'll see if anyone else has any ideas...

Avatar of TortoiseMaximus

Maybe could write a Python script to do it.  Pull all the links off the page with "*gameid=*" in it.  There's a Python pgn library that could probably combine all the individual files into one pgn.

Avatar of gambit-man

Thanks TM, but i've no idea what that is...Undecided

Avatar of TortoiseMaximus

Hang on, I'm interested now, I'll see if I can't cook something up ...

Avatar of gambit-man

...you're seeing the value in thematic tournament games?

Avatar of MrEdCollins

The value (game ID) is available in the page source.  (And can also be seen if you put your cursor over the game result.)

Maximus has an idea to write a script that will automatically visit the page, retrieve the Game IDs, and then access that page directly, and thus be able to download the PGN file.  The script will continue until all of the games in the tournament are downloaded.

It should work.  I've written similar programs before, to download stock data from multiple pages, for instance, and this doesn't seem too difficult.  (But I've never written them in Python, but the language one uses is immaterial, as long as it works.)

Avatar of gambit-man

Excellent! I'm catching on now, thanks for the explanation

Avatar of TortoiseMaximus

Here's a Python 2 script that will grab all the games off a tournament page and combine them into a PGN file.  Just change "tournament_url" to the name of the tournament and "pgn_name" to the name/location of the pgn file you want to create. 

In the second to last line, 'pgn = "\r\r".join(games)', change the second 'r' to an 'n', it just prints a blank line in the web browser if I type it in here.

Seems like it takes a couple of minutes to get ~100 games.

--------------------------

from urlib import urlopen
import re

# INPUTS
tournament_url = 'http://www.chess.com/tournament/10th-chesscom-thematic-tournament--caro-kann-1801-2000'
pgn_name = 'c:/tournament.pgn'   # Change to output filename for pgn file
   
tournament_page = urlopen(tournament_url).read()
pattern = re.compile('game\?id=(\d+)')
game_ids = re.findall(pattern, tournament_page)
game_links = ['http://www.chess.com/echess/download_pgn?id=' + g
              for g in game_ids]
games = [urlopen(link).read() for link in game_links]
pgn = "\r\r".join(games)
with open(pgn_name, 'w') as outfile:  print >>outfile, pgn

Avatar of gambit-man

That was incredibly quick TM, but i'm not sure what i should be doing with it... sorry, i'm not much of a techie...

Avatar of TortoiseMaximus

You can go to the command prompt on your computer (eg on Windows, clicking Start and typing "command" in the search box) and type in "python".  If something like "Python 2.6 >>>" comes up, then you could just type or copy/paste the script above in line by line and it should work. 

Or you could copy/paste the script into a text file and save it as "chesscom_pgn.py" and then type "python chesscom_pgn.py" into the command line and it should run.

Python is on most machines these days so if nothing happens when you type "python" there's usually just path variables that need to be set on your OS.

Anyway, good luck.

Avatar of gambit-man

OK, thanks. I'll need to wait til i've got time for a session before trying this.

Your effort is appreciated Smile