Skip to content

Commit

Permalink
Merge pull request #132 from nning/feature/exclude-filters
Browse files Browse the repository at this point in the history
Add exclude filters
  • Loading branch information
nning authored Nov 16, 2022
2 parents 578cf94 + 5c5a21d commit 063306d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/transmission-rss/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ def process_link(feed, item)

# Link is not a String directly.
link = link.href if link.class != String

# Determine whether to use guid or link as seen hash
seen_value = feed.seen_by_guid ? (item.guid.content rescue item.guid || link).to_s : link

# The link is not in +@seen+ Array.
unless @seen.include?(seen_value)
# Skip if filter defined and not matching.
unless feed.matches_regexp?(item.title)
unless feed.matches_regexp?(item.title) && !feed.exclude?(item.title)
@seen.add(seen_value)
return
end
Expand Down
15 changes: 13 additions & 2 deletions lib/transmission-rss/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Feed

def initialize(config = {})
@download_paths = {}
@excludes = {}

case config
when Hash
Expand All @@ -19,7 +20,7 @@ def initialize(config = {})

@regexp = build_regexp(matchers)

initialize_download_paths(config['regexp'])
initialize_download_paths_and_excludes(config['regexp'])
else
@config = {}
@url = config.to_s
Expand All @@ -43,21 +44,31 @@ def matches_regexp?(title)
@regexp.nil? || !(title =~ @regexp).nil?
end

def exclude?(title)
@excludes.each do |regexp, exclude|
return true if title =~ to_regexp(exclude)
end

return false
end

private

def build_regexp(matchers)
matchers = Array(matchers).map { |m| to_regexp(m) }
matchers.empty? ? nil : Regexp.union(matchers)
end

def initialize_download_paths(regexps)
def initialize_download_paths_and_excludes(regexps)
return unless regexps.is_a?(Array)

regexps.each do |regexp|
matcher = regexp['matcher']
path = regexp['download_path']
exclude = regexp['exclude']

@download_paths[matcher] = path if matcher && path
@excludes[matcher] = exclude if matcher && exclude
end
end

Expand Down
1 change: 1 addition & 0 deletions transmission-rss.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ feeds:
regexp:
- matcher: match1
download_path: /home/user/match1
exclude: dontmatch
- matcher: match2
download_path: /home/user/match2
- url: http://example.com/feed8
Expand Down

0 comments on commit 063306d

Please sign in to comment.