-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.go
162 lines (145 loc) · 5.45 KB
/
search.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
// 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 (
"gorm.io/gorm"
"log"
"strings"
"time"
)
// uniqueStrSlice: function to make a slice of program entries unique
func uniqueStrSlice(stringSlice *[]string) []string {
keys := make(map[string]bool)
var res = make([]string, 0)
for _, entry := range *stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
res = append(res, entry)
}
}
return res
}
// uniqueProgramEntryList: function to make a slice of program entries unique
func uniqueProgramEntryList(programEntrySlice *[]ProgramEntry) []ProgramEntry {
keys := make(map[uint]bool)
var res = make([]ProgramEntry, 0)
for _, entry := range *programEntrySlice {
if _, value := keys[entry.ID]; !value {
keys[entry.ID] = true
res = append(res, entry)
}
}
return res
}
// SearchProgram method to check the program data of the next days for user-defined keywords
func SearchProgram() {
now := time.Now()
tStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
endDate := now.Add(time.Duration(GetAppConf().SearchDaysInFuture+1) * 24 * time.Hour)
tEnd := time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 3, 0, 0, 0, now.Location())
db := getDb()
var excludedChannelIDs = getExcludedChannelsFromSearch(db)
var programEntryList = make([]ProgramEntry, 0)
programResponse := getProgramOf(&tStart, &tEnd, nil)
if programResponse == nil || len(*programResponse.ProgramEntryList) == 0 {
log.Fatalf("Could not retrieve program for date range '%s'-'%s'. Please fetch the program at first.\n", tStart, tEnd)
return
}
skipCounter := 0
for _, programEntry := range *programResponse.ProgramEntryList {
// exclude the channels found above
if isChannelExcluded(excludedChannelIDs, &programEntry) {
skipCounter++
continue
}
var keywords = make([]string, 0)
for _, searchWord := range GetAppConf().SearchKeywords {
if strings.Contains(strings.ToLower(programEntry.Title), strings.ToLower(searchWord)) {
programEntryList = append(programEntryList, programEntry)
keywords = append(keywords, searchWord)
}
if strings.Contains(strings.ToLower(programEntry.Description), strings.ToLower(searchWord)) {
programEntryList = append(programEntryList, programEntry)
keywords = append(keywords, searchWord)
}
if strings.Contains(strings.ToLower(programEntry.Tags), strings.ToLower(searchWord)) {
programEntryList = append(programEntryList, programEntry)
keywords = append(keywords, searchWord)
}
}
programEntryList = uniqueProgramEntryList(&programEntryList)
if len(keywords) > 0 {
keywords = uniqueStrSlice(&keywords)
var reccEntry Recommendation
db.Model(&Recommendation{}).Where("program_entry_id = ?", programEntry.ID).First(&reccEntry)
now := time.Now()
reccEntry.CreatedAt = &now
reccEntry.ProgramEntry = programEntry
reccEntry.ProgramEntryID = programEntry.ID
reccEntry.ChannelID = programEntry.ChannelID
reccEntry.StartDateTime = programEntry.StartDateTime
reccEntry.Keywords = strings.Join(keywords, ",")
if reccEntry.ID > 0 {
db.Model(&reccEntry).Updates(map[string]interface{}{
"keywords": reccEntry.Keywords,
"start_date_time": programEntry.StartDateTime,
"channel_id": programEntry.ChannelID,
})
} else {
db.Create(&reccEntry)
}
}
}
log.Printf("Found %v search results in tv program of today + %d days. Searched in %d program entries.\n", len(programEntryList), GetAppConf().SearchDaysInFuture, len(*programResponse.ProgramEntryList)-skipCounter)
}
func getExcludedChannelsFromSearch(db *gorm.DB) *[]uint {
var excludedChannelIds = make([]uint, len(GetAppConf().SearchSkipChannels))
for _, channel := range GetAppConf().SearchSkipChannels {
var channelEntry Channel
db.Model(&Channel{}).Where("is_deprecated is false AND title = ?", channel).First(&channelEntry)
if channelEntry.ID > 0 {
excludedChannelIds = append(excludedChannelIds, channelEntry.ID)
} else {
log.Printf("Problem with channel name '%s'. Could not find it in database. Skipping this entry.\n", channel)
}
}
return &excludedChannelIds
}
func isChannelFamilyExcluded(family *ChannelFamily) bool {
if !appConf.EnableARD && family.Title == "ARD" {
return true
}
if !appConf.EnableZDF && family.Title == "ZDF" {
return true
}
if !appConf.EnableORF && family.Title == "ORF" {
return true
}
if !appConf.EnableSRF && family.Title == "SRF" {
return true
}
return false
}
// method to check if a single program entry should be skipped because its channel id is contained in the first parameter
func isChannelExcluded(excludedChannelIds *[]uint, programEntry *ProgramEntry) bool {
for _, skippedChannelID := range *excludedChannelIds {
if programEntry.ChannelID == skippedChannelID {
return true
}
}
return false
}