From 6ec519a11f705e992aff3e54342be37a0fe7394b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 30 Jun 2019 21:41:18 -0400 Subject: [PATCH] Handle feed URL redirects Closes #1 --- lib/frenzy/update_feeds.ex | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/frenzy/update_feeds.ex b/lib/frenzy/update_feeds.ex index f68bfeb..1adc8f0 100644 --- a/lib/frenzy/update_feeds.ex +++ b/lib/frenzy/update_feeds.ex @@ -74,6 +74,18 @@ defmodule Frenzy.UpdateFeeds do {:ok, %HTTPoison.Response{status_code: 404}} -> Logger.warn("RSS feed #{feed.feed_url} not found") + {: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}) + {:ok, feed} = Repo.insert(changeset) + update_feed(feed) + {:error, %HTTPoison.Error{reason: reason}} -> Logger.error("Couldn't load RSS feed: #{reason}") end @@ -200,7 +212,7 @@ defmodule Frenzy.UpdateFeeds do {:err, "404 not found"} {:ok, %HTTPoison.Response{status_code: status_code, headers: headers}} - when status_code == 301 or status_code == 302 -> + when status_code in [301, 302] -> {"Location", new_url} = Enum.find(headers, fn {name, _value} -> name == "Location"