-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
198 lines (170 loc) · 5.56 KB
/
parser.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
// oerc, alias oer-collector
// Copyright (C) 2021-2024 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 (
"github.com/alitto/pond"
"gorm.io/gorm"
"log"
"time"
)
// Parser common data structure of all parsers
type Parser struct {
ChannelFamilyKey string
ChannelFamily ChannelFamily
db *gorm.DB
dateRangeHandler dateRangeHandler
parallelWorkersCount int
}
// ParserInterface all parsers should implement this interface
type ParserInterface interface {
handleDay(chn Channel, day time.Time) // process a single day for a single channel
fetchTVShows() // handle tv shows
postProcess() // run tasks after the program and tv show entries are fetched
preProcess() bool // run tasks before the fetch process (like receiving API keys etc.), return success state
isDateValidToFetch(day *time.Time) (bool, error)
}
type dateRangeHandler interface {
getDateRange() *[]time.Time
}
type defaultDateRangeHandler struct {
DaysInPast uint
DaysInFuture uint
}
type specificDateRangeHandler struct {
StartDateTime time.Time
EndDateTime time.Time
}
func newDefaultDateRangeHandler() dateRangeHandler {
return &defaultDateRangeHandler{
DaysInPast: GetAppConf().DaysInPast,
DaysInFuture: GetAppConf().DaysInFuture,
}
}
func newDefaultDateRangeHandlerPadded(paddingDays uint) dateRangeHandler {
return &defaultDateRangeHandler{
DaysInPast: GetAppConf().DaysInPast - paddingDays,
DaysInFuture: GetAppConf().DaysInFuture + paddingDays,
}
}
func newSpecificDateRangeHandler(startDateTime time.Time, endDateTime time.Time) dateRangeHandler {
return &specificDateRangeHandler{
StartDateTime: startDateTime,
EndDateTime: endDateTime,
}
}
func newSpecificDateRangeHandlerPadded(startDateTime time.Time, endDateTime time.Time, paddingDays uint) dateRangeHandler {
return &specificDateRangeHandler{
StartDateTime: startDateTime.Add(time.Duration(paddingDays) * -24 * time.Hour),
EndDateTime: endDateTime.Add(time.Duration(paddingDays) * 24 * time.Hour),
}
}
func (d *defaultDateRangeHandler) getDateRange() *[]time.Time {
return generateDateRangeInPastAndFuture(d.DaysInPast, d.DaysInFuture)
}
func (s *specificDateRangeHandler) getDateRange() *[]time.Time {
return generateDateRangeBetweenDates(s.StartDateTime, s.EndDateTime)
}
// Fetch generic fetch function ready to handle all parsers, sets ChannelFamily and DB to the instance
func (p *Parser) Fetch(parserInterface ParserInterface) {
// setup db
db := getDb()
p.db = db
// get channel family db record and save it to the parser instance
var channelFamily = getChannelFamily(db, p.ChannelFamilyKey)
if channelFamily.ID == 0 {
log.Fatalf("ChannelFamilyKey '%s' was not found!\n", p.ChannelFamilyKey)
return
}
p.ChannelFamily = *channelFamily
if parserInterface == nil {
log.Fatalf("Incompliant parser instance received! Key: '%s'\n", p.ChannelFamilyKey)
}
isReady := parserInterface.preProcess()
if !isReady {
log.Printf("Parser is not ready to start\n")
return
}
// import tv shows
if GetAppConf().EnableTVShowCollection {
parserInterface.fetchTVShows()
}
timeRange := p.dateRangeHandler.getDateRange()
var times []time.Time
if timeRange != nil {
times = *timeRange
} else {
log.Printf("No valid time range received!\n")
return
}
if GetAppConf().EnableProgramEntryCollection {
// import program entries for the configured date range
pool := pond.New(int(p.parallelWorkersCount), 100, getWorkerPoolIdleTimeout(), pond.PanicHandler(func(i interface{}) {
log.Printf("Problem with goroutine pool: %v\n", i)
}))
for _, channel := range getChannelsOfFamily(db, channelFamily) {
for _, day := range times {
chn := channel
dayToFetch := day
ok, err := parserInterface.isDateValidToFetch(&dayToFetch)
if ok && err == nil {
pool.Submit(func() {
parserInterface.handleDay(chn, dayToFetch)
})
} else {
if verboseGlobal {
log.Printf("Skipping date '%s' of channel #%s\n", dayToFetch.Format("2006-01-02"), chn.ChannelFamily.Title)
}
}
}
}
// wait for finish
pool.StopAndWait()
// general tag linking
parserInterface.postProcess()
}
if verboseGlobal {
log.Printf("%s parsed successfully\n", p.ChannelFamilyKey)
}
}
func (p *Parser) isMoreThanXDaysInFuture(day *time.Time, days uint) bool {
if day == nil {
return false
}
if days == 0 {
return true
}
now := time.Now()
return day.After(now) && day.Sub(now) > time.Duration(days)*24*time.Hour
}
func (p *Parser) isMoreThanXDaysInPast(day *time.Time, days uint) bool {
if day == nil {
return false
}
if days == 0 {
return true
}
now := time.Now()
return day.Before(now) && now.Sub(*day) > time.Duration(days)*24*time.Hour
}
func (p *Parser) logRecentFetch(customMessage string) {
timeOfNextUpdate := getTimeOfNextUpdate()
log.Printf("%s, due to recent fetch. Next regular fetch will be at: %s. "+
"Use 'forceUpdate' = true to ignore this.",
customMessage,
timeOfNextUpdate.Format(time.RFC822),
)
}