/
rtorrent-controller.lisp
256 lines (216 loc) · 8.41 KB
/
rtorrent-controller.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
;;; -*- Mode: Lisp -*-
;;; This software is in the public domain and is
;;; provided with absolutely no warranty.
(in-package #:rtorrent-controller)
(defvar *host* "localhost")
(defun call-rtorrent (method &rest arguments)
(s-xml-rpc:xml-rpc-call (apply #'s-xml-rpc:encode-xml-rpc-call
method arguments)
:host *host*))
(defun list-files (torrent)
(mapcar #'car
(call-rtorrent "f.multicall" torrent
"" "f.path=")))
(defun list-files-with-size (torrent)
(call-rtorrent "f.multicall" torrent
"" "f.get_path="
"f.get_size_bytes="
"f.get_priority=" ))
(defun list-torrents ()
(call-rtorrent "d.multicall" ""
"d.get_hash="
"d.get_name="
"d.get_left_bytes="
"d.get_state="
"d.get_down_rate="))
(defun enabled-files-finished-p (torrent)
(loop for (priority chunks completed-chunks) in
(call-rtorrent "f.multicall" torrent ""
"f.priority="
"f.size_chunks="
"f.completed_chunks=")
always (or (zerop priority)
(= chunks completed-chunks))))
(defun list-finished-torrents ()
(let ((torrents (call-rtorrent "d.multicall2" "" ""
"d.hash="
"d.base_path="
"d.left_bytes=")))
(loop for (hash path size-left) in torrents
when (and (not (equal path ""))
(or (zerop size-left)
(enabled-files-finished-p hash)))
collect path)))
(defun stop (hash)
(call-rtorrent "d.stop" hash))
(defun start (hash)
(call-rtorrent "d.start" hash))
(defun erase (hash)
(call-rtorrent "d.erase" hash))
(defun multicall (&rest args)
(call-rtorrent "system.multicall"
(loop for (method . parameters) in args
collect
(s-xml-rpc:xml-rpc-struct "methodName" method "params"
(or parameters
#())))))
(defun format-bytes (bytes)
(loop for unit in '("" "KB" "MB" "GB" "TB")
for prev = 1 then multiplier
for multiplier = 1024 then (* multiplier 1024)
when (< bytes multiplier)
return (format nil "~$ ~a" (/ bytes prev) unit)))
(defun process-list (functions list)
(loop for x in list
for i from 0
collect (funcall (getf functions i #'identity)
x)))
(defun process-lists (functions lists)
(loop for list in lists
collect (process-list functions list)))
(defun state-description (n)
(ecase n
(0 "Stopped")
(1 "Started")))
(defun priority-description (n)
(ecase n
(0 "Off")
(1 "Normal")
(2 "High")))
(defun status ()
(mapcar #'car (multicall '("get_up_rate") '("get_down_rate")
'("get_upload_rate") '("get_download_rate"))))
(defun set-download-rate (value)
(call-rtorrent "set_download_rate" value))
(defun set-upload-rate (value)
(call-rtorrent "set_upload_rate" value))
;;;
(defvar *useful-files* '("flac" "cue" "mp3" "ape" "wv" "m4a" "ac3"
"pdf" "epub" "djvu"
"avi" "mkv" "mp4" "vob" "ifo" "bup" "mov"))
(defun useful-file-p (filename)
(member (pathname-type filename) *useful-files*
:test #'string-equal))
(defun load-torrent (filename &key start)
(call-rtorrent (if start
"load.start"
"load.normal") "" filename))
(defun disable-not-needed-files (torrent)
(loop for index from 0
for file in (list-files torrent)
unless (useful-file-p file)
do (call-rtorrent "f.priority.set" (format nil "~a:f~a" torrent index) 0)))
(defun disable-last-torrent (&optional (n 1))
(disable-not-needed-files
(car (last (call-rtorrent "download_list" "") n))))
(defun run-program (program args)
#+sbcl
(sb-ext:run-program program args
:search t)
#+ccl
(ccl:run-program program args)
#-(or sbcl ccl)
(error "Not supported"))
(defun remove-file (filename)
(handler-case (delete-file filename)
(file-error ())))
(defun native-namestring (file)
#+sbcl (sb-ext:native-namestring file)
#+ccl (ccl:native-translated-namestring file)
#-(or sbcl ccl) (error "Not supported"))
(defun move-file (filename)
(run-program "rsync" (list "-az"
"--delay-updates"
(native-namestring filename)
"desktop:/tmp/"))
(remove-file filename))
(defun process-torrent (filename)
(format t "Loading ~a~%" filename)
(if (equal (machine-instance) "debian")
(let ((namestring (remove #\\ (namestring filename))))
(load-torrent namestring :start (equal "v" (pathname-name filename)))
(disable-last-torrent)
(remove-file filename))
(move-file filename)))
(defun process-subtitle (filename)
(format t "Loading ~a~%" filename)
(cond ((equal (machine-instance) "desktop")
(let ((new-path (namestring (merge-pathnames
(make-pathname
:directory '(:relative "sub")
:defaults filename)
(user-homedir-pathname)))))
(run-program "mv"
(list (namestring filename) new-path))
(run-program "rj" (list new-path))))
(t
(move-file filename))))
(defun process-zip-subtitle (filename)
(format t "Loading ~a~%" filename)
(cond ((equal (machine-instance) "desktop")
(run-program "us"
(list
(namestring filename))))
(t
(move-file filename))))
(defun load-existing-torrents (directory)
(mapcar #'process-torrent
(directory (merge-pathnames "*.torrent" directory))))
(defun load-existing-subtitles (directory)
(let ((sub (probe-file (merge-pathnames "g.zip" directory))))
(when sub
(process-zip-subtitle sub))))
#+linux
(defun inotify-loop (directory)
(load-existing-torrents directory)
(load-existing-subtitles directory)
(inotify:with-inotify (inot `((,directory ,inotify:in-moved-to)))
(write-line "Waiting for files.")
(loop
(dolist (event (inotify:read-events inot))
(handler-case
(let* ((path (inotify:event-full-name event))
(type (pathname-type path)))
(cond ((equal type "torrent")
(process-torrent path))
((and (equal type "zip")
(equal (pathname-name path) "g"))
(process-zip-subtitle path))
((equal type "srt")
(process-subtitle path))))
(error (e)
(warn "An error ~a occured while processing event ~s"
e event)))))))
#+darwin
(defun kqueue-loop (directory)
(let ((wild-directory (make-pathname :defaults directory
:name :wild
:type :wild)))
(kqueue:with-kqueue (kqueue `((,directory ,kqueue:note-write)))
(loop
(dolist (event (kqueue:read-events kqueue))
(handler-case
(loop for path in (directory wild-directory)
for type = (pathname-type path)
do
(cond ((equal type "torrent")
(process-torrent path))
((and (equal type "zip")
(equal (pathname-name path) "g"))
(process-zip-subtitle path))
((equal type "srt")
(process-subtitle path))))
(error (e)
(warn "An error ~a occured while processing event ~s"
e event))))))))
(defun fs-loop (&key (directory #-darwin #p"/tmp/"
#+darwin (merge-pathnames #p"Downloads/"
(user-homedir-pathname))))
(load-existing-torrents directory)
(load-existing-subtitles directory)
#+linux (inotify-loop directory)
#+darwin (kqueue-loop directory))
#+(or)
(sb-ext:save-lisp-and-die "rtr-controller"
:toplevel #'rtorrent-controller:fs-loop
:executable t)