1
0

transmission.lisp 6.4 KB

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