#!/usr/bin/env -S sbcl --script

(load "~/quicklisp/setup.lisp")
(ql:quickload '(drakma lquery))

(defun get-images ()
  (loop for i from 1490 to 1734 do
    (let* ((request (drakma:http-request
		     (concatenate 'string "https://www.lfg.co/read/comic/" (write-to-string i))))
	   (parsed-content (lquery:$ (lquery:initialize request)))
	   (url (vector-pop (lquery:$ parsed-content "img" (attr :src))))
	   (img (drakma:http-request url)))

      (if (not (search ".jpg" url))
	  (with-open-file (stream
			   (concatenate 'string "/home/chris/pics/lfg/" (write-to-string i) ".gif")
			   :direction :output
			   :if-exists :supersede
			   :if-does-not-exist :create
			   :element-type 'unsigned-byte)
	    (write-sequence img stream))
	  (with-open-file (stream
			   (concatenate 'string "/home/chris/pics/lfg/" (write-to-string i) ".jpg")
			   :direction :output
			   :if-exists :supersede
			   :if-does-not-exist :create
			   :element-type 'unsigned-byte)
	    (write-sequence img stream))))))

(get-images)
