do we have any python programmers using the OGS API , how do we get support on the API ?

Is anyone using the API using python ?
How do you get support on the OGS API ?
Do we have any example code in python ?

Thanks
Hugh

It’s been a while since I touched it, but here’s a project I did:

Powered by @Bone-A_Lisa’s library:

2 Likes

see my python code at the end of this thread.

i ran my java code through copilot and made the python code below. it seems to work.

import requests
import time

class OGS:
@staticmethod
def pp(json_data):
“”" Pretty-print JSON data “”"
import json
formatted_json = json.dumps(json_data, indent=4)
print(“pp:”, formatted_json)

@staticmethod
def get_json(url):
    """ Fetch JSON data from the given URL """
    try:
        response = requests.get(url)
        if response.status_code == 429:
            print("429! - Going to sleep.")
            time.sleep(5)
            print("429! - Awake.")
            return None

        response.raise_for_status()
        time.sleep(0.2)  # Simulate delay in request handling
        return response.json()
    except requests.exceptions.RequestException as e:
        print("Caught:", e)
        return None

@staticmethod
def list_reviews(player_id):
    """ List reviews for a given player ID """
    games = OGS.get_json(f"https://online-go.com/api/v1/players/{player_id}/games")

    if games is None:
        print("Failed to retrieve games data.")
        return

    print(f"{games.get('count', 'Unknown')} games.")
    next_page = games.get("next")

    while next_page:
        for game in games.get("results", []):
            game_id = game.get("id")
            started = game.get("started")
            ended = game.get("ended")
            reviews_url = f"https://online-go.com/api/v1/games/{game_id}/reviews"

            reviews = OGS.get_json(reviews_url)

            if reviews is None:
                print("Reviews is null! Probably due to a 429 response, stopping.")
                return

            review_count = reviews.get("count", 0)
            if review_count == 0:
                continue

            print(f"Game: {game_id} {game.get('name')} {started} - {ended}")

            for review in reviews.get("results", []):
                username = review.get("owner", {}).get("username")
                print(f"\tReview by: {username}")

        games = OGS.get_json(next_page)
        next_page = games.get("next") if games else None

@staticmethod
def main(args=None):
    """ Main function to list reviews for players """
    ray, hugh = 179, 1567393
    player_id = hugh if not args else int(args[0])
    OGS.list_reviews(player_id)

Example usage

if name == “main”:
OGS.main()

1 Like

I’m struggling to get the API authenticated.
The code that rtayek supplied works but is using the limited unauthenticated interface.

When I try the ogs client code with the test code:


from ogsapi.client import OGSClient


client_id = '...'
client_secret = '....'
username = '...'
password = '...'

ogs = OGSClient(client_id, client_secret, username, password)

I get:

(venv) $ python3 tryogs.py
Traceback (most recent call last):
  File "/Users/xxxx/Projects/Rest/tryogs.py", line 15, in <module>
    ogs = OGSClient(client_id, client_secret, username, password)
  File "/Users/xxxx/venv/lib/python3.13/site-packages/ogsapi/client.py", line 92, in __init__
    self.credentials.user_id = self.user_vitals()['id']
                               ~~~~~~~~~~~~~~~~^^
  File "/Users/xxxx/venv/lib/python3.13/site-packages/ogsapi/client.py", line 113, in user_vitals
    return self.api.call_rest_endpoint('GET', endpoint=endpoint).json()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'json'
(venv) $

A more basic test is:

import requests


client_id = '...'
client_secret = '....'
username = '...'
password = '...'


def get_access_token(client_id, client_secret, username, password):
    # access_token gives 404, token gives 405
    url = 'https://online-go.com/oauth2/token'

    payload = {
        'grant_type': 'password',
        'client_id': client_id,
        'client_secret': client_secret,
        'username': username,
        'password': password,
    }

    print(f'payload: {payload}')
    response = requests.post(
                   url,
                   data=payload,
                   headers={'Content-Type': 'application/x-www-form-urlencoded'},
                   timeout=20
               )

                             
    print(f'response: {response}')

token = get_access_token(client_id, client_secret, username, password)
print(f'Access Token: {token}')

The result is:

(venv) $ python3 get-token.py
payload: {'grant_type': 'password', 'client_id': '...', 'client_secret': '...', 'username': '...', 'password': '....'}
response: <Response [405]>
Access Token: None
(venv) $
(venv) $

For username and password I am using my login details for when I play games.
For client_id and client_secret I am using the values returned to me when I registered to use the application.