Bulk SGF download

Hi,

For an app project I would like to download all of my games. I could do this manually but it would be time consuming. Is it possible and not too much effort for an OGS mod to upload a zip file somewhere? Or is there an API I can use?

I don’t believe there is an easy way to do this (even for mods), but it should be possible to write a script using the APIs.

If you do go this route, please share the script - I’m sure it would be useful to other people on the forum!

2 Likes

I used Bing Copilot (essentially ChatGPT 4) with @benjito’s API links. This is what it gave me:

import requests
import os
import re

def sanitize_filename(filename):
    # Remove invalid characters (e.g., colons, slashes, etc.)
    return re.sub(r'[\\/:*?"<>|]', '_', filename)

def download_sgf_files(url):
    try:
        while url:
            response = requests.get(url)
            data = response.json()
            if "results" in data:
                for result in data["results"]:
                    if "related" in result and "detail" in result["related"]:
                        game_id = result["related"]["detail"].split("/")[-1]
                        game_name = result.get("name", "unknown")
                        sanitized_game_name = sanitize_filename(game_name)
                        final_url = f"https://online-go.com/api/v1/games/{game_id}/sgf"
                        download_file(final_url, f"game_{game_id}_{sanitized_game_name}.sgf")
            else:
                print("No game data found.")
                break

            # Check if there's a next page
            url = data.get("next")
    except Exception as e:
        print(f"Error: {e}")

def download_file(url, filename):
    try:
        response = requests.get(url)
        with open(filename, "wb") as file:
            file.write(response.content)
        print(f"Downloaded {filename}")
    except Exception as e:
        print(f"Error downloading {filename}: {e}")

if __name__ == "__main__":
    base_url = "https://online-go.com/api/v1/players/1338646/games"
    download_sgf_files(base_url)

Change this line:
base_url = "https://online-go.com/api/v1/players/1338646/games"
and put your own account ID.

Install Python from:
Download Python | Python.org

Then open a terminal and install the requests module:
python -m pip install requests

After that navigate to where you saved the script and call it:
python REPLACE_WITH_SCRIPT_NAME.py

All games from your account will be downloaded next to the script.

1 Like