transmission.lisp 7.1 KB

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