-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstream.go
49 lines (41 loc) · 1 KB
/
stream.go
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
package radiko
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"path"
)
// URLItem represents a stream url.
type URLItem struct {
Areafree bool `xml:"areafree,attr"`
Item string `xml:",chardata"`
}
// GetStreamMultiURL returns a slice of the stream url.
func GetStreamMultiURL(stationID string) ([]URLItem, error) {
endpoint := path.Join(apiV2, "station/stream_multi",
fmt.Sprintf("%s.xml", stationID))
resp, err := http.Get("http://radiko.jp/" + endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
urlData := streamURLData{}
if err = xml.Unmarshal(b, &urlData); err != nil {
return nil, err
}
return urlData.Items, err
}
type streamURLData struct {
XMLName xml.Name `xml:"url"`
Items []URLItem `xml:"item"`
}
// GetLiveURL returns a live url for web browser.
func GetLiveURL(stationID string) string {
endpoint := path.Join("#!/live", stationID)
return defaultEndpoint + "/" + endpoint
}