commit fd33fdd85b2855655eb84f32907f31b7d845ec98 Author: not manx Date: Fri Jul 10 16:12:18 2020 +0000 Initial commit diff --git a/booru.lisp b/booru.lisp new file mode 100644 index 0000000..5f3ad44 --- /dev/null +++ b/booru.lisp @@ -0,0 +1,36 @@ +(in-package #:lolicore) + +(defmacro make-ratings ((safe questionable explicit)) + `(quote (("s" . ,safe) + ("q" . ,questionable) + ("e" . ,explicit)))) + +(defvar ratings-full-words '(("s" . "safe") + ("q" . "questionable") + ("e" . "explicit"))) + +(defclass booru () + ((name + :initarg :name + :reader :name) + (url + :initarg :url + :reader url) + (rating-table + :initarg :rating-table + :reader rating-table) + (rating-posts + :initarg :rating-posts + :reader rating-posts)) + (:default-initargs + :url (error "Need a URL") + :rating-table '(("s" . "s") + ("q" . "q") + ("e" . "e")) + :rating-posts (error "Need number of posts per rating"))) + +(defmethod get-rating ((obj booru) rating) + (cdr (assoc rating (rating-table obj) :test 'string=))) + +(defmethod get-posts ((obj booru) rating) + (cdr (assoc rating (rating-posts obj) :test 'string=))) diff --git a/lolicore.asd b/lolicore.asd new file mode 100644 index 0000000..862edec --- /dev/null +++ b/lolicore.asd @@ -0,0 +1,14 @@ +(asdf:defsystem #:lolicore + :description "" + :author "Manx " + :license "GPLv3" + :version "1.0.0" + :serial t + :depends-on (:qbase64 + :dexador + :cl-rng + :jsown) + :Components ((:file "package") + (:file "utils") + (:file "booru") + (:file "lolicore"))) diff --git a/lolicore.lisp b/lolicore.lisp new file mode 100644 index 0000000..78c5cb9 --- /dev/null +++ b/lolicore.lisp @@ -0,0 +1,47 @@ +(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) + (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) + (car (jsown:parse (dex:get (loli-link rating))))) + +(defun loli-data (json &key tags file-url + &aux (lst '())) + (when tags + (push (add-data :tags "tags" json) lst)) + (when file-url + (push (add-data :file-url "file_url" json) lst)) + lst) + +(defun loli-get (rating) + "Identical to calling `loli-data' with all keys" + (loli-data (loli-json rating) + :tags t + :file-url t)) + +(defun based-loli (url) + "I got told off for trying to inline this." + (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 new file mode 100644 index 0000000..0c1d4eb --- /dev/null +++ b/package.lisp @@ -0,0 +1,3 @@ +(defpackage #:lolicore + (:nicknames #:rori) + (:use #:cl)) diff --git a/utils.lisp b/utils.lisp new file mode 100644 index 0000000..cd06dd2 --- /dev/null +++ b/utils.lisp @@ -0,0 +1,11 @@ +(in-package #:lolicore) + +(defmacro str (&rest strs) + "Concatenate a list of strings `strs' to one string." + `(concatenate 'string ,@strs)) + +(defun add-data (keyword key json) + (cons keyword (jsown:val json key))) + +(defmacro add-booru (&rest args) + `(push (make-instance 'booru ,@args) *boorus*))