This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.scm
106 lines (90 loc) · 3.97 KB
/
example.scm
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
(import chicken.pretty-print)
(import chicken.process-context)
(import chicken.repl)
(import chicken.string)
(import optimism)
(import srfi-1)
(import srfi-42)
(import transmission)
(import transmission.utils)
(define (torrent-get-example)
(display "torrent-get-example: ")
(print-result (torrent-get '("id" "totalSize") #:tag (unique-tag))))
(define (session-get-example)
(display "session-get-example: ")
(print-result (session-get #:tag (unique-tag))))
(define (print-result result)
(when result ; rpc-call returns #f on wrong parameters
(let ((reply (vector->list result)))
(let ((arguments (cdr (assoc "arguments" reply)))
(result (cdr (assoc "result" reply))))
(if (string=? result "success")
(pp arguments)
(pp result))))))
(define (init args)
(define *OPTS*
`(((--host) . host)
((--port) . ,string->number)
((--authenv -ne))
((--username) . username)
((--password) . password)
((--session-id) . session-id)
((--repl))))
(let ((pargs (parse-command-line args *OPTS*)))
(let ((host (alist-ref '--host pargs))
(port (alist-ref '--port pargs))
(authenv (or (alist-ref '--auth-env pargs) (alist-ref '-ne pargs)))
(username (alist-ref '--username pargs))
(password (alist-ref '--password pargs))
(session-id (alist-ref '--session-id pargs))
(repl? (not (not (assoc '--repl pargs)))))
(when host (*host* host))
(when port (*port* port))
(when session-id (*session-id* session-id))
; Update authentication variables
(cond
(authenv
(let ((tr_auth (get-environment-variable "TR_AUTH")))
(unless tr_auth
(eprint "TR_AUTH not set"))
(let ((username/password (string-split tr_auth ":")))
(unless (= (length username/password) 2)
(eprint "Splitting TR_AUTH by ':' didn't result in 2 elements; authentication will most likely fail."))
(let ((username (car username/password))
(password (cadr username/password)))
(*username* username)
(*password* password)))))
((or username password)
(unless (and username password)
(eprint "Did you forget to set both username and password? Authentication may fail."))
(when username (*username* username))
(when password (*password* password))))
repl?)))
(define (main args)
(if (init args)
(repl)
(with-transmission-result
(result-bind
(torrent-get '("id" "downloadDir" "status" "name") #:ids #f #:tag (unique-tag))
(lambda (arguments . _)
(alist-let/and arguments (torrents)
(let ((ids (list-ec (:vector tor torrents)
(:let status (reply-ref 'status tor))
(:let download-dir (reply-ref 'downloadDir tor))
(and (member status `(,status/seed ,status/seed-wait))
(string=? download-dir "/some/path/to/files/"))
(:let id (reply-ref 'id tor))
id)))
(torrent-get '("id" "uploadRatio") #:ids ids #:tag (unique-tag)))))
(lambda (arguments . _)
(alist-let/and arguments (torrents)
(result/ok (list-ec (:vector tor torrents)
(:let upload-ratio (reply-ref 'uploadRatio tor))
(if (> upload-ratio 2))
(:let id (reply-ref 'id tor))
id)))))
(lambda (ids)
(pp ids))
(lambda (result . _)
(print "something failed: " result)))))
(main (command-line-arguments))