diff --git a/command-line.lisp b/command-line.lisp index 68cdf7a..c8fa2ec 100644 --- a/command-line.lisp +++ b/command-line.lisp @@ -8,9 +8,12 @@ :long "help" :description "prints this help") (:name :version - :short #\v :long "version" :description "prints the version") + (:name :verbose + :short #\v + :long "verbose" + :description "prints file names as they get downloaded") (:name :assume-yes :long "no-prompt" :description "assumes yes for all download prompts") @@ -48,7 +51,7 @@ :meta-var "PASSOWRD" :arg-parser #'identity :description "passowrd for the jellyfin server") - (:name :season-number + (:name :season :short #\s :long "season" :meta-var "SEASON" diff --git a/seanut.lisp b/seanut.lisp index 75f702e..b3eb54e 100644 --- a/seanut.lisp +++ b/seanut.lisp @@ -2,7 +2,7 @@ (in-package #:seanut) -(defun prompt-and-download (domain auth root opts &optional assume-yes) +(defun prompt-and-download (domain auth root opts) "prompt the user with y-or-n-p and download ITEM" (labels ((fetch-user-id () (gethash "Id" (json-request (format-url domain "Users/Me") @@ -22,7 +22,7 @@ (format nil "~A (~A)~@[ Season ~A~]~@[/~]" (gethash "Name" root) (gethash "ProductionYear" root) - (getf opts :season-number) + (getf opts :season) add-trailing)) (create-directory (dir) @@ -32,27 +32,28 @@ ;; PARENTS is a list of all parent names with FIRST being ;; the oldest grandparent (for building complete download path) (let ((children (gethash "Items" - (if (string= (gethash "Type" item) "Season") - (json-request (format-url domain "Shows/~A/Episodes?fields=Path&seasonId=~A~@[&season=~A~]" - (gethash "SeriesId" item) - (gethash "Id" item) - (getf opts :season-number)) + (if (or (string= (gethash "Type" item) "Season") + (and (string= (gethash "Type" item) "Series") + (getf opts :season))) + (json-request (format-url domain "Shows/~A/Episodes?fields=Path~@[&seasonId=~A~]~@[&season=~A~]" + (or (gethash "SeriesId" item) + (gethash "Id" item)) + (unless (getf opts :season) + (gethash "Id" item)) + (getf opts :season)) :auth auth) (json-request (format-url domain "Items?userId=~A&fields=Path,ChildCount&parentId=~A" (fetch-user-id) (gethash "Id" item)) :auth auth))))) - ;; DELETE: debug prints lmao - (loop :for child :across children - :do (format t "Name: ~A, Id: ~A~%" - (gethash "Name" child) - (gethash "Id" child))) (loop :for child :across children :if (zerop (gethash "ChildCount" child 0)) :do ;; download single file + (when (getf opts :verbose) + (format t "Downloading ~A~%" (generate-filename child))) (download-media (generate-filename child) (format-url domain "Items/~A/Download" (gethash "Id" child)) @@ -65,30 +66,19 @@ ;; and loop over them, recursing for each one ;; ensure that a directory exists for Parent ;; then recurse with children - (format t "Creating child directory in ~A and recursing: ~A~%" - (uiop:getcwd) (gethash "Name" child)) (uiop:with-current-directory ((create-directory (gethash "Name" child))) (download-item-or-children child)))))) - (format t "Name: ~A, Id: ~A~%" - (gethash "Name" root) - (gethash "Id" root)) - (when (or assume-yes - (y-or-n-p "Download ~A" (generate-root-name))) + (when (or (getf opts :assume-yes) + (y-or-n-p "Download \"~A\"" (generate-root-name))) ;; CD into our output directory (uiop:with-current-directory ((getf opts :output #P"./")) - (let ((grandest-parent (format nil "~A (~A)~@[ Season ~A~]/" - (gethash "Name" root) - (gethash "ProductionYear" root) - (getf opts :season-number)))) - - ;; create our folder for our current download - (ensure-directories-exist grandest-parent) - - ;; CD into it, then download the files - (uiop:with-current-directory (grandest-parent) - (download-item-or-children root))))))) + + ;; create our folder for our current download + ;; then CD into it, and start downloading + (uiop:with-current-directory ((create-directory (generate-root-name 'trailing-slash))) + (download-item-or-children root)))))) (defun main () @@ -118,7 +108,7 @@ (when (some #'null args) (quit-with-message 1 "domain and/or media name not provided")) - + (destructuring-bind (domain search-term) args (let* ((authorization (or (and (getf opts :token) (generate-authorization (getf opts :token))) (generate-authorization (get-access-token domain opts)))) @@ -134,7 +124,7 @@ ;; after reading more (loop :for item :across results :do (prompt-and-download domain authorization - item (getf opts :assume-yes))) + item opts)) (quit-with-message 0 "No results found for ~A" search-term))))) (error (e)