Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exclude filters #83

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/transmission-rss/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def process_link(feed, item)
# The link is not in +@seen+ Array.
unless @seen.include?(link)
# Skip if filter defined and not matching.
unless feed.matches_regexp?(item.title)
unless feed.matches_regexp?(item.title) && !(feed.exclude?(item.title))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unless feed.matches_regexp?(item.title) && !(feed.exclude?(item.title))
unless feed.matches_regexp?(item.title) && !feed.exclude?(item.title)

@seen.add(link)
return
end
Expand Down
14 changes: 12 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 @@ -20,7 +21,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 @@ -41,21 +42,30 @@ def matches_regexp?(title)
@regexp.nil? || !(title =~ @regexp).nil?
end

def exclude?(title)
@excludes.each do |regexp, exclude|
return title =~ to_regexp(exclude) if title =~ to_regexp(regexp)
Copy link
Owner

@nning nning Nov 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of title =~ to_regexp(exclude) should only be evaluated once.

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']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
exclude = regexp['exclude']
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