-
Notifications
You must be signed in to change notification settings - Fork 1
/
zdf.go
442 lines (397 loc) · 14.1 KB
/
zdf.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// oerc, alias oer-collector
// Copyright (C) 2021-2023 emschu[aet]mailbox.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program.
// If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/alitto/pond"
"log"
"net/url"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
)
const (
zdfHost = "https://www.zdf.de"
zdfAPIHost = "https://api.zdf.de"
zdfAPIKeyPath = "/live-tv"
zdfAPIProgramEntries = zdfAPIHost + "/cmdm/epg/broadcasts?limit=100&order=asc"
)
var (
pendingHashes sync.Map // to avoid duplicate entries
zdfTvShowLinkMatcher = regexp.MustCompile(`^https?://(www\.)?zdf.de/.*`)
zdfTvShowExternalIDMatcher = regexp.MustCompile(`[a-zA-Z0-9_-]+`)
)
// ZDFParser struct to group zdf parsing code
type ZDFParser struct {
ParserInterface
Parser
zdfAPIKey string
}
func (z *ZDFParser) postProcess() {}
func (z *ZDFParser) preProcess() bool {
var apiKeyErr error
zdfAPIKey, apiKeyErr := z.getZdfAPIKey()
if apiKeyErr != nil {
log.Printf("Error fetching zdf api key: %v\n", apiKeyErr)
return false
}
if verboseGlobal {
log.Printf("Using ZDF API key: %s\n", *zdfAPIKey)
}
z.zdfAPIKey = *zdfAPIKey
z.parallelWorkersCount = 3
return true
}
// getZdfAPIKey method to retrieve the api key we need to connect to the zdf api
func (z *ZDFParser) getZdfAPIKey() (*string, error) {
apiURL := fmt.Sprintf("%s%s", zdfHost, zdfAPIKeyPath)
doc, err := getDocument(apiURL)
if doc == nil || err != nil {
return nil, fmt.Errorf("Problem fetching url '%s'", apiURL)
}
var apiToken string
doc.Find("script").Each(func(i int, selection *goquery.Selection) {
html, _ := selection.Html()
if strings.Contains(html, "IMPORTANT CONFIGURATION!") {
extractionPattern := regexp.MustCompile(`apiToken: '(.*)'`)
findString := extractionPattern.FindAllStringSubmatch(html, 1)
if len(findString) > 0 && len(findString[0]) > 1 {
apiToken = trimAndSanitizeString(findString[0][1])
}
}
})
if len(apiToken) > 0 {
return &apiToken, nil
}
return nil, fmt.Errorf("can't fetch ZDF api key")
}
// method to process a single day of a single channel
func (z *ZDFParser) handleDay(channel Channel, day time.Time) {
var startDateStr = day.Format(time.RFC3339)
var endDate = day.AddDate(0, 0, 1)
var endDateStr = endDate.Format(time.RFC3339)
// TODO pagination?
apiURL := fmt.Sprintf(
"%s&tvServices=%s&from=%s&to=%s&page=1",
zdfAPIProgramEntries,
url.QueryEscape(channel.TechnicalID),
url.QueryEscape(startDateStr),
url.QueryEscape(endDateStr),
)
if verboseGlobal {
log.Printf("Visit: %s\n", apiURL)
}
zdfProgramRequest, apiErr := z.doZDFApiBroadcastRequest(apiURL)
if apiErr != nil {
log.Printf("%v\n", apiErr)
return
}
for _, broadcast := range zdfProgramRequest.BroadCasts {
var programEntry ProgramEntry
hash := buildHash([]string{
broadcast.PosID,
broadcast.AirtimeBegin.Format(time.RFC3339),
broadcast.PlayoutID,
broadcast.Title,
broadcast.TvServiceID,
})
z.db.Model(ProgramEntry{}).
Where("hash = ?", hash).
Where("channel_id = ?", channel.ID).
Preload("ImageLinks").
Find(&programEntry)
programEntry.Hash = hash
if programEntry.ID >= 0 && programEntry.isRecentlyUpdated() {
atomic.AddUint64(&status.TotalSkippedPE, 1)
continue
}
_, ok := pendingHashes.Load(hash)
if ok {
// the same hash is already getting added
continue
}
pendingHashes.Store(hash, true)
programEntry.TechnicalID = broadcast.PosID
begin := broadcast.EffectiveAirtimeBegin
end := broadcast.EffectiveAirtimeEnd
// Use them as fallback...
if begin.IsZero() {
begin = broadcast.AirtimeBegin
}
if end.IsZero() {
end = broadcast.AirtimeEnd
}
if begin.IsZero() || end.IsZero() {
appLog(fmt.Sprintf("Error fetching zdf program entry at POSID '%s'. Invalid airtime begin or end date.", broadcast.PosID))
continue
}
programEntry.StartDateTime = &begin
programEntry.EndDateTime = &end
programEntry.DurationMinutes = int16(programEntry.EndDateTime.Sub(*programEntry.StartDateTime).Minutes())
programEntry.URL = fmt.Sprintf("%s%s", zdfAPIHost, broadcast.Self)
programEntry.Title = trimAndSanitizeString(broadcast.Title)
var subTitle = trimAndSanitizeString(broadcast.SubTitle)
if len(broadcast.SubTitle) > 0 {
// append to title
programEntry.Title += " - " + subTitle
}
programEntry.Description = trimAndSanitizeString(broadcast.Text)
programEntry.Channel = channel
programEntry.ChannelFamily = z.ChannelFamily
// save image links
programEntry.handleProgramImageLinks(&broadcast)
programItemAPIUrl := zdfAPIHost + broadcast.ProgrammeItem
programEntry.Homepage = programItemAPIUrl
apiResponse, apiErr := z.doZDFApiProgramItemRequest(programItemAPIUrl)
if apiErr != nil {
log.Printf("Problem fetching zdf api program item data: '%s'\n", programItemAPIUrl)
} else {
if len(apiResponse.Category) > 0 {
programEntry.considerTagExists(&apiResponse.Category)
}
if len(apiResponse.Genre) > 0 {
programEntry.considerTagExists(&apiResponse.Genre)
}
}
programEntry.saveProgramEntryRecord(z.db)
pendingHashes.Delete(programEntry.Hash)
}
}
// atm only useful in zdf context
func (p *ProgramEntry) handleProgramImageLinks(broadcast *ZdfBroadcast) {
if broadcast == nil || p == nil {
// nothing to do here
return
}
if len(broadcast.Images.Layouts.W2400) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W2400) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W2400})
}
if len(broadcast.Images.Layouts.W1920) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W1920) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W1920})
}
if len(broadcast.Images.Layouts.W1280) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W1280) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W1280})
}
if len(broadcast.Images.Layouts.W768) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W768) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W768})
}
if len(broadcast.Images.Layouts.W640) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W640) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W640})
}
if len(broadcast.Images.Layouts.W384) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W384) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W384})
}
if len(broadcast.Images.Layouts.W276) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W276) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W276})
}
if len(broadcast.Images.Layouts.W240) > 0 && !p.doesImageLinkExist(broadcast.Images.Layouts.W240) {
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: broadcast.Images.Layouts.W240})
}
}
func (p *ProgramEntry) doesImageLinkExist(url string) bool {
for _, entryDetails := range p.ImageLinks {
if entryDetails.URL == url {
return true
}
}
return false
}
func (z *ZDFParser) doZDFApiBroadcastRequest(apiURL string) (*ZdfBroadcastResponse, error) {
headers := map[string]string{
"Host": "api.zdf.de",
"Accept": "application/vnd.de.zdf.v1.0+json",
"Origin": zdfHost,
"Api-Auth": "Bearer " + z.zdfAPIKey,
}
resp, err := doGetRequest(apiURL, headers, 3)
if resp == nil || err != nil {
errMsg := fmt.Sprintf("Problem fetching URL '%s' with error '%v'", apiURL, err)
appLog(errMsg)
log.Println(errMsg)
return nil, err
}
var response ZdfBroadcastResponse
jsonErr := json.Unmarshal([]byte(*resp), &response)
if jsonErr != nil {
errMsg := fmt.Sprintf("Invalid json format in zdf api response. url: '%s'", apiURL)
appLog(errMsg)
log.Println(errMsg)
return nil, jsonErr
}
return &response, nil
}
func (z *ZDFParser) doZDFApiProgramItemRequest(apiURL string) (*ZdfProgramItemResponse, error) {
headers := map[string]string{
"Host": "api.zdf.de",
"Accept": "application/vnd.de.zdf.v1.0+json",
"Origin": zdfHost,
"Api-Auth": "Bearer " + z.zdfAPIKey,
}
resp, err := doGetRequest(apiURL, headers, 3)
if resp == nil || err != nil {
errMsg := fmt.Sprintf("Problem fetching URL '%s' with error '%v'", apiURL, err)
appLog(errMsg)
log.Println(errMsg)
return nil, err
}
var response ZdfProgramItemResponse
jsonErr := json.Unmarshal([]byte(*resp), &response)
if jsonErr != nil {
errMsg := fmt.Sprintf("Invalid json format: %s", jsonErr.Error())
appLog(errMsg)
log.Println(errMsg)
return nil, jsonErr
}
return &response, nil
}
// fetchTvShows method to fetch zdf tv shows
func (z *ZDFParser) fetchTVShows() {
if !GetAppConf().EnableTVShowCollection || isRecentlyFetched() {
z.logRecentFetch("Skip update of zdf tv shows")
return
}
var tvShowGroups = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0+-+9"}
// fetch all links to tv shows
if verboseGlobal {
log.Printf("Collecting zdf tv shows ...\n")
}
var tvShowLinks = make([]string, 0)
for _, group := range tvShowGroups {
apiURL := fmt.Sprintf("%s/sendungen-a-z?group=%s", zdfHost, group)
document, err := getDocument(apiURL)
if document == nil || err != nil {
appLog(fmt.Sprintf("Problem with http call to zdf %v", err))
continue
}
document.Find(".b-content-teaser-item h3 a").Each(func(i int, selection *goquery.Selection) {
tvShowPage := selection.AttrOr("href", "")
if len(tvShowPage) > 0 {
tvShowLinks = append(tvShowLinks, zdfHost+tvShowPage)
}
})
}
// and now process them
if verboseGlobal {
log.Printf("Processing %d zdf tv shows ...\n", len(tvShowLinks))
}
pool := pond.New(4, 100, getWorkerPoolIdleTimeout())
for _, singleTvShowPage := range tvShowLinks {
singlePage := singleTvShowPage
pool.Submit(func() {
z.processSingleTvShow(singlePage)
})
}
pool.StopAndWait()
}
func (z *ZDFParser) processSingleTvShow(singleTvShowPage string) {
doc, err := getDocument(singleTvShowPage)
if doc == nil || err != nil {
appLog(fmt.Sprintf("Could not fetch tv show detail page at '%s'", singleTvShowPage))
return
}
// var tvShowRecord TvShow
plusBtn := doc.Find(".b-plus-button")
var tvShowRecord TvShow
tvShowTitle, exists := plusBtn.Attr("data-plusbar-title")
tvShowURL, exists2 := plusBtn.Attr("data-plusbar-url")
tvShowAPIPath, exists3 := plusBtn.Attr("data-plusbar-path")
tvShowExternalID, exists4 := plusBtn.Attr("data-plusbar-external-id")
tvShowID, exists5 := plusBtn.Attr("data-plusbar-id")
if !exists || !exists2 || !exists3 || !exists4 || !exists5 {
appLog(fmt.Sprintf("Error finding zdf tv show information"))
return
}
hash := buildHash([]string{tvShowExternalID, tvShowID})
z.db.Where("hash = ?", hash).Find(&tvShowRecord)
// do validation steps
if !zdfTvShowLinkMatcher.Match([]byte(tvShowURL)) {
appLog(fmt.Sprintf("Unexpected zdf tv show url '%s' detected. URL was set empty.", tvShowURL))
tvShowURL = ""
}
if !zdfTvShowExternalIDMatcher.Match([]byte(tvShowExternalID)) {
appLog("Unexpected external id received for zdf tv show. Skipping entry.")
return
}
tvShowRecord.Hash = hash
tvShowRecord.Title = trimAndSanitizeString(tvShowTitle)
tvShowRecord.Homepage = tvShowURL
tvShowRecord.URL = zdfHost + tvShowAPIPath
tvShowRecord.TechnicalID = tvShowExternalID
tvShowRecord.ChannelFamily = z.ChannelFamily
tvShowRecord.saveTvShowRecord(z.db)
return
}
func (z *ZDFParser) isDateValidToFetch(day *time.Time) (bool, error) {
if day == nil {
return false, fmt.Errorf("invalid day")
}
if z.isMoreThanXDaysInFuture(day, 43) { // = six weeks in future + today
return false, fmt.Errorf("maximum for days in future for ZDF is 43")
}
location, _ := time.LoadLocation(GetAppConf().TimeZone)
earliestDate := time.Date(2015, 2, 1, 0, 0, 0, 0, location)
if day.Before(earliestDate) {
return false, fmt.Errorf("maximum for days in past for ZDF is %s", earliestDate.Format(time.RFC822))
}
return true, nil
}
// ZdfBroadcastResponse api response struct definitions
type ZdfBroadcastResponse struct {
BroadCasts []ZdfBroadcast `json:"http://zdf.de/rels/cmdm/broadcasts"`
}
// ZdfBroadcast zdf api object
type ZdfBroadcast struct {
PosID string `json:"posId"`
PlayoutID string `json:"playoutId"`
AirtimeBegin time.Time `json:"airtimeBegin"`
AirtimeEnd time.Time `json:"airtimeEnd"`
EffectiveAirtimeBegin time.Time `json:"effectiveAirtimeBegin"`
EffectiveAirtimeEnd time.Time `json:"effectiveAirtimeEnd"`
Self string `json:"self"`
Title string `json:"title"`
SubTitle string `json:"subtitle"`
Text string `json:"text"`
Images ZdfImage `json:"http://zdf.de/rels/image"`
TvServiceID string `json:"tvServiceId"`
ProgrammeItem string `json:"http://zdf.de/rels/cmdm/programme-item"`
}
// ZdfImage zdf api object
type ZdfImage struct {
Source string `json:"source"`
Layouts ZdfImageLayouts `json:"layouts"`
}
// ZdfImageLayouts zdf api object
type ZdfImageLayouts struct {
W2400 string `json:"2400x1350,omitempty"`
W1920 string `json:"1920x1080,omitempty"`
W1280 string `json:"1280x720,omitempty"`
W1152 string `json:"1152x1296,omitempty"`
W768 string `json:"768x432,omitempty"`
W640 string `json:"640x720,omitempty"`
W384 string `json:"384x216,omitempty"`
W276 string `json:"276x155,omitempty"`
W240 string `json:"240x270,omitempty"`
}
// ZdfProgramItemResponse zdf api object
type ZdfProgramItemResponse struct {
Category string `json:"category,omitempty"`
Genre string `json:"genre,omitempty"`
}