-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
186 lines (163 loc) · 4.02 KB
/
model.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"strings"
"sync"
"time"
)
type Response struct {
Data []Data `json:"data"`
Included []Field `json:"included"`
}
func (r Response) toRadio() []*Radio {
data := r.Data
included := r.Included
radios := make([]*Radio, len(data))
var wg sync.WaitGroup
for i, datum := range data {
wg.Add(1)
go func(i int, datum Data) {
defer wg.Done()
attr := datum.Attributes
category := datum.getCategory(included)
media := datum.getMedia(included)
audio := media.getAudio()
radio := Radio{
ID: datum.ID,
Title: attr.Title,
Description: datum.getDescription(),
Thumb: datum.getThumb(),
PublishAt: datum.getPublish(),
Category: category.Name,
Audio: audio,
Length: remoteContentLength(audio),
Duration: attr.Duration,
}
radios[i] = &radio
}(i, datum)
}
wg.Wait()
return radios
}
type Data struct {
ID string `json:"id"`
Attributes struct {
Title string `json:"title"`
Description string `json:"desc"`
Thumb string `json:"thumb"`
PublishedAt string `json:"published-at"`
Duration int `json:"duration"`
Content string `json:"content"`
} `json:"attributes"`
Relationships struct {
Category Relation `json:"category"`
Media Relation `json:"media"`
} `json:"relationships"`
}
func (d Data) getThumb() string {
thumb := d.Attributes.Thumb
if !isUrlValid(thumb) {
thumb = imageDomain + thumb
}
return thumb
}
func (d Data) getDescription() string {
attr := d.Attributes
var builder strings.Builder
builder.WriteString(attr.Description)
builder.WriteString("<br/>")
if len(attr.Content) > 0 {
var block ContentBlock
if err := json.Unmarshal([]byte(attr.Content), &block); nil == err {
for i, b := range block.Blocks {
replaced := strings.ReplaceAll(b.Text, "\n", "<br/>")
builder.WriteString(replaced)
if i < len(block.Blocks)-1 {
builder.WriteString("<br/>")
}
}
}
}
desc := builder.String()
return cleanString(desc)
}
func (d Data) getPublish() time.Time {
pub := d.Attributes.PublishedAt
t, _ := time.Parse(time.RFC3339, pub)
return t
}
func (d Data) getCategory(fields []Field) Category {
category := d.Relationships.Category
typ := category.Data.Type
id := category.Data.ID
for _, field := range fields {
if field.Type == typ && field.ID == id {
return field.Attributes.Category
}
}
return Category{}
}
func (d Data) getMedia(fields []Field) Media {
media := d.Relationships.Media
typ := media.Data.Type
id := media.Data.ID
for _, field := range fields {
if field.Type == typ && field.ID == id {
return field.Attributes.Media
}
}
return Media{}
}
type ContentBlock struct {
Blocks []struct {
Text string `json:"text"`
} `json:"blocks"`
}
type Relation struct {
Data struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"data"`
}
type Field struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Category
Media
} `json:"attributes"`
}
type Category struct {
Name string `json:"name"` // type: categories
}
type Media struct {
Audio string `json:"audio"` // type: media
}
func (m Media) getAudio() string {
audio := m.Audio
if !isUrlValid(audio) {
audio = audioDomain + audio
}
return audio
}
type Radio struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Thumb string `json:"thumb"`
PublishAt time.Time `json:"publish_at"`
Category string `json:"category"`
Audio string `json:"audio"`
Length int64 `json:"length"`
Duration int `json:"duration"`
}
// ByPublishAt implements sort.Interface based on the Radio PublishAt field.
type ByPublishAt []*Radio
func (a ByPublishAt) Len() int { return len(a) }
func (a ByPublishAt) Less(i, j int) bool {
if a[i].PublishAt.Equal(a[j].PublishAt) {
return a[i].ID > a[j].ID
}
return a[i].PublishAt.After(a[j].PublishAt)
}
func (a ByPublishAt) Swap(i, j int) { a[i], a[j] = a[j], a[i] }