Skip to content

Instantly share code, notes, and snippets.

@aragaer
Last active November 9, 2018 08:26
Show Gist options
  • Save aragaer/b9b708fa2dcb7894d4656710ee76f27e to your computer and use it in GitHub Desktop.
Save aragaer/b9b708fa2dcb7894d4656710ee76f27e to your computer and use it in GitHub Desktop.
chicken goes to trello
(use json)
(use posix)
(use utils)
(require-extension srfi-13)
(require-extension srfi-18)
(require-extension srfi-19)
(define api-key '())
(define token '())
(let ((file (open-input-file "keys")))
(do ((line (read-line file) (read-line file))) ((eof-object? line))
(let* ((args (string-split line "="))
(key (car args))
(value (cadr args)))
(cond ((string= key "key") (set! api-key value))
((string= key "token") (set! token value)))))
(close-input-port file))
(define api-url "https://api.trello.com")
(define boards-api "/1/members/me/boards")
(define lists-api (lambda (board-id) (conc "/1/boards/" board-id "/lists")))
(define cards-api (lambda (list-id) (conc "/1/lists/" list-id "/cards")))
(define boards-file "boards.cache")
(define build-url
(lambda (method . args)
(let ((get-args '()))
(do ((it args (cddr it))) ((null? it))
(set! get-args (cons (format "~a=~a" (car it) (cadr it)) get-args)))
(conc api-url method "?" (string-intersperse (reverse get-args) "&")))))
(define newer-than
(lambda (file . duration)
(> 0 (let ((now (seconds->date (current-seconds)))
(last-change (seconds->date (file-modification-time file))))
(date-compare now (date-add-duration last-change (apply make-duration duration)))))))
(define get-data
(lambda (method cache-file)
(let ((up-to-date #f)
(result '()))
(if (file-exists? cache-file)
(set! up-to-date (newer-than cache-file #:days 1)))
(if up-to-date
(fprintf (current-error-port) "Using cache file '~a' for method '~a'\n" cache-file method)
(let ((file (file-open cache-file (+ open/wronly open/creat) (+ perm/irusr perm/iwusr)))
(url (build-url method 'key api-key 'token token)))
(use http-client)
(file-write file (with-input-from-request url #f read-string))
(file-close file))))
(let ((file (open-input-file cache-file)))
(set! result (json-read file))
(close-input-port file))
result))
(do ((it (get-data boards-api boards-file) (cdr it))) ((null? it))
(let* ((board (vector->list (car it)))
(name (cdr (assoc "name" board)))
(id (cdr (assoc "id" board))))
(printf "~a = ~a\n" name id)
(do ((it (get-data (lists-api id) (conc "board-" id ".cache")) (cdr it))) ((null? it))
(let* ((lst (vector->list (car it)))
(name (cdr (assoc "name" lst)))
(id (cdr (assoc "id" lst))))
(printf " ~a = ~a\n" name id)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment