summaryrefslogtreecommitdiff
path: root/scripts/rh-client.in
blob: 64dfde65d62cdd5cdb07fe10c2a49ddb5855d03f (plain)
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
#!/usr/bin/env guile
!#
(use-modules (simple-zmq)
	     (config)
	     (config api)
	     (config parser sexp)
	     (config licenses)
	     (json))

(define config
  (configuration
   (name 'rh-client)
   (synopsis "client for reading-heap")
   (keywords
    (list
     (setting
      (name 'service-socket)
      (synopsis "socket for communication with the service")
      (default (string-append "ipc://" (or (getenv "XDG_RUNTIME_DIR")
					   "/tmp")
			      "/reading-heap.sock")))
     (switch
      (name 'write)
      (synopsis "write the default config")
      (default #f)
      (test boolean?)
      (character #f))
     (switch
      (name 'title)
      (synopsis "title of the new media")
      (default "Foundation Trilogy")
      (test string?)
      (character #t))
     (switch
      (name 'author)
      (synopsis "author of the new media")
      (default "Isaac Asimov")
      (test string?)
      (character #t))
     (switch
      (name 'location)
      (synopsis "location of the new media")
      (default "https://openlibrary.org/books/OL20930675M/The_foundation_trilogy")
      (test string?)
      (character #t))
     (switch
      (name 'priority)
      (synopsis "priority of the new media")
      (default "1")
      (test string?)
      (character #t))))
   (subcommands
    (list
     (configuration
      (name 'next)
      (synopsis "get the next media to consume")
      (wanted '((keywords . (service-socket)))))
     (configuration
      (name 'new)
      (synopsis "add new media to the heap")
      (wanted '((keywords . (service-socket title author location priority)))))
     (configuration
      (name 'consume)
      (synopsis "remove the next media from the heap")
      (wanted '((keywords . (service-socket)))))))
   (directory (list (in-home ".reading-heap/")
		    (path (given
			   (string-append (or (getenv "XDG_CONFIG_HOME")
					      (string-append (getenv "HOME")
							     "/.config"))
					  "/reading-heap/"))
			  (eager? #t))))
   (parser sexp-parser)))


(define context (zmq-create-context))
(define client-socket (zmq-create-socket context ZMQ_REQ))

(define (client-setup sock)
  (zmq-connect client-socket sock))
  
(define (rh-client-next)
  (zmq-send client-socket "next")
  (let ((msg (zmq-receive-bytevector client-socket 1500)))
    (display (bv->string msg))))

(define (rh-client-new json)
  (zmq-send client-socket "new")
  (zmq-receive client-socket 1500)
  (zmq-send client-socket json)
  (display (zmq-receive client-socket 1500))
  (zmq-send client-socket "next")
  (display (zmq-receive client-socket 1500)))

(define (rh-client-consumed)
  (zmq-send client-socket "consume")
  (display (zmq-receive client-socket 1500)))

(define (json-from-args priority title author location)
  (scm->json-string `(("priority" . ,priority)
		      ("title" . ,title)
		      ("author" . ,author)
		      ("location" . ,location))))

(define (main cmd-line)
  (let ((options (getopt-config-auto cmd-line config)))
    (when (option-ref options 'write)
      (options-write options))
    (cond ((string=? (cadr (full-command options)) "next")
	   (client-setup (option-ref options 'service-socket))
	   (rh-client-next))
	  ((string=? (cadr (full-command options)) "new")
	   (client-setup (option-ref options 'service-socket))
	   (rh-client-new (json-from-args (string->number (option-ref options 'priority))
					  (option-ref options 'title)
					  (option-ref options 'author)
					  (option-ref options 'location))))
	  ((string=? (cadr (full-command options)) "consume")
	   (client-setup (option-ref options 'service-socket))
	   (rh-client-consumed))
	  (#t (emit-help options)))))

(main (command-line))