Skip to content

Commit 13e77d0

Browse files
authored
Merge pull request #148 from dop251/filters-pr
Implemented negative filters for title and description.
2 parents af09e4c + b47cc9b commit 13e77d0

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ vimeo = [ # Multiple keys will be rotated.
7777
# custom = { cover_art = "{IMAGE_URL}}", category = "TV", explicit = true, lang = "en" } # Optional feed customizations
7878
# max_height = "720" # Optional maximal height of video, example: 720, 1080, 1440, 2160, ...
7979
# cron_schedule = "@every 12h" # Optional cron expression format. If set then overwrite 'update_period'. See details below
80-
# filters = { title = "regex for title here" } # Optional Golang regexp format. If set, then only download episodes with matching titles.
80+
# filters = { title = "regex for title here", not_title = "regex for negative title match", description = "...", not_description = "..." } # Optional Golang regexp format. If set, then only download matching episodes.
8181
# opml = true|false # Optional inclusion of the feed in the OPML file (default value: false)
8282
# clean = { keep_last = 10 } # Keep last 10 episodes (order desc by PubDate)
8383

cmd/podsync/updater.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,40 @@ func (u *Updater) updateFeed(ctx context.Context, feedConfig *config.Feed) error
124124
return nil
125125
}
126126

127+
func (u *Updater) matchRegexpFilter(pattern, str string, negative bool, logger log.FieldLogger) bool {
128+
if pattern != "" {
129+
matched, err := regexp.MatchString(pattern, str)
130+
if err != nil {
131+
logger.Warnf("pattern %q is not a valid")
132+
} else {
133+
if matched == negative {
134+
logger.Infof("skipping due to mismatch")
135+
return false
136+
}
137+
}
138+
}
139+
return true
140+
}
141+
142+
func (u *Updater) matchFilters(episode *model.Episode, filters *config.Filters) bool {
143+
logger := log.WithFields(log.Fields{"episode_id": episode.ID})
144+
if !u.matchRegexpFilter(filters.Title, episode.Title, false, logger.WithField("filter", "title")) {
145+
return false
146+
}
147+
if !u.matchRegexpFilter(filters.NotTitle, episode.Title, true, logger.WithField("filter", "not_title")) {
148+
return false
149+
}
150+
151+
if !u.matchRegexpFilter(filters.Description, episode.Description, false, logger.WithField("filter", "description")) {
152+
return false
153+
}
154+
if !u.matchRegexpFilter(filters.NotDescription, episode.Description, true, logger.WithField("filter", "not_description")) {
155+
return false
156+
}
157+
158+
return true
159+
}
160+
127161
func (u *Updater) downloadEpisodes(ctx context.Context, feedConfig *config.Feed) error {
128162
var (
129163
feedID = feedConfig.ID
@@ -140,16 +174,8 @@ func (u *Updater) downloadEpisodes(ctx context.Context, feedConfig *config.Feed)
140174
return nil
141175
}
142176

143-
if feedConfig.Filters.Title != "" {
144-
matched, err := regexp.MatchString(feedConfig.Filters.Title, episode.Title)
145-
if err != nil {
146-
log.Warnf("pattern %q is not a valid filter for %q Title", feedConfig.Filters.Title, feedConfig.ID)
147-
} else {
148-
if !matched {
149-
log.Infof("skipping %q due to lack of match with %q", episode.Title, feedConfig.Filters.Title)
150-
return nil
151-
}
152-
}
177+
if !u.matchFilters(episode, &feedConfig.Filters) {
178+
return nil
153179
}
154180

155181
// Limit the number of episodes downloaded at once

pkg/config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ type Feed struct {
4545
}
4646

4747
type Filters struct {
48-
Title string `toml:"title"`
48+
Title string `toml:"title"`
49+
NotTitle string `toml:"not_title"`
50+
Description string `toml:"description"`
51+
NotDescription string `toml:"not_description"`
4952
// More filters to be added here
5053
}
5154

0 commit comments

Comments
 (0)