logo

news_parse_ex

commit: 646e307e4866d984df43fb1a490ff0b1be4d0b7a
parent a41b4ae5ec685fff120acbee9157eda351cb64fa
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 27 Dec 2022 12:21:00 +0100

Add initial RSS support

Diffstat:

Mlib/news_parse_ex.ex36+++++++++++++++++++++++++++++-------
Atest/fixtures/git.sr.ht/pkgconf-2022-12-27.rss2++
Mtest/news_parse_ex_test.exs9+++++++++
3 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/lib/news_parse_ex.ex b/lib/news_parse_ex.ex @@ -6,25 +6,47 @@ defmodule NewsParseEx do alias NewsParseEx.XML def get_feed_type(doc) do - with {_, {:ok, "feed"}} <- {:root_name, XML.string_from_xpath(~s[name()], doc)}, - {:ok, namespace} <- XML.string_from_xpath(~s{/feed/namespace::*[name()='']}, doc) do - case namespace do - "http://www.w3.org/2005/Atom" -> {:ok, :atom} - e -> {:error, e} + with {:ok, root_name} <- XML.string_from_xpath(~s[name()], doc) do + get_feed_type(doc, root_name) + end + end + + defp get_feed_type(doc, "feed") do + with {:ok, namespace} <- XML.string_from_xpath(~s{/feed/namespace::*[name()='']}, doc) do + if namespace == "http://www.w3.org/2005/Atom" do + {:ok, :atom} + else + {:error, "Atom feed with wrong root namespace: #{namespace}"} end - else - {:root_name, name} -> {:error, "XML root isn't <feed> but <#{name}>"} end end + defp get_feed_type(doc, "rss") do + with {:ok, version} <- XML.string_from_xpath(~s{/rss/@version}, doc) do + case version do + "2.0" -> {:ok, :rss2_0} + version -> {:error, "RSS with unknown version: #{version}"} + end + end + end + + defp get_feed_type(_doc, root_name) do + {:error, "XML root isn't <feed> but <#{root_name}>"} + end + def get_feed_title(doc, :atom), do: XML.string_from_xpath(~s[/feed/title/text()], doc) + def get_feed_title(doc, :rss2_0), do: XML.string_from_xpath(~s[/rss/channel/title/text()], doc) + def get_feed_id(doc, :atom), do: XML.string_from_xpath(~s[/feed/id/text()], doc) + def get_feed_id(doc, :rss2_0), do: XML.string_from_xpath(~s[/rss/channel/link/text()], doc) def get_feed_last_update(doc, :atom) do {:ok, updated} = XML.string_from_xpath(~s[/feed/updated/text()], doc) DateTime.from_iso8601(updated) end + def get_feed_last_update(_doc, :rss2_0), do: {:ok, nil, nil} + def parse(str) when is_bitstring(str) do with {_, {:ok, doc}} <- {:parse, XML.parse_document(str)}, {_, {:ok, feed_type}} <- {:type, get_feed_type(doc)}, diff --git a/test/fixtures/git.sr.ht/pkgconf-2022-12-27.rss b/test/fixtures/git.sr.ht/pkgconf-2022-12-27.rss @@ -0,0 +1 @@ +<rss version="2.0"><channel><title>~kaniini/pkgconf refs</title><link>https://git.sr.ht/~kaniini/pkgconf/refs</link><description>Git refs for ~kaniini/pkgconf</description><language>en</language><item><title>pkgconf-1.7.3</title><description>pkgconf 1.7.3.</description><author>ariadne@dereferenced.org (Ariadne Conill)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.3</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.3</guid><pubDate>Sat, 30 May 2020 19:17:12 -0600</pubDate></item><item><title>pkgconf-1.7.2</title><description>pkgconf 1.7.2.</description><author>ariadne@dereferenced.org (Ariadne Conill)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.2</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.2</guid><pubDate>Tue, 26 May 2020 13:46:20 -0600</pubDate></item><item><title>pkgconf-1.7.1</title><description>pkgconf 1.7.1.</description><author>ariadne@dereferenced.org (Ariadne Conill)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.1</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.1</guid><pubDate>Tue, 26 May 2020 11:06:35 -0600</pubDate></item><item><title>pkgconf-1.7.0</title><description>pkgconf 1.7.0.</description><author>ariadne@dereferenced.org (Ariadne Conill)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.0</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.7.0</guid><pubDate>Sun, 24 May 2020 14:44:01 -0600</pubDate></item><item><title>pkgconf-1.6.3</title><description>pkgconf 1.6.3.</description><author>ariadne@dereferenced.org (Ariadne Conill)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.3</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.3</guid><pubDate>Fri, 12 Jul 2019 06:53:37 -0500</pubDate></item><item><title>pkgconf-1.6.2</title><description>pkgconf 1.6.2. (closes #38, #40, #41)</description><author>ariadne@dereferenced.org (Ariadne Conill)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.2</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.2</guid><pubDate>Thu, 11 Jul 2019 03:36:03 -0500</pubDate></item><item><title>pkgconf-1.6.1</title><description>pkgconf 1.6.1.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.1</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.1</guid><pubDate>Sat, 23 Mar 2019 22:38:50 -0500</pubDate></item><item><title>pkgconf-1.6.0</title><description>pkgconf 1.6.0.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.0</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.6.0</guid><pubDate>Mon, 14 Jan 2019 14:12:32 -0600</pubDate></item><item><title>pkgconf-1.5.4</title><description>pkgconf 1.5.4.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.4</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.4</guid><pubDate>Mon, 15 Oct 2018 10:23:35 -0500</pubDate></item><item><title>pkgconf-1.5.3</title><description>pkgconf 1.5.3.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.3</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.3</guid><pubDate>Sat, 28 Jul 2018 19:47:53 -0500</pubDate></item><item><title>pkgconf-1.5.2</title><description>pkgconf 1.5.2.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.2</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.2</guid><pubDate>Thu, 19 Jul 2018 17:30:33 -0500</pubDate></item><item><title>pkgconf-1.5.1</title><description>pkgconf 1.5.1.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.1</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.5.1</guid><pubDate>Sat, 16 Jun 2018 16:36:40 -0500</pubDate></item><item><title>pkgconf-1.4.2</title><description>pkgconf 1.4.2.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.4.2</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.4.2</guid><pubDate>Mon, 12 Feb 2018 00:37:03 -0600</pubDate></item><item><title>pkgconf-1.4.1</title><description>tests: parser: late-breaking testsuite changes for quoting changes</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.4.1</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.4.1</guid><pubDate>Mon, 22 Jan 2018 15:35:54 -0600</pubDate></item><item><title>pkgconf-1.4.0</title><description>pkgconf 1.4.0.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.4.0</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.4.0</guid><pubDate>Fri, 05 Jan 2018 11:33:21 -0600</pubDate></item><item><title>pkgconf-1.3.90</title><description>pkgconf 1.3.90.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.90</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.90</guid><pubDate>Fri, 15 Dec 2017 19:00:09 -0600</pubDate></item><item><title>pkgconf-1.3.12</title><description>pkgconf 1.3.12.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.12</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.12</guid><pubDate>Fri, 08 Dec 2017 13:42:06 -0600</pubDate></item><item><title>pkgconf-1.3.11</title><description>pkgconf 1.3.11.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.11</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.11</guid><pubDate>Fri, 08 Dec 2017 13:06:28 -0600</pubDate></item><item><title>pkgconf-1.3.10</title><description>pkgconf 1.3.10.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.10</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.10</guid><pubDate>Tue, 17 Oct 2017 11:58:19 -0500</pubDate></item><item><title>pkgconf-1.3.9</title><description>pkgconf 1.3.9.</description><author>nenolod@dereferenced.org (William Pitcock)</author><link>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.9</link><guid>https://git.sr.ht/~kaniini/pkgconf/refs/pkgconf-1.3.9</guid><pubDate>Tue, 19 Sep 2017 21:36:23 -0500</pubDate></item></channel></rss> +\ No newline at end of file diff --git a/test/news_parse_ex_test.exs b/test/news_parse_ex_test.exs @@ -24,4 +24,13 @@ defmodule NewsParseExTest do assert(parsed.id == "https://gitlab.freedesktop.org/wlroots/wlroots/-/tags") assert(parsed.last_update == ~U[2021-11-01T16:09:55Z]) end + + test "parses git.sr.ht RSS feed" do + feed = File.read!("test/fixtures/git.sr.ht/pkgconf-2022-12-27.rss") + + {:ok, parsed} = NewsParseEx.parse(feed) + assert(parsed.title == "~kaniini/pkgconf refs") + assert(parsed.id == "https://git.sr.ht/~kaniini/pkgconf/refs") + assert(parsed.last_update == nil) + end end