diff --git a/booru.lisp b/booru.lisp index 5f3ad44..f25e8c9 100644 --- a/booru.lisp +++ b/booru.lisp @@ -1,5 +1,7 @@ (in-package #:lolicore) +(defparameter *boorus* '()) + (defmacro make-ratings ((safe questionable explicit)) `(quote (("s" . ,safe) ("q" . ,questionable) @@ -12,7 +14,7 @@ (defclass booru () ((name :initarg :name - :reader :name) + :reader name) (url :initarg :url :reader url) @@ -23,6 +25,7 @@ :initarg :rating-posts :reader rating-posts)) (:default-initargs + :name (error "Need a name") :url (error "Need a URL") :rating-table '(("s" . "s") ("q" . "q") @@ -30,7 +33,19 @@ :rating-posts (error "Need number of posts per rating"))) (defmethod get-rating ((obj booru) rating) + "Gets the `rating' from `rating-table' of `booru'" (cdr (assoc rating (rating-table obj) :test 'string=))) (defmethod get-posts ((obj booru) rating) + "Gets the max posts for `rating' for `booru'" (cdr (assoc rating (rating-posts obj) :test 'string=))) + +;; Built-in boorus +(add-booru :name 'lolibooru + :url "https://lolibooru.moe/post/index.json?tags=rating:~a+-3dcg+-rape&limit=1&page=~a" + :rating-posts (make-ratings (74000 53000 92000))) + +(add-booru :name 'gelbooru + :url "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&tags=rating:~a&limit=1&pid=~a" + :rating-posts (make-ratings (20000 20000 20000)) + :rating-table ratings-full-words) diff --git a/lolicore.lisp b/lolicore.lisp index 78c5cb9..56fa4fb 100644 --- a/lolicore.lisp +++ b/lolicore.lisp @@ -1,27 +1,22 @@ (in-package #:lolicore) -(defparameter *boorus* '()) - -(add-booru :name 'lolibooru - :url "https://lolibooru.moe/post/index.json?tags=rating:~a+-3dcg+-rape&limit=1&page=~a" - :rating-posts (make-ratings (74000 53000 92000))) - -(add-booru :name 'gelbooru - :url "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&tags=rating:~a&limit=1&pid=~a" - :rating-posts (make-ratings (20000 20000 20000)) - :rating-table ratings-full-words) - -(defun loli-link (rating) +(defun loli-link (rating ) + "Generate a valid link to a loli image from a random booru" (let ((booru (cl-rng:within *boorus*))) (format nil (url booru) (get-rating booru rating) (cl-rng:crandom :limit (get-posts booru rating) :transform 'floor)))) (defun loli-json (rating) + "Get a link using `loli-link', download it and return parsed JSON" (car (jsown:parse (dex:get (loli-link rating))))) (defun loli-data (json &key tags file-url &aux (lst '())) + "Takes input `json' from `loli-json' and a set of keys to return +data from + +Passing `:tags' will return the images tags, and so fourth." (when tags (push (add-data :tags "tags" json) lst)) (when file-url @@ -39,9 +34,3 @@ (declare (type string url)) (str "data:image/jpeg;base64," (qbase64:encode-bytes (dex:get url)))) -(export '(loli-link - based-loli - loli-data - loli-json - loli-get - *boorus*)) diff --git a/package.lisp b/package.lisp index 0c1d4eb..1b53601 100644 --- a/package.lisp +++ b/package.lisp @@ -1,3 +1,9 @@ (defpackage #:lolicore (:nicknames #:rori) - (:use #:cl)) + (:use #:cl) + (:export #:loli-link + #:based-loli + #:loli-data + #:loli-json + #:loli-get + #:*boorus*)) diff --git a/utils.lisp b/utils.lisp index cd06dd2..dbc03e9 100644 --- a/utils.lisp +++ b/utils.lisp @@ -9,3 +9,14 @@ (defmacro add-booru (&rest args) `(push (make-instance 'booru ,@args) *boorus*)) + +(defun list-boorus () + "List all of the exported boorus" + (loop for booru in *boorus* collect (name booru))) + +(defun get-booru (name) + "Get a specific `booru' from `*boorus*' by name" + (loop for booru in *boorus* + do (when (eql name (:name booru)) + (return booru)) + finally (error "No such booru")))