From 53ca0a271c305efd78ac4971624f4f1f694750cb Mon Sep 17 00:00:00 2001 From: not manx Date: Thu, 2 Jul 2020 16:09:39 +0000 Subject: [PATCH] Use asdf:defsystem --- .gitignore | 2 +- about.html | 2 +- config.example.lisp | 1 - loli.lisp | 125 +++++++++--------- lolisp.asd | 16 +++ ....example.service => lolisp.example.service | 0 package.lisp | 2 + readme.org | 4 +- 8 files changed, 84 insertions(+), 68 deletions(-) delete mode 100644 config.example.lisp create mode 100644 lolisp.asd rename loli.example.service => lolisp.example.service (100%) create mode 100644 package.lisp diff --git a/.gitignore b/.gitignore index 46c652c..977f80d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ config.lisp -loli.service +lolisp.service \#*# *~ \ No newline at end of file diff --git a/about.html b/about.html index 1969ac8..6d9a52a 100644 --- a/about.html +++ b/about.html @@ -6,7 +6,7 @@

plum.moe/loli

-

Gets a random loli from lolibooru, converts it to base64 and displays it in the browser.

+

Gets a random loli from lolibooru.

By default it only searches for images tagged as safe. You can pass rating=q or rating=e if you're feeling lewd.
Passing rating=-e will randomly choose a non-explicit image and so fourth.

Tring to query lolibooru via the URL otherwise will not work.

Made by Manx, source available on github.

