Skip to content

Instantly share code, notes, and snippets.

@matburt
Last active June 16, 2021 13:15
Show Gist options
  • Save matburt/5614df61b39036504b5f to your computer and use it in GitHub Desktop.
Save matburt/5614df61b39036504b5f to your computer and use it in GitHub Desktop.
A simple emacs module for interfacing with online-go.com
;;; onlinego.el
;; Author: Matthew Jones <[email protected]>
(require 'request)
(defvar onlinego-api-base "https://online-go.com/api/v1"
"The base onlinego api url")
(defcustom onlinego-api-client-id nil
"onlinego api client id, see: https://online-go.com/developer"
:group 'onlinego
:type 'string)
(defcustom onlinego-api-client-secret nil
"onlinego api client secret, see: https://online-go.com/developer"
:group 'onlinego
:type 'string)
(defcustom onlinego-api-user nil
"onlinego username"
:group 'onlinego
:type 'string)
(defcustom onlinego-api-password nil
"onlinego application specific password, can be generated at: https://online-go.com/user/settings"
:group 'onlinego
:type 'string)
(defvar onlinego-gamelist-mode-map nil
"Keymap used in undo-tree-mode.")
(defvar onlinego-access-token nil
"Used for bearer authentication when making api requests")
(defconst onlinego-gamelist-buffername "*onlinego-gamelist*")
(defface onlinego-gamelist-selection-color
'((((class color)) :background "gray"))
"Face used to highlight items in the gamelist"
:group 'onlinego)
(defface onlinego-gamelist-name-not-turn-color
'((((class color)) :foreground "green"))
"Face used to highlight the name of a game in the game list when it isn't your turn"
:group 'onlinego)
(defface onlinego-gamelist-name-turn-color
'((((class color)) :foreground "red"))
"Face used to highlight the name of a game in the game list when it is your turn"
:group 'onlinego)
(unless onlinego-gamelist-mode-map
(let ((map (make-sparse-keymap)))
;; basic keys
(define-key map(kbd "g") 'onlinego-refresh-game-list)
(define-key map(kbd "q") 'onlinego-quit-game-list)
;; set keymap
(setq onlinego-gamelist-mode-map map)))
(defun onlinego-refresh-authorization-token ()
"Use the client and secret to refresh"
(unless onlinego-access-token
(request
"https://online-go.com/oauth2/access_token/"
:type "POST"
:parser 'json-read
:data `((client_id . ,onlinego-api-client-id)
(client_secret . ,onlinego-api-client-secret)
(grant_type . "password")
(username . ,onlinego-api-user)
(password . ,onlinego-api-password))
:success (function*
(lambda (&key data &allow-other-keys)
(setq onlinego-access-token (assoc-default 'access_token data))))
:error (function* (lambda (&key error-thrown &allow-other-keys&rest _)
(message "Error refreshing token: %S" error-thrown))))))
(defun onlinego-display-game (game)
(let ((inhibit-read-only t))
;;(insert (assoc-default 'name this_game) "\n")))
(insert (propertize (assoc-default 'name this_game) 'font-lock-face 'onlinego-gamelist-name-turn-color))
(insert "\n")))
(defun onlinego-refresh-game-list ()
"Re-fetch and display games in the game list"
(interactive)
(message "Using %S" onlinego-access-token)
(request
"https://online-go.com/api/v1/me/games/"
:headers `(("Content-Type" . "application/json")
("Authorization" . ,(format "Bearer %s" onlinego-access-token)))
:params '(("ended__isnull" . "true"))
:parser 'json-read
:success (function*
(lambda (&key data &allow-other-keys)
(let ((my_games (assoc-default 'results data))
(idx 0))
(while (< idx (length my_games))
(let ((this_game (elt my_games idx)))
(onlinego-display-game this_game))
;;(message (assoc-default 'name this_game)))
(setq idx (1+ idx))))))))
(defun onlinego-show-games ()
"Show list of games on OGS"
(interactive)
(onlinego-refresh-authorization-token)
(switch-to-buffer-other-window
(get-buffer-create onlinego-gamelist-buffername))
(insert "Game List:")
(newline 2)
(onlinego-gamelist-mode))
(defun onlinego-quit-game-list ()
"Exit the onlinego game list"
(interactive)
(kill-buffer nil))
(define-derived-mode
onlinego-gamelist-mode special-mode "onlinego-show-games"
"The major mode for onlinego's game list"
:syntax-table nil
:abbrev-table nil
(setq truncate-lines t)
(setq cursor-type nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment