-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.go
More file actions
132 lines (115 loc) · 2.92 KB
/
Copy pathprogram.go
File metadata and controls
132 lines (115 loc) · 2.92 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package radikron
import (
"encoding/xml"
"fmt"
"io"
"net/http"
)
// Prog contains the solicited program metadata
type Prog struct {
ID string
StationID string
Ft string
To string
Title string
Desc string
Info string
Pfm string
Tags []string
Genre ProgGenre
M3U8 string
RuleName string // name of the rule that matched this program
RuleFolder string // folder from the rule that matched this program
IsManualInjection bool // true if this program was manually injected
}
type ProgGenre struct {
Personality string
Program string
}
// Progs is a slice of Prog.
type Progs []*Prog
func (ps *Progs) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var xw XMLWeekly
if err := d.DecodeElement(&xw, &start); err != nil {
return err
}
stationID := xw.XMLStations.Station[0].StationID
for _, p := range xw.XMLStations.Station[0].Progs.Prog {
prog := &Prog{
ID: p.ID,
StationID: stationID,
Ft: p.Ft,
To: p.To,
Title: p.Title,
Desc: p.Desc,
Info: p.Info,
Pfm: p.Pfm,
M3U8: "",
}
prog.Genre = ProgGenre{
Personality: p.Genre.Personality.Name,
Program: p.Genre.Program.Name,
}
for _, t := range p.Tag.Item {
prog.Tags = append(prog.Tags, t.Name)
}
*ps = append(*ps, prog)
}
return nil
}
// XMLProg contains the raw program metadata
type XMLProg struct {
ID string `xml:"id,attr"`
Ft string `xml:"ft,attr"`
To string `xml:"to,attr"`
Title string `xml:"title"`
Desc string `xml:"desc"`
Info string `xml:"info"`
Pfm string `xml:"pfm"`
Tag struct {
Item []XMLProgItem `xml:"item"`
} `xml:"tag"`
Genre struct {
Personality XMLProgItem `xml:"personality"`
Program XMLProgItem `xml:"program"`
} `xml:"genre"`
}
type XMLProgs struct {
Date string `xml:"date"`
Prog []*XMLProg `xml:"prog"`
}
type XMLProgItem struct {
ID string `xml:"id,attr,omitempty"`
Name string `xml:"name"`
}
type XMLWeekly struct {
XMLName xml.Name `xml:"radiko"`
XMLStations struct {
XMLName xml.Name `xml:"stations"`
Station []XMLWeeklyStation `xml:"station"`
} `xml:"stations"`
}
type XMLWeeklyStation struct {
StationID string `xml:"id,attr"`
Name string `xml:"name"`
Progs XMLProgs `xml:"progs"`
}
// FetchWeeklyPrograms returns the weekly programs.
func FetchWeeklyPrograms(stationID string) (Progs, error) {
endpoint := fmt.Sprintf(APIWeeklyProgram, stationID)
resp, err := http.Get(endpoint) //nolint:gosec,noctx
if err != nil {
return Progs{}, err
}
defer resp.Body.Close()
return decodeWeeklyProgram(resp.Body)
}
func decodeWeeklyProgram(iorc io.ReadCloser) (Progs, error) {
progs := Progs{}
body, err := io.ReadAll(iorc)
if err != nil {
return progs, err
}
err = xml.Unmarshal(body, &progs)
return progs, err
}