Innocenty Enikeew 9 лет назад
Родитель
Сommit
59ade31be1
1 измененных файлов с 18 добавлено и 0 удалено
  1. 18 0
      plugins/transmission.lisp

+ 18 - 0
plugins/transmission.lisp

@@ -2,6 +2,7 @@
 
 (defsetting *transmission-settings* nil "ALIST of (chat-id . url)")
 (defsetting *transmission-locations* nil "ALIST of (name . location)")
+(defsetting *transmission-monitor* nil "LIST of chat-ids where to send status updates")
 
 (defvar *transmission-sessions* nil "ALIST of (url . x-transmission-session-id)")
 
@@ -75,6 +76,7 @@
             (smart-f (* 100 (aget "percentDone" torrent)) 1)
             (when (not (= -1 eta)) (format-interval eta)))))
 
+;; Command handlers
 (def-message-cmd-handler handle-torrents (:torrents)
   (let ((url (aget chat-id *transmission-settings*)))
     (if url
@@ -111,6 +113,7 @@
       (%torrent-add-and-respond chat-id :metainfo (cl-base64:usb8-array-to-base64-string
                                                    (telegram-file-contents file-id))))))
 
+;; Callback handlers
 (defun %handle-torrent-move (query-id chat-id torrent-id name)
   (alexandria:when-let* ((url (aget chat-id *transmission-settings*))
                          (location (aget name *transmission-locations*)))
@@ -125,3 +128,18 @@
       (split-sequence:split-sequence #\- data :count 3)
     (case (intern (string-upcase type) "KEYWORD")
       (:l (%handle-torrent-move query-id chat-id (parse-integer id) val)))))
+
+;; Cron
+(defvar *transmission-last-results* (make-hash-table) "Last check results for each chat-id")
+(defcron process-transmission ()
+  (dolist (chat-id *transmission-monitor*)
+    (alexandria:when-let (url (aget chat-id *transmission-settings*))
+      (let ((old-result (gethash chat-id *transmission-last-results*))
+            (new-result (loop for torrent in (transmission-get-torrents url nil '("id" "status"))
+                           collect (cons (aget "id" torrent) (aget "status" torrent)))))
+        (when old-result
+          (alexandria:when-let (updated (mapcar #'car (set-difference new-result old-result :test #'equal)))
+            (bot-send-message chat-id (format nil "~{~A~^~%~}"
+                                              (mapcar #'%format-torrent
+                                                      (transmission-get-torrents url updated))))))
+        (setf (gethash chat-id *transmission-last-results*) new-result)))))