This repository has been archived by the owner on Jul 11, 2020. It is now read-only.
forked from ljgago/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 4
/
azuracast.go
66 lines (54 loc) · 1.47 KB
/
azuracast.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"encoding/json"
"net/http"
"log"
"io/ioutil"
"errors"
"strconv"
)
// Struct definitions
type AzuraCast struct {
base_url string
}
type AzuraCastApiResponse struct {
Station *AzuraCastStation `json:"station"`
NowPlaying struct {
Song *AzuraCastNowPlaying `json:"song"`
} `json:"now_playing"`
}
type AzuraCastStation struct {
ID int64 `json:"id"`
Name string `json:"name"`
ListenURL string `json:"listen_url"`
}
type AzuraCastNowPlaying struct {
ID string `json:"id"`
Text string `json:"text"`
Artist string `json:"artist"`
Title string `json:"title"`
Album string `json:"album"`
}
// Update now playing data using the already stored station ID.
func (ac *AzuraCast) UpdateNowPlaying(v *VoiceInstance) (error) {
return ac.GetNowPlaying(v, strconv.FormatInt(v.station.ID, 10))
}
// Get now playing data for the station specified.
func (ac *AzuraCast) GetNowPlaying(v *VoiceInstance, stationName string) (error) {
apiUrl := ac.base_url+"/api/nowplaying/"+stationName
resp, err := http.Get(apiUrl)
if err != nil {
log.Println("ERROR: AzuraCast Response", err.Error())
return err
}
if resp.StatusCode == http.StatusOK {
apiResponseBody, _ := ioutil.ReadAll(resp.Body)
var apiResponse AzuraCastApiResponse
json.Unmarshal(apiResponseBody, &apiResponse)
v.station = apiResponse.Station
v.np = apiResponse.NowPlaying.Song
return nil
} else {
return errors.New("API Url returned status"+resp.Status)
}
}