-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.go
363 lines (301 loc) · 8.06 KB
/
fetcher.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus"
)
const (
dbDriver = "sqlite3"
tempDBFile = "mwb.db"
)
func (c *Config) getMMData() (mmd MeetingData, err error) {
jwpubBytes, err := c.getJWPub("mwb")
if err != nil {
return
}
tempDir, err := os.MkdirTemp("", "jwpub_fetcher_")
if err != nil {
return
}
defer os.RemoveAll(tempDir)
contents, err := unzipFile(jwpubBytes, "contents")
if err != nil {
return
}
dbBytes, err := unzipFile(contents, "mwb*.db")
if err != nil {
return
}
// write this file to disk; quick check doesn't show an easy way for sqlite to handle things in memory only
dbFilename := filepath.Join(tempDir, tempDBFile)
dbFile, err := os.Create(dbFilename)
if err != nil {
return
}
_, err = dbFile.Write(dbBytes)
if err != nil {
return
}
dbFile.Close()
sqlDB, err := sql.Open(dbDriver, dbFilename)
if err != nil {
return
}
defer sqlDB.Close()
docs, err := getMWBDocuments(sqlDB)
if err != nil {
return
}
logrus.Debug("docs >>", docs)
var docGroups []Document
for _, doc := range docs {
if c.Date != doc.Date {
continue
}
docGroups = append(docGroups, doc)
}
if len(docGroups) == 0 {
return mmd, errors.New("no docs found!")
}
mmd = MeetingData{
DateString: docGroups[0].Date.Format("2006-01-02"),
Songs: getMWBSongs(sqlDB, docGroups),
}
if c.FetchOtherMedia {
imageNames := getImageNames(sqlDB, docGroups)
for _, name := range imageNames {
// skip images we already have
for _, p := range mmd.Pictures {
if p.Name == name {
continue
}
}
// fetch image from contents
pic, err := unzipFile(contents, name)
if err != nil {
return MeetingData{}, errors.New("problem getting pic")
}
// queue for storage
mmd.Pictures = append(mmd.Pictures, file{Name: name, Payload: pic})
}
mmd.Videos = getMWBVideos(sqlDB, docGroups)
linkedDocs := c.getLinkedDocs(sqlDB, docGroups)
for _, ld := range linkedDocs {
docMedia, err := c.getDocMedia(ld)
if err != nil {
logrus.Warn(err)
}
mmd.Pictures = append(mmd.Pictures, docMedia.Pictures...)
mmd.Videos = append(mmd.Videos, docMedia.Videos...)
}
}
return
}
func (c *Config) getWMData() (wmd MeetingData, err error) {
jwpubBytes, err := c.getJWPub("w")
if err != nil {
return
}
tempDir, err := os.MkdirTemp("", "jwpub_fetcher_")
if err != nil {
return
}
defer os.RemoveAll(tempDir)
contents, err := unzipFile(jwpubBytes, "contents")
if err != nil {
return
}
dbBytes, err := unzipFile(contents, "w*.db")
if err != nil {
return
}
// write this file to disk; quick check doesn't show an easy way for sqlite to handle things in memory only
dbFilename := filepath.Join(tempDir, tempDBFile)
dbFile, err := os.Create(dbFilename)
if err != nil {
return
}
_, err = dbFile.Write(dbBytes)
if err != nil {
return
}
dbFile.Close()
sqlDB, err := sql.Open(dbDriver, dbFilename)
if err != nil {
return
}
defer sqlDB.Close()
// pub := getPublication(sqlDB)
docs, err := getWTDocuments(sqlDB)
if err != nil {
return
}
logrus.Debug("docs >>", docs)
dates, err := getWTDates(sqlDB)
if err != nil {
return
}
logrus.Debug("dates >>", dates)
for i, doc := range docs {
if c.Date != dates[i] {
continue
}
wmd = MeetingData{
DateString: c.Date.Format("2006-01-02"),
Songs: getWTSongs(sqlDB, c.Date),
}
if c.FetchOtherMedia {
pics := []file{}
imageNames := getImageNames(sqlDB, []Document{{ID: doc}})
for _, name := range imageNames {
pic, err := unzipFile(contents, name)
if err != nil {
return wmd, errors.New("problem getting pic")
}
pics = append(pics, file{Name: name, Payload: pic})
}
wmd.Pictures = pics
}
}
return
}
func (c *Config) getDocMedia(ld LinkedDocument) (md MeetingData, err error) {
jwpubBytes, err := c.getJWPub(ld.PublicationSymbol)
if err != nil {
return
}
tempDir, err := os.MkdirTemp("", "jwpub_fetcher_")
if err != nil {
return
}
defer os.RemoveAll(tempDir)
contents, err := unzipFile(jwpubBytes, "contents")
if err != nil {
return
}
dbBytes, err := unzipFile(contents, ld.PublicationSymbol+"*.db")
if err != nil {
return
}
// write this file to disk; quick check doesn't show an easy way for sqlite to handle things in memory only
dbFilename := filepath.Join(tempDir, tempDBFile)
dbFile, err := os.Create(dbFilename)
if err != nil {
return
}
_, err = dbFile.Write(dbBytes)
if err != nil {
return
}
dbFile.Close()
sqlDB, err := sql.Open(dbDriver, dbFilename)
if err != nil {
return
}
defer sqlDB.Close()
mepsDocs, err := getMEPSDocuments(sqlDB, ld.MepsDocumentID)
if err != nil {
return
}
logrus.Debug("docs >>", mepsDocs)
for _, d := range mepsDocs {
switch d.MimeType {
case "image/jpeg":
// skip known images that are not needed from 'th'
if d.Name == "1102018440_univ_cnt_1.jpg" ||
d.Name == "1102018440_univ_cnt_2.jpg" {
continue
}
pic, err := unzipFile(contents, d.Name)
if err != nil {
return MeetingData{}, errors.New("problem getting pic")
}
md.Pictures = append(md.Pictures, file{Name: d.Name, Payload: pic})
case "video/mp4":
md.Videos = append(md.Videos, d.video)
}
}
// logrus.Debug("getDocMedia()", md)
return
}
func (c *Config) getJWPub(pub string) ([]byte, error) {
date := c.Date
switch pub {
case "w":
// W is published 2 months prior to it beeing needed for the meeting
date = c.Date.AddDate(0, -2, 0)
case "mwb":
// mwb is released two months at a time.
if c.Date.Month()%2 == 0 {
date = c.Date.AddDate(0, -1, 0)
}
}
m, err := c.getJWPubInfo(date.Year(), int(date.Month()), pub)
if err != nil {
return nil, err
}
jwpubItem := m.Files[c.Language].JWPUB[0]
filename := filepath.Base(jwpubItem.File.URL)
payload, err := c.getFromCache(filename, jwpubItem.File.Checksum)
if err == nil {
return payload, err
}
payload, err = c.download(jwpubItem)
if err == nil {
c.saveToCache(file{
Name: filename,
Payload: payload,
})
}
return payload, err
}
func (c *Config) getJWPubInfo(year, month int, pub string) (*mediaInfo, error) {
var str string
switch pub {
case "w", "mwb":
str = fmt.Sprintf("https://b.jw-cdn.org/apis/pub-media/GETPUBMEDIALINKS?issue=%d%02d&output=json&pub=%s&fileformat=JWPUB&alllangs=0&langwritten=%s&txtCMSLang=%s", year, month, pub, c.Language, c.Language)
default:
str = fmt.Sprintf("https://b.jw-cdn.org/apis/pub-media/GETPUBMEDIALINKS?output=json&pub=%s&fileformat=JWPUB&alllangs=0&langwritten=%s&txtCMSLang=%s", pub, c.Language, c.Language)
}
logrus.Debug("getJWPubInfo()", str)
resp, err := c.HttpClient.Get(str)
if err != nil {
return nil, fmt.Errorf("failed to get media info for %s-%d-%02d workbook", c.Language, year, month)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("no workbook available for %v-%02d", year, month)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("error reading info for workbook")
}
info := new(mediaInfo)
err = json.Unmarshal(body, info)
return info, err
}
func (c *Config) download(jwpi JWPubItem) ([]byte, error) {
var body []byte
resp, err := c.HttpClient.Get(jwpi.File.URL)
if err != nil {
return body, errors.New("failed to download " + jwpi.File.URL)
}
c.Progress.ProgressBar.SetValue(0)
c.Progress.Total = 0
c.Progress.Title = filepath.Base(jwpi.File.URL)
c.Progress.ProgressBar.Max = float64(jwpi.Filesize)
data := io.TeeReader(resp.Body, c.Progress)
body, err = io.ReadAll(data)
if err != nil {
return body, errors.New("error reading data from " + jwpi.File.URL)
}
if !validChecksum(jwpi.File.Checksum, body) {
return nil, errors.New("invalid checksum for downloaded file")
}
return body, nil
}