Skip to content

Instantly share code, notes, and snippets.

@StargazeSparkle
Created January 8, 2021 05:53
Show Gist options
  • Save StargazeSparkle/9a1bac1a5fc48fcc735973471977c08f to your computer and use it in GitHub Desktop.
Save StargazeSparkle/9a1bac1a5fc48fcc735973471977c08f to your computer and use it in GitHub Desktop.
(defpackage :spellcheck
(:use cl arror-macros)
(:export :start-bot))
(in-package :spellcheck)
(require :access)
(require :cl-json)
(require :drakma)
(require :quri)
(require :unix-opts)
(defvar *cookie-jar* (make-instance 'drakma:cookie-jar)
"Stores session cookies for each request.")
(defvar *wiki-url* nil
"Fully-qualified url to the base of the installation.")
(defun http-get (url params)
"Performs an HTTP GET request to the URL with PARAMS."
(let ((qs (-<> (quri:url-encode-params params)
(format nil "~A?~A" *wiki-url* <>)))
(drakma:http-request url
:cookie-jar *cookie-jar*
:preserve-url t))))
(defun http-post (url params)
"Performs an HTTP POST request to the URL with PARAMS."
(drakma:http-request url
:content-type "application/x-www-form-urlencoded"
:cookie-jar *cookie-jar*
:method :post
:parameters params
:preserve-uri t))
(defun get-lgtoken (resp)
"Fetches the login token from RESP."
(access:accesses resp 'query 'tokens 'logintoken))
(defun lgtoken-p (resp)
"Checks whether or not LGTOKEN is present in RESP."
(nil? (get-lgtoken resp)))
(defun login (username password)
"Performs the complete login handshake with the API."
(let ((api (format nil "~A/api.php" *wiki-url*))
(params `(("action" "edit")
("lgname" ,username)
("lgpassword" ,password)
("format" "json")))
(token-res (http-post api params)))
(if (lgtoken-p (json:decode-json-from-string token-res))
;; HERE I need to send another request and process the return value
;; (http-post api (cons params `("lgtoken" ,(get-lgtoken res))))
nil)))
(defun start-bot (username password wiki &key lang)
"Entry-point to start the spellchecker bot."
(let ((wiki-lang (if (nil? lang) "en" lang)))
(setq *wiki-url* (format nil "~A/~A" wiki wiki-lang))
(login username password)))
@fiddlerwoaroof
Copy link

If is an expression, so this might help:

(defun login (username password)
  "Performs the complete login handshake with the API."
  (let* ((api (format nil "~A/api.php" *wiki-url*))
         (params `(("action" "edit")
                   ("lgname" ,username)
                   ("lgpassword" ,password)
                   ("format" "json")))
         (token-res (http-post api params))
         (response (if (lgtoken-p (json:decode-json-from-string token-res))
                       ;; HERE I need to send another request and process the return value
                       ;; (http-post api (cons params `("lgtoken" ,(get-lgtoken res))))
                       nil)))
    ;;Do Something with response
    ))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment