summaryrefslogtreecommitdiff
path: root/src/gizmo/wasp-copfish.el
blob: 024c27dc6b9d2b08f4d859a2b67576d4c2736bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
;;; wasp-copfish --- Copfish interface -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:

(require 'dash)
(require 's)
(require 'f)
(require 'ht)
(require 'rx)
(require 'cl-lib)
(require 'request)
(require 'dom)
(require 'wasp-user)

(defcustom w/copfish-server "https://cop.fish/api/"
  "Server URL for Copfish."
  :type '(string)
  :group 'wasp)

(defvar w/copfish-last-response nil)

(defun w/copfish-get (loc k)
  "Get LOC from Copfish, passing the returned HTML to K."
  (setf request-message-level -1)
  (request
    (s-concat w/copfish-server loc)
    :type "GET"
    :parser #'buffer-string
    :success
    (cl-function
     (lambda (&key data &allow-other-keys)
       (setq w/copfish-last-response data)
       (funcall k data))))
  t)

(defun w/copfish-get-fish (user k)
  "Retrieve USER's fish ratio from copfish API.
Pass the resulting fraction to K."
  (w/copfish-get
   (s-concat "fishdex/" user)
   (lambda (s)
     (let ((sp (s-split " " s)))
       (when (= (length sp) 2)
         (funcall k (cons (string-to-number (car sp)) (string-to-number (cadr sp)))))))))

(defvar w/copfish-user-cache nil)
(defun w/copfish-update-user (user)
  "Update USER data from Copfish."
  (unless (-contains? w/copfish-user-cache user)
    (add-to-list 'w/copfish-user-cache user)
    (w/copfish-get-fish
     user
     (lambda (ct)
       (w/user-bind
        user
        (lambda ()
          (setf (alist-get :copfish-ratio w/user-current) ct)))))))

(provide 'wasp-copfish)
;;; wasp-copfish.el ends here