/
wuvt.go
111 lines (98 loc) · 2.61 KB
/
wuvt.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/nlopes/slack"
)
const trackmanURL = "https://trackman-fm.apps.wuvt.vt.edu/api"
const djLinkURL = "https://www.wuvt.vt.edu/playlists/dj/%d"
const trackLinkURL = "https://www.wuvt.vt.edu/playlists/track/%d"
type TrackmanLatestTrackResponse struct {
Album string `json:"album"`
Artist string `json:"artist"`
DJ string `json:"dj"`
DJID int `json:"dj_id"`
Label string `json:"label"`
Title string `json:"title"`
Listeners int `json:"listeners"`
TrackID int `json:"id"`
}
func wuvtHandler(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get(trackmanURL + "/playlists/latest_track")
if err != nil {
log.Print(err)
http.Error(w, "Unable to load latest track information", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print(err)
http.Error(w, "Unable to load latest track information", http.StatusInternalServerError)
return
}
var track TrackmanLatestTrackResponse
err = json.Unmarshal(body, &track)
if err != nil {
log.Print(err)
http.Error(w, "Unable to load latest track information", http.StatusInternalServerError)
return
}
var djLink string
if track.DJID > 0 {
djLink = fmt.Sprintf("<%s|%s>", fmt.Sprintf(djLinkURL, track.DJID), track.DJ)
} else {
djLink = track.DJ
}
trackLink := fmt.Sprintf("<%s|%s>", fmt.Sprintf(trackLinkURL, track.TrackID), track.Title)
message := slack.NewBlockMessage(
slack.NewSectionBlock(
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf("*%s - %s*", track.Artist, trackLink),
false,
false,
),
[]*slack.TextBlockObject{
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf("Album: %s", track.Album),
false,
false,
),
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf("Label: %s", track.Label),
false,
false,
),
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf("DJ: %s", djLink),
false,
false,
),
slack.NewTextBlockObject(
slack.MarkdownType,
fmt.Sprintf("Listeners: %d", track.Listeners),
false,
false,
),
},
nil,
),
)
message.Msg.ResponseType = "in_channel"
message.Msg.Text = fmt.Sprintf("*%s - %s*\nDJ: %s", track.Artist, track.Title, djLink)
output, err := json.Marshal(message)
if err != nil {
log.Print(err)
http.Error(w, "Unable to marshal JSON response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(output)
}