mpvd

Control mpv using the MPD protocol

git clone https://git.8pit.net/mpvd.git

 1(import json)
 2(require [hy.contrib.walk [let]])
 3
 4(setv DELIMITER "\n")
 5
 6(defclass ServerMsg [object]
 7  (defn --init-- [self input]
 8    (setv self.dict (json.loads input)))
 9
10  (defn get-id [self]
11    (if (self.event?)
12      (raise (ValueError "events don't have ids"))
13      (if (in "request_id" self.dict)
14        (get self.dict "request_id")
15        None)))
16
17  (defn get-data [self]
18    (if (in "data" self.dict)
19      (get self.dict "data")
20      None))
21
22  (defn event? [self]
23    (in "event" self.dict))
24
25  (defn error? [self]
26    (if (self.event?)
27      False
28      (not (= (get self.dict "error") "success")))))
29
30(defclass ClientMsg [object]
31  (defn --init-- [self name &optional [args []] [id None]]
32    (if (and id (not (isinstance id int)))
33      (raise (TypeError "Request ID must be an int")))
34    (setv self.name name)
35    (setv self.args args)
36    (setv self.reqid id))
37
38  (defn to-json [self]
39    (let [dict {"command" (+ [self.name] self.args)}]
40      (json.dumps (if (is self.reqid None)
41                      dict
42                      (do (assoc dict "request_id" self.reqid) dict)))))
43
44  (defn to-bytes [self]
45    (.encode (+ (self.to-json) DELIMITER))))