aboutsummaryrefslogtreecommitdiff
path: root/internal/pvfm/station/station.go
blob: f6e9cd6f39a3eaf9a5ad52674c9e6f07fad88568 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Package station grabs fallback data from the radio station.
*/
package station

import (
	"encoding/json"
	"flag"
	"io/ioutil"
	"net/http"
	"time"
)

var (
	latestInfo Wrapper

	bugTime = flag.Int("station-poke-delay", 15, "how stale the info can get")
)

type Wrapper struct {
	Age  time.Time
	Info Info
}

type Info struct {
	Icestats struct {
		Admin            string `json:"admin"`
		BannedIPs        int    `json:"banned_IPs"`
		Build            string `json:"build"`
		Host             string `json:"host"`
		Location         string `json:"location"`
		OutgoingKbitrate int    `json:"outgoing_kbitrate"`
		ServerID         string `json:"server_id"`
		ServerStart      string `json:"server_start"`
		StreamKbytesRead int    `json:"stream_kbytes_read"`
		StreamKbytesSent int    `json:"stream_kbytes_sent"`
		Source           []struct {
			Artist             string `json:"artist"`
			AudioBitrate       int    `json:"audio_bitrate,omitempty"`
			AudioChannels      int    `json:"audio_channels,omitempty"`
			AudioInfo          string `json:"audio_info"`
			AudioSamplerate    int    `json:"audio_samplerate,omitempty"`
			Bitrate            int    `json:"bitrate"`
			Connected          int    `json:"connected"`
			Genre              string `json:"genre"`
			IceBitrate         int    `json:"ice-bitrate,omitempty"`
			IncomingBitrate    int    `json:"incoming_bitrate"`
			ListenerPeak       int    `json:"listener_peak"`
			Listeners          int    `json:"listeners"`
			Listenurl          string `json:"listenurl"`
			MetadataUpdated    string `json:"metadata_updated"`
			OutgoingKbitrate   int    `json:"outgoing_kbitrate"`
			QueueSize          int    `json:"queue_size"`
			ServerDescription  string `json:"server_description"`
			ServerName         string `json:"server_name"`
			ServerType         string `json:"server_type"`
			ServerURL          string `json:"server_url"`
			StreamStart        string `json:"stream_start"`
			Subtype            string `json:"subtype,omitempty"`
			Title              string `json:"title"`
			TotalMbytesSent    int    `json:"total_mbytes_sent"`
			YpCurrentlyPlaying string `json:"yp_currently_playing"`
		} `json:"source"`
	} `json:"icestats"`
}

func GetStats() (Info, error) {
	now := time.Now()
	if now.Before(latestInfo.Age.Add(time.Second * time.Duration(*bugTime))) {
		return latestInfo.Info, nil
	}

	i := Info{}

	resp, err := http.Get("http://dj.bronyradio.com:8000/status-json.xsl")
	if err != nil {
		return Info{}, err
	}

	defer resp.Body.Close()

	content, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return Info{}, err
	}

	err = json.Unmarshal(content, &i)
	if err != nil {
		return Info{}, err
	}

	latestInfo.Info = i
	latestInfo.Age = now

	return latestInfo.Info, nil
}