From 3b7cc47c8fac3b7e00a90af4775dc5cc6b395257 Mon Sep 17 00:00:00 2001 From: cage Date: Sun, 17 Oct 2021 18:11:50 +0200 Subject: [PATCH] - prompted for older threshold instead of using a constant. --- scripts/delete-old-posts.lisp | 70 ++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/scripts/delete-old-posts.lisp b/scripts/delete-old-posts.lisp index dc714a7..77c0161 100644 --- a/scripts/delete-old-posts.lisp +++ b/scripts/delete-old-posts.lisp @@ -25,11 +25,11 @@ (in-package :scripts) -(define-constant +min-past-date+ "2021-08-01" :test #'string=) +(defparameter *min-past-date* nil) -(defparameter *date-delete-treshold* "2021-10-01") +(defparameter *date-delete-treshold* nil) -(defun decode-date (post) +(defun decode-post-date (post) (multiple-value-bind (x y z day month year) (decode-universal-time (tooter:created-at post) 0) (declare (ignore x y z)) @@ -44,19 +44,20 @@ (string< a b)) (defun list-posts (min-past-date show-progress &optional (max-id nil) (accum ())) - (let* ((username (swconf:config-username)) - (user-id (db:acct->id username)) - (posts (api-client:get-timeline (ui::timeline->kind db:+home-timeline+) - :local t - :max-id max-id - :limit 10)) - (sorted-posts (api-client:sort-id< posts)) - (min-post-id (tooter:id (first sorted-posts))) - (min-post-date (decode-date (first sorted-posts))) - (my-posts (remove-if-not (lambda (a) - (string= (tooter:id (tooter:account a)) - user-id)) - posts))) + (let* ((username (swconf:config-username)) + (user-id (db:acct->id username)) + (timeline-type (ui::timeline->kind db:+home-timeline+)) + (posts (api-client:get-timeline timeline-type + :local t + :max-id max-id + :limit 10)) + (sorted-posts (api-client:sort-id< posts)) + (min-post-id (tooter:id (first sorted-posts))) + (min-post-date (decode-post-date (first sorted-posts))) + (my-posts (remove-if-not (lambda (a) + (string= (tooter:id (tooter:account a)) + user-id)) + posts))) (if (and posts (date>= min-post-date min-past-date)) (progn @@ -70,18 +71,37 @@ (client:authorize) (format t "This client has been authorized.~%") (format t "Please provide the post's maximum creation date.~%") - (format t "Posts with date older then the provided one will be deleted~%") + (format t "Posts with date older than the provided one will be deleted~%") (write-string "Maximum date (format \"YYY-MM-DD\"): ") (finish-output) (let* ((input-date (read-line)) (*date-delete-treshold* (db-utils:encode-datetime-string input-date))) - (when (yes-or-no-p "deleting posts older than ~s. Continue?" input-date) - (if *date-delete-treshold* - (let ((posts (list-posts +min-past-date+ t))) - (loop for post in posts when (date< (decode-date post) *date-delete-treshold*) do - (let ((post-contents (with-output-to-string (stream) (tooter::present post stream)))) - (format t "deleting~2%~a~%" post-contents) - (api-client:delete-status (tooter:id post)))))) - (format *error-output* "Date ~s is not valid, exiting.~%" input-date))))) + (format t "Please provide the post's minimum creation date.~%") + (format t "Posts with date older than the provided one will *not* be deleted~%") + (write-string "Minimum date (format \"YYY-MM-DD\"): ") + (finish-output) + (let* ((min-input-date (read-line)) + (*min-past-date* (db-utils:encode-datetime-string min-input-date))) + (when (yes-or-no-p "Deleting posts older than ~s and newer than ~s. Continue?" + input-date + min-input-date) + (cond + ((null *date-delete-treshold*) + (format *error-output* "Date ~s is not valid, exiting.~%" input-date)) + ((null *min-past-date*) + (format *error-output* "Date ~s is not valid, exiting.~%" min-input-date)) + (t + (let ((posts (list-posts (db-utils:decode-date-string *min-past-date*) t))) + (format t "Start deleting...~%") + (loop for post in posts + when (date< (decode-post-date post) + (db-utils:decode-date-string *date-delete-treshold*)) + do + (let ((post-contents (with-output-to-string (stream) + (tooter::present post stream)))) + (format t "deleting~2%~a~%" post-contents)))))))))) + +; (api-client:delete-status (tooter:id post))))))))))) + (main)