frenzy/lib/frenzy/update_feeds.ex

143 lines
3.8 KiB
Elixir
Raw Normal View History

2019-02-11 22:22:35 +00:00
defmodule Frenzy.UpdateFeeds do
use GenServer
2019-11-10 19:04:00 +00:00
alias Frenzy.{HTTP, Repo, Feed, Item}
alias Frenzy.Task.{CreateItem, FetchFavicon}
2019-02-11 22:22:35 +00:00
import Ecto.Query
require Logger
def start_link(state) do
GenServer.start_link(__MODULE__, :ok, state)
end
def refresh(pid, feed) do
2020-05-31 20:13:00 +00:00
GenServer.cast(pid, {:refresh, feed})
2019-02-11 22:22:35 +00:00
end
def init(state) do
update_feeds()
schedule_update()
{:ok, state}
end
2020-05-31 20:13:00 +00:00
def handle_cast({:refresh, feed}, state) do
2019-02-11 22:22:35 +00:00
update_feed(feed)
2020-05-31 20:13:00 +00:00
{:noreply, state}
2019-02-11 22:22:35 +00:00
end
def handle_info(:update_feeds, state) do
update_feeds()
schedule_update()
{:noreply, state}
end
defp schedule_update() do
# 30 minutes
Process.send_after(self(), :update_feeds, 30 * 60 * 1000)
2019-03-14 23:48:46 +00:00
# 1 minutes
2019-03-23 23:42:38 +00:00
# Process.send_after(self(), :update_feeds, 60 * 1000)
2019-02-11 22:22:35 +00:00
end
defp update_feeds() do
Logger.info("Updating all feeds")
Repo.all(from(Feed))
2019-03-14 23:48:46 +00:00
|> Enum.map(&update_feed/1)
2019-02-11 22:22:35 +00:00
prune_old_items()
end
defp prune_old_items() do
2019-03-09 15:59:08 +00:00
{count, _} =
2019-03-14 23:48:46 +00:00
from(i in Item,
2019-11-10 19:03:13 +00:00
where: not i.tombstone,
# todo: these time intervals should be configurable by the admin
where:
(i.read and i.read_date <= from_now(-1, "week")) or
(not i.read and i.inserted_at <= from_now(-2, "week")),
2019-03-14 23:48:46 +00:00
update: [
set: [tombstone: true, content: nil, creator: nil, date: nil, url: nil, title: nil]
]
)
|> Repo.update_all([])
Logger.info("Converted #{count} read items to tombstones")
2019-02-11 22:22:35 +00:00
end
defp update_feed(feed) do
Logger.debug("Updating #{feed.feed_url}")
2020-05-31 20:23:51 +00:00
case HTTP.get(feed.feed_url) do
2019-09-01 20:46:56 +00:00
{:ok,
%HTTPoison.Response{
status_code: 200,
body: body,
headers: headers
}} ->
2019-09-01 20:46:56 +00:00
{_, content_type} =
headers
|> Enum.find(fn {k, _v} -> k == "Content-Type" end)
2019-09-01 20:46:56 +00:00
content_type =
content_type
|> String.split(";")
|> Enum.map(&String.trim/1)
|> Enum.find(fn s -> !String.contains?(s, "=") end)
case FeedParser.parse(body, content_type) do
2019-02-11 22:22:35 +00:00
{:ok, rss} ->
update_feed_from_rss(feed, rss)
2020-05-30 00:04:41 +00:00
{:error, reason} ->
Logger.error("Unable to parse feed at '#{feed.feed_url}': #{inspect(reason)}")
2019-02-11 22:22:35 +00:00
end
2019-03-09 15:59:08 +00:00
2019-02-11 22:22:35 +00:00
{:ok, %HTTPoison.Response{status_code: 404}} ->
Logger.warn("RSS feed #{feed.feed_url} not found")
2019-03-09 15:59:08 +00:00
2019-07-01 01:41:18 +00:00
{:ok, %HTTPoison.Response{status_code: status_code, headers: headers}}
when status_code in [301, 302] ->
{"Location", new_url} =
Enum.find(headers, fn {name, _value} ->
name == "Location"
end)
Logger.debug("Got 301 redirect from #{feed.feed_url} to #{new_url}, updating feed URL")
changeset = Feed.changeset(feed, %{feed_url: new_url})
2019-07-01 21:46:54 +00:00
{:ok, feed} = Repo.update(changeset)
2019-07-01 01:41:18 +00:00
update_feed(feed)
2019-09-26 15:59:44 +00:00
{:ok, %HTTPoison.Response{} = response} ->
Logger.error(
"Couldn't load RSS feed #{feed.feed_url}, got unexpected response: #{inspect(response)}"
)
2019-09-26 15:59:44 +00:00
2019-02-11 22:22:35 +00:00
{:error, %HTTPoison.Error{reason: reason}} ->
2019-09-26 15:55:42 +00:00
Logger.error("Couldn't load RSS feed #{feed.feed_url}: #{inspect(reason)}")
2019-02-11 22:22:35 +00:00
end
end
2019-09-01 20:46:56 +00:00
defp update_feed_from_rss(feed, %FeedParser.Feed{} = rss) do
2019-03-09 15:59:08 +00:00
changeset =
Feed.changeset(feed, %{
title: rss.title,
2019-09-01 20:46:56 +00:00
site_url: rss.site_url,
last_updated: (rss.last_updated || DateTime.utc_now()) |> Timex.Timezone.convert(:utc)
2019-02-11 22:22:35 +00:00
})
2019-03-09 15:59:08 +00:00
2019-11-10 19:04:00 +00:00
{:ok, feed} = Repo.update(changeset)
if is_nil(feed.favicon) do
FetchFavicon.run(feed)
end
2019-02-11 22:22:35 +00:00
feed = Repo.preload(feed, [:items])
2019-02-11 22:22:35 +00:00
2019-11-01 02:21:17 +00:00
Enum.each(rss.items, fn entry ->
2019-03-10 23:47:01 +00:00
# todo: use Repo.exists for this
2019-09-01 20:46:56 +00:00
if !Enum.any?(feed.items, fn item -> item.guid == entry.guid end) do
2019-11-10 19:04:00 +00:00
CreateItem.start_link(feed, entry)
2019-02-11 22:22:35 +00:00
end
end)
end
2019-03-09 15:59:08 +00:00
end