diff --git a/config.example.lisp b/config.example.lisp deleted file mode 100644 index 9ce889e..0000000 --- a/config.example.lisp +++ /dev/null @@ -1 +0,0 @@ -(defparameter *host* "localhost") diff --git a/loli.lisp b/loli.lisp index 1196638..5d0d066 100644 --- a/loli.lisp +++ b/loli.lisp @@ -1,62 +1,63 @@ -(ql:quickload :aserve) -(ql:quickload :dexador) -(ql:quickload :jsown) -(ql:quickload :qbase64) -(ql:quickload :cl-ppcre) -(ql:quickload :cl-rng) -(ql:quickload :manx-utils) - -(load #p"config.lisp") - -(when (null *host*) - (print "Host can't be nil") - (exit :code -1)) - -(defun get-loli (rating) - (jsown:val - (car ;; %parse-json returns a list of a list - (jsown:parse - (dex:get - (manx-utils:to-string - "https://lolibooru.moe/post/index.json?tags=rating:" rating "+-3dcg+-rape&limit=1&page=" - (cl-rng:crandom :limit 25000 :transform 'floor))))) - "file_url")) - -(defun main (&rest args) - (net.aserve:start :port 8080 :host *host*) - - (net.aserve:publish - :path "/loli" - :content-type "text/html" - :function 'this-is-bad-code) - - (net.aserve:publish-file - :path "/loli/about" - :file #p"about.html") - - (loop (sleep 10000))) - -(defun loli-rating (rating) - (or (cl-ppcre:scan-to-strings "-?(s|q|e)" rating) - "s")) - -(defun based-loli (req) - (manx-utils:to-string - "data:image/jpg;base64," - (qbase64:encode-bytes - (dex:get (get-loli - (loli-rating (net.aserve:request-query-value "rating" req))))))) - -(defun this-is-bad-code (req ent) - (net.aserve:with-http-response (req ent) - (net.aserve:with-http-body (req ent) - (net.html.generator:html - (:html - (:head - ((:meta name "viewport" content "width=device-width, initial-scale=1"))) - ((:body :style "position:relative") - ((:img :style "object-fit: contain; width: 100%; height:100%" - :src (based-loli req))) - ((:a :style "position:absolute;top:8px;right:16px;" href "/loli/about") "about")))))) - (gc :full t)) ;; Removes downloaded files from memory -(main) +(in-package :lolisp) + +(defvar @html "text/html") +(defparameter *serb* nil + "The hunchentoot acceptor (server) that will serve our handlers and + files.") + +(defmacro str (&rest strs) + "Concatenate a list of strings `strs' to one string." + `(concatenate 'string ,@strs)) + +(defmacro config-item (thing &aux (item (gensym))) + "Get `thing' from the alist `config', error if it doesn't exist" + `(let ((,item (assoc ,thing config))) + (when (atom ,item) + (error "No such config item")) + (cdr ,item))) + +(defun loli-link (rating) + "Extract file_url from the JSON returned by `dex:get' and replace +escaped backslashes (\\\\/) with /. + +Use cl-rng to pick a random page from the lolibooru API" + (cl-ppcre:register-groups-bind (url) + ("(?:file_url\":\")(.*?)(?:\",)" + (dex:get (str "https://lolibooru.moe/post/index.json?tags=rating:" + (or (cl-ppcre:scan-to-strings "-?[sqe]" rating) "s") + "+-3dcg+-rape&limit=1&page=" + (write-to-string (cl-rng:crandom :limit 25000 :transform 'floor))))) + (cl-ppcre:regex-replace-all "\\\\/" url "/"))) + +(defun based-loli (url) + (str "data:image/jpeg;base64," (qbase64:encode-bytes (dex:get url)))) + +(henh:handle :get (loli :uri "/loli/") @html + (rating) + (cl-who:with-html-output-to-string (x) + (:html + (:head + (:meta :charset "utf-8") + (:meta :name "viewport" :content "width=device-width, initial-scale=1") + (:title "lolisp") + (:style "body{position:relative;}img{object-fit:contain;width:100%;height:100%;}#about{position:absolute;top:8px;right:16px}")) + (:body + (:img :src (based-loli (loli-link rating))) + (:a :id "about" :href "/loli/about" "about"))) + x)) + +(henh:host-file "/loli/about" "about.html" @html) + +(defun configure () + (setf *serb* (make-instance 'hunchentoot:easy-acceptor + :port (config-item :port) + :document-root (config-item :document-root)))) + +(defun start () + (handler-case (configure) + (error (e) "Configuration failed: ~a" e)) + (hunchentoot:start *serb*)) + +(export '(configure + start + *serb*)) diff --git a/lolisp.asd b/lolisp.asd new file mode 100644 index 0000000..e0ff7b3 --- /dev/null +++ b/lolisp.asd @@ -0,0 +1,16 @@ +(asdf:defsystem :lolisp + :description "The worst lolibooru scraper you've ever seen" + :author "Manx (boku@plum.moe)" + :license "X11/MIT" + :version "1.0.0" + :serial t + :depends-on (:hunchentoot + :hunchenhelpers + :cl-who + :dexador + :qbase64 + :cl-ppcre + :cl-rng) + :Components ((:file "package") + (:file "config") + (:file "loli"))) diff --git a/loli.example.service b/lolisp.example.service similarity index 100% rename from loli.example.service rename to lolisp.example.service diff --git a/package.lisp b/package.lisp new file mode 100644 index 0000000..57d0c31 --- /dev/null +++ b/package.lisp @@ -0,0 +1,2 @@ +(defpackage :lolisp + (:use :cl)) diff --git a/readme.org b/readme.org index e3f2134..eb6a419 100644 --- a/readme.org +++ b/readme.org @@ -8,15 +8,13 @@ This code is terrible because I am too. I use SBCL. Most systems will be downloaded by quicklisp. You wll need to git -clone [[https://github.com/C-xC-c/manx-utils][manx-utils]] and [[https://github.com/notflan/cl-rng][cl-rng]] manually and add them to your +clone [[https://github.com/C-xC-c/manx-utils][manx-utils]], hunchenhelpers and [[https://github.com/notflan/cl-rng][cl-rng]] manually and add them to your ~local-projects~. *** Hosting - Clone the repo - Move ~loli.example.service~ to ~/etc/systemd/system/loli.service~ - Change ~workingdirectory~ in ~loli.service~ to wherever your lolisp is -- Move ~config.example.lisp~ to ~config.lisp~ and change ~*host*~ to - your domain name - Run ~systemctl daemon-reload~ - Start lolisp with ~service loli start~ - View the page at ~*host*/loli~