refactor to data-source
This commit is contained in:
20
items.lisp
20
items.lisp
@@ -1,5 +1,7 @@
|
||||
(in-package :lispostory)
|
||||
|
||||
(define-data-source item)
|
||||
|
||||
(defparameter *maplestory-items-url* "https://maplestory.io/api/gms/83/item")
|
||||
|
||||
(defun get-maplestory-items ()
|
||||
@@ -7,21 +9,9 @@
|
||||
|
||||
(defparameter *chronostory-item-url-format* "https://chronostory.onrender.com/api/item-info?itemId=~a")
|
||||
|
||||
(defparameter *chronostory-items* (make-array 0))
|
||||
|
||||
(defun chronostory-item-url (item-id)
|
||||
(format nil *chronostory-item-url-format* item-id))
|
||||
|
||||
(defun reload-item-data ()
|
||||
(let ((item-data (shasht:read-json (uiop:read-file-string "items.json"))))
|
||||
(setf *chronostory-items* item-data)
|
||||
item-data))
|
||||
|
||||
(defun load-item-data ()
|
||||
(if (/= (length *chronostory-items*) 0)
|
||||
*chronostory-items*
|
||||
(reload-item-data)))
|
||||
|
||||
(defun get-chrono-item (maple-item)
|
||||
(if (gethash "isCash" maple-item)
|
||||
nil
|
||||
@@ -33,7 +23,7 @@
|
||||
(uiop:println (concatenate 'string "Retrieved id " (write-to-string item-id))))
|
||||
data)))
|
||||
|
||||
(defun refresh-item-data ()
|
||||
(defmethod refresh ((ds item-data-source))
|
||||
(let* ((maple-items (get-maplestory-items))
|
||||
(_ (uiop:println "Retrieved maplestory items. Getting chronostory items..."))
|
||||
(chrono-items
|
||||
@@ -45,10 +35,10 @@
|
||||
collect chrono-item)))
|
||||
(declare (ignore _))
|
||||
(uiop:println "Retrieved chronostory items. Writing out to json file...")
|
||||
(setf *chronostory-items* chrono-items)
|
||||
(setf (cache ds) chrono-items)
|
||||
(write-string-into-file
|
||||
(shasht:write-json chrono-items nil)
|
||||
"items.json"
|
||||
(file-path ds)
|
||||
:if-exists :overwrite
|
||||
:if-does-not-exist :create)
|
||||
(uiop:println "Done!")))
|
||||
|
||||
Reference in New Issue
Block a user