transmission.lisp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. (in-package :cl-user)
  2. (defpackage chatikbot.plugins.transmission
  3. (:use :cl :chatikbot.common))
  4. (in-package :chatikbot.plugins.transmission)
  5. (defsetting *transmission-settings* nil "ALIST of (chat-id . url)")
  6. (defsetting *transmission-locations* nil "ALIST of (name . location)")
  7. (defsetting *transmission-monitor* nil "LIST of chat-ids where to send status updates")
  8. (defvar *transmission-sessions* nil "ALIST of (url . x-transmission-session-id)")
  9. (defun transmission-get-session (url)
  10. (multiple-value-bind (body status headers)
  11. (handler-bind ((dex:http-request-conflict #'dex:ignore-and-continue))
  12. (http-request url))
  13. (declare (ignore body status))
  14. (gethash "x-transmission-session-id" headers)))
  15. (defun transmission-set-session (url session-id)
  16. (setf *transmission-sessions*
  17. (cons (cons url session-id)
  18. (remove url *transmission-sessions* :test #'equal :key #'car))))
  19. (defun transmission-request (url method &rest arguments)
  20. (let ((retries (getf arguments :retries 0)))
  21. (when (> retries 5)
  22. (error "Too many retries"))
  23. (remf arguments :retries)
  24. (let ((session-id (aget url *transmission-sessions*))
  25. (content
  26. (with-output-to-string (stream)
  27. (yason:encode (alexandria:plist-hash-table
  28. (list "method" (dekeyify method t)
  29. "arguments" (alexandria:plist-hash-table
  30. (loop for (key value) on arguments by #'cddr
  31. when value
  32. appending (list (dekeyify key) value)))))
  33. stream))))
  34. (handler-case
  35. (let* ((response (json-request url :method :post :content content
  36. :headers (list (cons :x-transmission-session-id session-id))))
  37. (result (aget "result" response)))
  38. (unless (equal "success" result)
  39. (error result))
  40. (aget "arguments" response))
  41. (dex:http-request-conflict (e)
  42. (transmission-set-session url (gethash "x-transmission-session-id" (dex:response-headers e)))
  43. (apply #'transmission-request url method :retries (1+ retries) arguments))))))
  44. (defun transmission-get-torrents (url &optional ids (fields '("id" "name" "status" "percentDone" "eta" "totalSize")))
  45. (aget "torrents" (transmission-request url :torrent-get :ids ids :fields fields)))
  46. (defun transmission-add-torrent (url &key filename metainfo)
  47. (let ((torrent-added (transmission-request url :torrent-add :filename filename :metainfo metainfo)))
  48. (car (transmission-get-torrents url (list (aget "id" (or (aget "torrent-added" torrent-added)
  49. (aget "torrent-duplicate" torrent-added))))))))
  50. (defun %format-torrent-status (status)
  51. (case status
  52. (0 "⏹") ;; Stopped
  53. (1 "🔂") ;; Check wait
  54. (2 "🔁") ;; Check
  55. (3 "⏸") ;; Queued to download
  56. (4 "▶️") ;; Downloading
  57. (5 "⏸") ;; Queued to seed
  58. (6 "⏺") ;; Seeding
  59. ))
  60. (defun %format-torrent (torrent)
  61. (let ((eta (aget "eta" torrent)))
  62. (format nil "~A ~A ~A (~A)~:[~; (~A%~@[, eta ~A~])~]"
  63. (aget "id" torrent)
  64. (%format-torrent-status (aget "status" torrent))
  65. (aget "name" torrent)
  66. (format-size (aget "totalSize" torrent))
  67. (eq 4 (aget "status" torrent))
  68. (smart-f (* 100 (aget "percentDone" torrent)) 1)
  69. (when (not (= -1 eta)) (format-interval eta)))))
  70. ;; Command handlers
  71. (def-message-cmd-handler handle-torrents (:torrents)
  72. (let ((url (aget *chat-id* *transmission-settings*)))
  73. (if url
  74. (let ((torrents (transmission-get-torrents url)))
  75. (bot-send-message (if torrents (format nil "~{~A~^~%~}" (mapcar #'%format-torrent torrents))
  76. "Пустой список!")))
  77. (bot-send-message "Бота настрой!"))))
  78. (defun %torrent-add-and-respond (&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 (%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 :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 :metainfo (cl-base64:usb8-array-to-base64-string
  102. (telegram-file-contents file-id))))))
  103. ;; Callback handlers
  104. (defun %handle-torrent-move (torrent-id name)
  105. (alexandria:when-let* ((url (aget *source-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 (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 (format nil "~{~A~^~%~}"
  128. (mapcar #'%format-torrent
  129. (transmission-get-torrents url updated))))))
  130. (setf (gethash *chat-id* *transmission-last-results*) new-result)))))