-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
347 lines (292 loc) · 8.44 KB
/
media.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"github.com/sirupsen/logrus"
)
func (c *Config) fetchMeetingStuff(m string) (err error) {
logrus.Debug("fetchMeetingStuff()")
if c.PurgeSaveDir {
logrus.Info("Deleting all files in " + c.SaveLocation)
if err := RemoveContents(c.SaveLocation); err != nil {
logrus.Warn(err)
}
}
if c.AutoFetchMeetingData {
logrus.Info("Auto-Fetching!")
var data MeetingData
var err error
switch m {
case WM:
data, err = c.getWMData()
c.SongsToGet = []string{
c.SongsToGet[0],
data.Songs[0],
data.Songs[1],
}
case MM:
data, err = c.getMMData()
c.SongsToGet = data.Songs
c.Videos = data.Videos
}
if err != nil {
return err
}
c.Pictures = data.Pictures
}
for _, song := range c.SongsToGet {
if err := c.downloadSong(song); err != nil {
return err
}
}
if c.FetchOtherMedia {
for i, video := range c.Videos {
name, err := c.downloadVideo(&video)
if err != nil {
logrus.Warnf("error fetching video: %s => %s", name, err)
continue
}
c.Videos[i].Name = name
}
for _, picture := range c.Pictures {
logrus.Infof("saving picture %s", picture.Name)
file := filepath.Join(c.SaveLocation, picture.Name)
if os.WriteFile(file, picture.Payload, 0644) != nil {
return errors.New("error writing data to " + file)
}
}
}
if c.CreatePlaylist {
return c.createPlaylist()
}
return
}
func (c *Config) createPlaylist() error {
logrus.Info("creating playlist")
sort.Slice(c.Pictures, func(i, j int) bool {
return c.Pictures[i].Name < c.Pictures[j].Name
})
file := filepath.Join(c.SaveLocation, "/playlist.m3u")
body := "#EXTM3U\n"
for _, s := range c.SongsNames {
body += s + "\n"
}
for _, v := range c.Videos {
body += v.Name + "\n"
}
for _, p := range c.Pictures {
body += p.Name + "\n"
}
if err := os.WriteFile(file, []byte(body), 0644); err != nil {
return err
}
return nil
}
func (c *Config) downloadSong(num string) (err error) {
logrus.Info("downloading song " + num)
var res int
switch c.Resolution {
case RES240:
res = 0
case RES360:
res = 1
case RES480:
res = 2
case RES720:
res = 3
default:
res = 0
}
songInfo, err := c.getSongInfo(num)
if err != nil {
return
}
filename := filepath.Base(songInfo.Files[c.Language].MP4[res].File.URL)
payload, err := c.getFromCache(filename, songInfo.Files[c.Language].MP4[res].File.Checksum)
if err != nil {
payload, err = c.downloadSongMedia(songInfo, res)
if err != nil {
return err
}
}
c.SongsNames = append(c.SongsNames, filename)
return c.saveAndLink(file{
Name: filename,
Payload: payload,
})
}
func (c *Config) downloadVideo(v *video) (filename string, err error) {
var res int
switch c.Resolution {
case RES240:
res = 0
case RES360:
res = 1
case RES480:
res = 2
case RES720:
res = 3
default:
res = 0
}
var url, checksum string
var filesize int
if v.IssueTagNumber == 0 {
vidInfo, err := c.getMediaVideoInfo(v)
if err != nil {
return "", err
}
url = vidInfo.Files[c.Language].MP4[res].File.URL
filesize = vidInfo.Files[c.Language].MP4[res].Filesize
checksum = vidInfo.Files[c.Language].MP4[res].File.Checksum
} else {
vidInfo, err := c.getPubVideoInfo(v)
if err != nil {
return "", err
}
for i, v := range vidInfo.Media[0].Files {
if v.Label == c.Resolution && !v.Subtitled {
res = i
break
}
}
url = vidInfo.Media[0].Files[res].Progressivedownloadurl
filesize = vidInfo.Media[0].Files[res].Filesize
checksum = vidInfo.Media[0].Files[res].Checksum
}
filename = filepath.Base(url)
logrus.Infof("downloading video: %s", filename)
payload, err := c.getFromCache(filename, checksum)
if err != nil {
payload, err = c.downloadVideoMedia(url, filesize)
if err != nil {
return "", err
}
}
return filename, c.saveAndLink(file{
Name: filename,
Payload: payload,
})
}
func (c *Config) downloadSongMedia(songInfo *mediaInfo, vidKey int) (payload []byte, err error) {
url := songInfo.Files[c.Language].MP4[vidKey].File.URL
filename := filepath.Base(url)
if *c.DebugMode {
logrus.Debug("Mock downloadSongMedia:", url)
} else {
logrus.Debug("downloading media " + url)
if *c.DebugMode {
logrus.Debug("Mock Download SongMedia:", url)
} else {
resp, err := c.HttpClient.Get(url)
if err != nil {
return nil, errors.New("failed to download " + url)
}
c.Progress.ProgressBar.SetValue(0)
c.Progress.Total = 0
c.Progress.Title = filepath.Base(url)
c.Progress.ProgressBar.Max = float64(songInfo.Files[c.Language].MP4[vidKey].Filesize)
data := io.TeeReader(resp.Body, c.Progress)
payload, err = io.ReadAll(data)
if err != nil {
return nil, errors.New("error reading data from " + url)
}
if !validChecksum(songInfo.Files[c.Language].MP4[vidKey].File.Checksum, payload) {
return nil, errors.New("invalid checksum for " + filename)
}
}
}
return
}
func (c *Config) downloadVideoMedia(url string, filesize int) (payload []byte, err error) {
if *c.DebugMode {
logrus.Debug("Mock downloadVideoMedia:", url)
} else {
logrus.Debug("downloading media " + url)
resp, err := c.HttpClient.Get(url)
if err != nil {
return nil, errors.New("failed to download " + url)
}
c.Progress.ProgressBar.SetValue(0)
c.Progress.Total = 0
c.Progress.Title = filepath.Base(url)
c.Progress.ProgressBar.Max = float64(filesize)
data := io.TeeReader(resp.Body, c.Progress)
payload, err = io.ReadAll(data)
if err != nil {
return nil, errors.New("error reading data from " + url)
}
c.saveToCache(file{
Name: filepath.Base(url),
Payload: payload,
})
}
return payload, nil
}
func (c *Config) getSongInfo(num string) (*mediaInfo, error) {
logrus.Debug("fetching info for song number " + num)
resp, err := c.HttpClient.Get(fmt.Sprintf("https://b.jw-cdn.org/apis/pub-media/GETPUBMEDIALINKS?output=json&pub=sjjm&fileformat=mp4&alllangs=0&track=%s&langwritten=%s&txtCMSLang=%s", num, c.Language, c.Language))
if err != nil {
return nil, errors.New("failed to get media info for song #" + num)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("error reading info for song #" + num)
}
info := new(mediaInfo)
err = json.Unmarshal(body, info)
logrus.Debugf("fetched #%v: %#v", num, info)
return info, err
}
func (c *Config) getMediaVideoInfo(v *video) (*mediaInfo, error) {
logrus.Debugf("fetching info for video: %#v ", v)
variable := ""
if v.MepsDocumentID.Valid {
variable = fmt.Sprintf("&docid=%v", v.MepsDocumentID.Int64)
} else {
variable = fmt.Sprintf("&pub=%s", v.KeySymbol.String)
}
url := fmt.Sprintf("https://b.jw-cdn.org/apis/pub-media/GETPUBMEDIALINKS?%s&output=json&fileformat=mp4&alllangs=0&track=%v&langwritten=%s&txtCMSLang=%s", variable, v.Track.Int64, c.Language, c.Language)
logrus.Debug("getMediaVideoInfo() url:", url)
resp, err := c.HttpClient.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to get media info for video: %#v", v)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading info for video: %#v", v)
}
info := new(mediaInfo)
err = json.Unmarshal(body, info)
logrus.Debug("getMediaVideoInfo() info:", info)
return info, err
}
// example: https://b.jw-cdn.org/apis/mediator/v1/media-items/E/pub-jwbcov_201605_4_VIDEO
func (c *Config) getPubVideoInfo(v *video) (*PubVideo, error) {
logrus.Debugf("fetching info for video: %#v ", v)
url := fmt.Sprintf("https://b.jw-cdn.org/apis/mediator/v1/media-items/%s/pub-%s_%v_%v_VIDEO", c.Language, v.KeySymbol.String, v.IssueTagNumber/100, v.Track.Int64)
logrus.Debug("getVideoInfo() url:", url)
resp, err := c.HttpClient.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to get media info for video: %#v", v)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading info for video: %#v", v)
}
info := new(PubVideo)
err = json.Unmarshal(body, info)
logrus.Debug("getVideoInfo() info:", info)
return info, err
}
func (wc *progress) Write(p []byte) (int, error) {
n := len(p)
wc.Total += int64(n)
wc.ProgressBar.SetValue(float64(wc.Total))
return n, nil
}