diff options
author | Simon Garrelou <simon.garrelou@gmail.com> | 2022-12-08 22:35:17 +0100 |
---|---|---|
committer | Simon Garrelou <simon.garrelou@gmail.com> | 2022-12-08 22:35:17 +0100 |
commit | 7e4333dac70cdb003e71b6805ed4d81f18aa233a (patch) | |
tree | 26190c147141f765d69880d98d891bf50071e891 /music/patch.go | |
parent | 96cc5db2b4062ced82faf01ddac24abef04df343 (diff) | |
download | termsonic-7e4333dac70cdb003e71b6805ed4d81f18aa233a.tar.gz termsonic-7e4333dac70cdb003e71b6805ed4d81f18aa233a.zip |
Music playback working
Diffstat (limited to 'music/patch.go')
-rw-r--r-- | music/patch.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/music/patch.go b/music/patch.go new file mode 100644 index 0000000..aba20ec --- /dev/null +++ b/music/patch.go | |||
@@ -0,0 +1,76 @@ | |||
1 | package music | ||
2 | |||
3 | import ( | ||
4 | "encoding/xml" | ||
5 | "fmt" | ||
6 | "io" | ||
7 | "io/ioutil" | ||
8 | "net/url" | ||
9 | "strings" | ||
10 | |||
11 | "github.com/delucks/go-subsonic" | ||
12 | "github.com/jfbus/httprs" | ||
13 | ) | ||
14 | |||
15 | // Stream2 patches subsonic.Client.Stream to return a ReadCloser for use with "beep" | ||
16 | func Stream2(s *subsonic.Client, id string, parameters map[string]string) (io.ReadCloser, error) { | ||
17 | params := url.Values{} | ||
18 | params.Add("id", id) | ||
19 | for k, v := range parameters { | ||
20 | params.Add(k, v) | ||
21 | } | ||
22 | response, err := s.Request("GET", "stream", params) | ||
23 | if err != nil { | ||
24 | return nil, err | ||
25 | } | ||
26 | contentType := response.Header.Get("Content-Type") | ||
27 | if strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml") { | ||
28 | // An error was returned | ||
29 | responseBody, err := ioutil.ReadAll(response.Body) | ||
30 | if err != nil { | ||
31 | return nil, err | ||
32 | } | ||
33 | resp := subsonic.Response{} | ||
34 | err = xml.Unmarshal(responseBody, &resp) | ||
35 | if err != nil { | ||
36 | return nil, err | ||
37 | } | ||
38 | if resp.Error != nil { | ||
39 | err = fmt.Errorf("Error #%d: %s\n", resp.Error.Code, resp.Error.Message) | ||
40 | } else { | ||
41 | err = fmt.Errorf("An error occurred: %#v\n", resp) | ||
42 | } | ||
43 | return nil, err | ||
44 | } | ||
45 | |||
46 | return httprs.NewHttpReadSeeker(response), nil | ||
47 | } | ||
48 | |||
49 | func Download2(s *subsonic.Client, id string) (io.ReadCloser, error) { | ||
50 | params := url.Values{} | ||
51 | params.Add("id", id) | ||
52 | response, err := s.Request("GET", "download", params) | ||
53 | if err != nil { | ||
54 | return nil, err | ||
55 | } | ||
56 | contentType := response.Header.Get("Content-Type") | ||
57 | if strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml") { | ||
58 | // An error was returned | ||
59 | responseBody, err := ioutil.ReadAll(response.Body) | ||
60 | if err != nil { | ||
61 | return nil, err | ||
62 | } | ||
63 | resp := subsonic.Response{} | ||
64 | err = xml.Unmarshal(responseBody, &resp) | ||
65 | if err != nil { | ||
66 | return nil, err | ||
67 | } | ||
68 | if resp.Error != nil { | ||
69 | err = fmt.Errorf("Error #%d: %s\n", resp.Error.Code, resp.Error.Message) | ||
70 | } else { | ||
71 | err = fmt.Errorf("An error occurred: %#v\n", resp) | ||
72 | } | ||
73 | return nil, err | ||
74 | } | ||
75 | return httprs.NewHttpReadSeeker(response), nil | ||
76 | } | ||