1
0

transmission.lisp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. (in-package #:chatikbot)
  2. (defsetting *transmission-settings* nil "ALIST of (chat-id . url)")
  3. (defsetting *transmission-locations* nil "ALIST of (name . location)")
  4. (defsetting *transmission-monitor* nil "LIST of chat-ids where to send status updates")
  5. (defvar *transmission-sessions* nil "ALIST of (url . x-transmission-session-id)")
  6. (defun transmission-get-session (url)
  7. (multiple-value-bind (body status headers uri stream)
  8. (drakma:http-request url :force-binary t :decode-content t)
  9. (declare (ignore body status uri stream))
  10. (aget :x-transmission-session-id headers)))
  11. (defun transmission-set-session (url session-id)
  12. (set-setting '*transmission-sessions*
  13. (cons (cons url session-id)
  14. (remove url *transmission-sessions* :test #'equal :key #'car))))
  15. (defun transmission-request (url method &rest arguments)
  16. (let ((session-id (aget url *transmission-sessions*))
  17. (content
  18. (with-output-to-string (stream)
  19. (yason:encode (alexandria:plist-hash-table
  20. (list "method" (dekeyify method t)
  21. "arguments" (alexandria:plist-hash-table
  22. (loop for (key value) on arguments by #'cddr
  23. when value
  24. appending (list (dekeyify key) value)))))
  25. stream))))
  26. (multiple-value-bind (stream status headers uri http-stream)
  27. (drakma:http-request url :method :post :content content :content-type "application/json"
  28. :force-binary t :want-stream t :decode-content t
  29. :additional-headers (list (cons :x-transmission-session-id session-id)))
  30. (declare (ignore uri))
  31. (ecase status
  32. (200 (unwind-protect
  33. (progn
  34. (setf (flex:flexi-stream-external-format stream) :utf-8)
  35. (let* ((response (yason:parse stream :object-as :alist))
  36. (result (aget "result" response)))
  37. (unless (equal "success" result)
  38. (error result))
  39. (aget "arguments" response)))
  40. (ignore-errors (close http-stream))))
  41. (409 (transmission-set-session url (aget :x-transmission-session-id headers))
  42. (apply #'transmission-request url method arguments))))))
  43. (defun transmission-get-torrents (url &optional ids (fields '("id" "name" "status" "percentDone" "eta" "totalSize")))
  44. (aget "torrents" (transmission-request url :torrent-get :ids ids :fields fields)))
  45. (defun transmission-add-torrent (url &key filename metainfo)
  46. (let ((torrent-added (transmission-request url :torrent-add :filename filename :metainfo metainfo)))
  47. (car (transmission-get-torrents url (list (aget "id" (or (aget "torrent-added" torrent-added)
  48. (aget "torrent-duplicate" torrent-added))))))))
  49. (defun %format-torrent-status (status)
  50. (case status
  51. (0 "⏹") ;; Stopped
  52. (1 "🔂") ;; Check wait
  53. (2 "🔁") ;; Check
  54. (3 "⏸") ;; Queued to download
  55. (4 "▶️") ;; Downloading
  56. (5 "⏸") ;; Queued to seed
  57. (6 "⏺") ;; Seeding
  58. ))
  59. (defun %format-torrent (torrent)
  60. (let ((eta (aget "eta" torrent)))
  61. (format nil "~A ~A ~A (~A)~:[~; (~A%~@[, eta ~A~])~]"
  62. (aget "id" torrent)
  63. (%format-torrent-status (aget "status" torrent))
  64. (aget "name" torrent)
  65. (format-size (aget "totalSize" torrent))
  66. (eq 4 (aget "status" torrent))
  67. (smart-f (* 100 (aget "percentDone" torrent)) 1)
  68. (when (not (= -1 eta)) (format-interval eta)))))
  69. ;; Command handlers
  70. (def-message-cmd-handler handle-torrents (:torrents)
  71. (let ((url (aget chat-id *transmission-settings*)))
  72. (if url
  73. (let ((torrents (transmission-get-torrents url)))
  74. (bot-send-message chat-id
  75. (if torrents (format nil "~{~A~^~%~}" (mapcar #'%format-torrent torrents))
  76. "Пустой список!")))
  77. (bot-send-message chat-id "Бота настрой!"))))
  78. (defun %torrent-add-and-respond (chat-id &key filename metainfo)
  79. (alexandria:when-let (url (aget chat-id *transmission-settings*))
  80. (let* ((new-torrent (transmission-add-torrent url :filename filename :metainfo metainfo))
  81. (markup (loop for (name . location) in *transmission-locations*
  82. collect (list :text (format nil "💾 ~A" name)
  83. :callback-data (encode-callback-data
  84. chat-id :tm
  85. (format nil "l-~A-~A" name
  86. (aget "id" new-torrent))
  87. 86400)))))
  88. (bot-send-message chat-id (%format-torrent new-torrent)
  89. :reply-markup (and markup (telegram-inline-keyboard-markup (list markup)))))))
  90. (defparameter +magnet-regex+ (ppcre:create-scanner "magnet:\\?\\S+"))
  91. (def-message-handler handle-magnet (message)
  92. (alexandria:when-let (magnet (ppcre:scan-to-strings +magnet-regex+ text))
  93. (%torrent-add-and-respond chat-id :filename magnet)))
  94. (def-message-handler handle-torrent (message)
  95. (alexandria:when-let* ((url (aget chat-id *transmission-settings*))
  96. (doc (aget "document" message))
  97. (file-name (aget "file_name" doc))
  98. (file-id (aget "file_id" doc)))
  99. (when (and (equal "torrent" (pathname-type (pathname file-name)))
  100. (< (aget "file_size" doc) (* 512 1024)))
  101. (%torrent-add-and-respond chat-id :metainfo (cl-base64:usb8-array-to-base64-string
  102. (telegram-file-contents file-id))))))
  103. ;; Callback handlers
  104. (defun %handle-torrent-move (query-id chat-id torrent-id name)
  105. (alexandria:when-let* ((url (aget chat-id *transmission-settings*))
  106. (location (aget name *transmission-locations*)))
  107. (transmission-request url :torrent-set-location
  108. :ids torrent-id
  109. :location location
  110. :move t)
  111. (telegram-answer-callback-query query-id :text (format nil "Moved to '~A'" location))))
  112. (def-callback-section-handler cb-handle-tm (:tm)
  113. (destructuring-bind (type val id)
  114. (split-sequence:split-sequence #\- data :count 3)
  115. (case (intern (string-upcase type) "KEYWORD")
  116. (:l (%handle-torrent-move query-id chat-id (parse-integer id) val)))))
  117. ;; Cron
  118. (defvar *transmission-last-results* (make-hash-table) "Last check results for each chat-id")
  119. (defcron process-transmission ()
  120. (dolist (chat-id *transmission-monitor*)
  121. (alexandria:when-let (url (aget chat-id *transmission-settings*))
  122. (let ((old-result (gethash chat-id *transmission-last-results*))
  123. (new-result (loop for torrent in (transmission-get-torrents url nil '("id" "status"))
  124. collect (cons (aget "id" torrent) (aget "status" torrent)))))
  125. (when old-result
  126. (alexandria:when-let (updated (mapcar #'car (set-difference new-result old-result :test #'equal)))
  127. (bot-send-message chat-id (format nil "~{~A~^~%~}"
  128. (mapcar #'%format-torrent
  129. (transmission-get-torrents url updated))))))
  130. (setf (gethash chat-id *transmission-last-results*) new-result)))))