Procházet zdrojové kódy

[music] Seeding watcher with setting.

Innokentiy Enikeev před 4 roky
rodič
revize
fad09d8d36
1 změnil soubory, kde provedl 42 přidání a 7 odebrání
  1. 42 7
      plugins/music.lisp

+ 42 - 7
plugins/music.lisp

@@ -4,7 +4,7 @@
 (in-package :chatikbot.plugins.music)
 
 (defvar *cookies* (cl-cookie:make-cookie-jar))
-(defvar *deluge-api* "http://hq2.chad-partners.com:8112/json")
+(defvar *deluge-api* "http://localhost:8112/json")
 (defvar *deluge-password* "chads")
 (defvar *deluge-request-id* 1)
 
@@ -61,6 +61,9 @@
 (defun deluge-get-torrents-status (filter fields)
   (deluge-request "core.get_torrents_status" (list filter fields)))
 
+(defun deluge-remove-torrent (ih &optional from-disk)
+  (deluge-request "core.remove_torrent" (list ih from-disk)))
+
 (defparameter +deluge-default-status-fields+ '("name" "state" "save_path" "files" "file_priorities" "total_size" "total_wanted" "total_done" "num_files"))
 (defun raw-pathname (pathspec)
   (values (cl-ppcre:regex-replace-all "\\[" pathspec "\\\\[")))
@@ -107,7 +110,7 @@
 
 (defun get-import-notify (info)
   (labels ((f (fl) (getf info fl)))
-    (format nil "[MUSIC] Importing *~a*, ~a tracks in ~a albums. Total size ~a"
+    (format nil "[[🎶]] Importing *~a*, ~a tracks in ~a albums. Total size ~a"
             (f :name) (f :wanted-files) (length (f :media-dirs)) (format-size (f :total-wanted)))))
 
 (defun get-root-paths (info)
@@ -129,8 +132,8 @@
         (uiop:run-program cmd))))
 
 (defun deluge-get-seeding-torrents ()
-  (loop for (torrent state) on (deluge-get-torrents-status '(:|state| "Seeding") '("name")) by #'cddr
-     collect (cons (string torrent) (getf state :|name|))))
+  (loop for (torrent state) on (deluge-get-torrents-status '(:|state| "Seeding") '("name" "total_wanted")) by #'cddr
+     collect (list (string torrent) (getf state :|name|) (getf state :|total_wanted|)))
 
 (defun deluge-pause-torrents (&rest torrents)
   (deluge-request "core.pause_torrent" (list torrents)))
@@ -159,8 +162,40 @@
     (send-admins notify dry)
     (deluge-pause-torrents ih)
     (deluge-delete-skipped ih dry)
-    (run-import paths dry)))
+    (run-import paths dry)
+    (if dry (format t "Removing torrent ~a" ih)
+      (deluge-remove-torrent ih))))
 
-(defvar *import-enabled* nil)
 (defun process-imports ()
-  )
+  (loop
+    (loop for (ih name size) in (deluge-get-seeding-torrents)
+	  do (handler-case (process-downloaded ih)
+	       (error (e) (log:error "Error processing torrent" name e))))
+    (sleep 1)))
+
+(defvar *watcher* nil "Importer thread ")
+(defvar *lock* (bordeaux-threads:make-recursive-lock "Music watch"))
+(defun ensure-watcher ()
+  (bt:with-recursive-lock-held (*lock*)
+    (if (lists-get :music-watch)
+        (unless *watcher*
+          (setf *watcher* (bt:make-thread 'process-imports :name "music watch")))
+        (when *watcher*
+          (bt:destroy-thread *watcher*)
+          (setf *watcher* nil))))
+  (values))
+
+(defun handle-set-watch (enable)
+  (lists-set-entry :music-watch *chat-id* enable)
+  (ensure-watcher)
+  (bot-send-message (if enable "[[🎶]] Импортим музло" "[[🎶]] Пусть мешки парятся")))
+
+(defun handle-status ())
+
+(def-message-cmd-handler handle-cmd-music (:music)
+  (cond
+    ((= 1 (length *args*))
+     (handle-set-watch (equal "on" (car *args*))))
+    (:otherwise (handle-status))))
+
+(add-hook :starting #'ensure-watcher)