aboutsummaryrefslogtreecommitdiff
path: root/music/patch.go
diff options
context:
space:
mode:
Diffstat (limited to 'music/patch.go')
-rw-r--r--music/patch.go76
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 @@
1package music
2
3import (
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"
16func 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
49func 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}