Skip to content

Commit

Permalink
Merge pull request #148 from dop251/filters-pr
Browse files Browse the repository at this point in the history
Implemented negative filters for title and description.
  • Loading branch information
mxpv authored Jun 8, 2020
2 parents af09e4c + b47cc9b commit 13e77d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ vimeo = [ # Multiple keys will be rotated.
# custom = { cover_art = "{IMAGE_URL}}", category = "TV", explicit = true, lang = "en" } # Optional feed customizations
# max_height = "720" # Optional maximal height of video, example: 720, 1080, 1440, 2160, ...
# cron_schedule = "@every 12h" # Optional cron expression format. If set then overwrite 'update_period'. See details below
# filters = { title = "regex for title here" } # Optional Golang regexp format. If set, then only download episodes with matching titles.
# 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.
# opml = true|false # Optional inclusion of the feed in the OPML file (default value: false)
# clean = { keep_last = 10 } # Keep last 10 episodes (order desc by PubDate)

Expand Down
46 changes: 36 additions & 10 deletions cmd/podsync/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ func (u *Updater) updateFeed(ctx context.Context, feedConfig *config.Feed) error
return nil
}

func (u *Updater) matchRegexpFilter(pattern, str string, negative bool, logger log.FieldLogger) bool {
if pattern != "" {
matched, err := regexp.MatchString(pattern, str)
if err != nil {
logger.Warnf("pattern %q is not a valid")
} else {
if matched == negative {
logger.Infof("skipping due to mismatch")
return false
}
}
}
return true
}

func (u *Updater) matchFilters(episode *model.Episode, filters *config.Filters) bool {
logger := log.WithFields(log.Fields{"episode_id": episode.ID})
if !u.matchRegexpFilter(filters.Title, episode.Title, false, logger.WithField("filter", "title")) {
return false
}
if !u.matchRegexpFilter(filters.NotTitle, episode.Title, true, logger.WithField("filter", "not_title")) {
return false
}

if !u.matchRegexpFilter(filters.Description, episode.Description, false, logger.WithField("filter", "description")) {
return false
}
if !u.matchRegexpFilter(filters.NotDescription, episode.Description, true, logger.WithField("filter", "not_description")) {
return false
}

return true
}

func (u *Updater) downloadEpisodes(ctx context.Context, feedConfig *config.Feed) error {
var (
feedID = feedConfig.ID
Expand All @@ -140,16 +174,8 @@ func (u *Updater) downloadEpisodes(ctx context.Context, feedConfig *config.Feed)
return nil
}

if feedConfig.Filters.Title != "" {
matched, err := regexp.MatchString(feedConfig.Filters.Title, episode.Title)
if err != nil {
log.Warnf("pattern %q is not a valid filter for %q Title", feedConfig.Filters.Title, feedConfig.ID)
} else {
if !matched {
log.Infof("skipping %q due to lack of match with %q", episode.Title, feedConfig.Filters.Title)
return nil
}
}
if !u.matchFilters(episode, &feedConfig.Filters) {
return nil
}

// Limit the number of episodes downloaded at once
Expand Down
5 changes: 4 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ type Feed struct {
}

type Filters struct {
Title string `toml:"title"`
Title string `toml:"title"`
NotTitle string `toml:"not_title"`
Description string `toml:"description"`
NotDescription string `toml:"not_description"`
// More filters to be added here
}

Expand Down

0 comments on commit 13e77d0

Please sign in to comment.