Packages

Discover and parse RSS and Atom feeds

Current section

Files

Jump to
feedparser lib feedparser.ex
Raw

lib/feedparser.ex

defmodule Feedparser do
defmodule Utils do
@moduledoc """
Finds Rss/Atom feed urls in html strings.
"""
@vsn 0.1
@doc """
Takes a chunk of html as input and returns all valid feed urls in it
"""
@spec get_feeds(char_list) :: [char_list]
def get_feeds(html) do
urls = urls(html)
|> List.flatten
|> Enum.filter(&(feed?(&1)))
|> Enum.map(&(extract_href &1))
|> Enum.filter(&(&1 != nil))
end
@spec urls(char_list) :: [char_list]
defp urls(html) do
Regex.scan(~r/<(?:a|link) .*?>/, html)
end
@spec feed?(char_list) :: boolean
defp feed?(url) do
atom?(url) or rss?(url)
end
@spec atom?(char_list) :: boolean
defp atom?(url), do: url =~ ~r/.*type=['"]application\/atom\+xml['"].*/
@spec rss?(char_list) :: boolean
defp rss?(url), do: url =~ ~r/.*type=['"]application\/rss\+xml['"].*/
@spec extract_href(char_list) :: char_list
defp extract_href(url) do
matches = Regex.run(~r/.*href=['"](.*?)['"].*/, url)
if matches != nil and length(matches) == 2 do
List.last matches
end
end
end
end