Add feed filters

This commit is contained in:
Shadowfacts 2019-03-14 21:42:02 -04:00
parent 27cf787d52
commit eefa65ed56
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 72 additions and 12 deletions

View File

@ -0,0 +1,33 @@
defmodule Frenzy.FilterEngine do
def matches?(item, filter) do
score =
filter.rules
|> Enum.map(fn rule -> score(item, rule) end)
|> Enum.sum()
score >= filter.score
end
def score(item, rule) do
prop_value = get_property(item, rule.property)
if matches(prop_value, rule.mode, rule.param) do
rule.weight
else
0
end
end
def matches(value, "contains_string", param) do
String.contains?(value, param)
end
def matches(value, "matches_regex", param) do
regex = Regex.compile(param)
String.match?(value, regex)
end
def get_property(item, "url"), do: item.url
def get_property(item, "title"), do: item.title
def get_property(item, "author"), do: item.author
end

View File

@ -1,6 +1,6 @@
defmodule Frenzy.UpdateFeeds do defmodule Frenzy.UpdateFeeds do
use GenServer use GenServer
alias Frenzy.{Repo, Feed, Item} alias Frenzy.{Repo, Feed, Item, FilterEngine}
import Ecto.Query import Ecto.Query
require Logger require Logger
@ -89,7 +89,7 @@ defmodule Frenzy.UpdateFeeds do
Repo.update(changeset) Repo.update(changeset)
feed = Repo.preload(feed, :items) feed = Repo.preload(feed, items: [], filter: [:rules])
Enum.map(rss.items, fn entry -> Enum.map(rss.items, fn entry ->
# todo: use Repo.exists for this # todo: use Repo.exists for this
@ -100,8 +100,7 @@ defmodule Frenzy.UpdateFeeds do
end end
defp create_item(feed, entry) do defp create_item(feed, entry) do
Logger.debug("Creating item for:") Logger.debug("Creating item for #{entry.url}")
IO.inspect(entry)
url = get_real_url(entry) url = get_real_url(entry)
@ -115,15 +114,43 @@ defmodule Frenzy.UpdateFeeds do
entry.description entry.description
end end
changeset = item_params = %{
Ecto.build_assoc(feed, :items, %{
guid: entry.id, guid: entry.id,
title: entry.title, title: entry.title,
url: url, url: url,
date: parse_date(entry.published_at), date: parse_date(entry.published_at),
creator: "", creator: "",
content: content content: content
}
result =
if feed.filter_enabled do
case {feed.filter.mode, FilterEngine.matches?(item_params, feed.filter)} do
{"accept", true} ->
:store
{"reject", false} ->
:store
_ ->
Logger.debug("Skipping item #{url} due to feed filter")
:tombstone
end
else
:store
end
changeset =
case result do
:store ->
Ecto.build_assoc(feed, :items, item_params)
:tombstone ->
Ecto.build_assoc(feed, :items, %{
guid: entry.id,
tombstone: true
}) })
end
Repo.insert(changeset) Repo.insert(changeset)
end end