logo

news_parse_ex

commit: 8580e00f537e3a01d65f7f9a81d11bc3461849e7
parent 312a3999820947792d3fa15ce0c3a4d10188bbff
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 27 Dec 2022 12:27:02 +0100

Get feed description

Diffstat:

Mlib/news_parse_ex.ex11+++++++++--
Mtest/news_parse_ex_test.exs3+++
2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/lib/news_parse_ex.ex b/lib/news_parse_ex.ex @@ -47,16 +47,23 @@ defmodule NewsParseEx do defp get_feed_last_update(_doc, :rss2_0), do: {:ok, nil, nil} + defp get_feed_description(_doc, :atom), do: {:ok, nil} + + defp get_feed_description(doc, :rss2_0), + do: XML.string_from_xpath(~s[/rss/channel/description/text()], doc) + def parse(str) when is_bitstring(str) do with {_, {:ok, doc}} <- {:parse, XML.parse_document(str)}, {_, {:ok, feed_type}} <- {:type, get_feed_type(doc)}, - {_, {:ok, title}} <- {:title, get_feed_title(doc, feed_type)}, {_, {:ok, id}} <- {:id, get_feed_id(doc, feed_type)}, {_, {:ok, last_update, _tz_offset}} <- - {:last_update, get_feed_last_update(doc, feed_type)} do + {:last_update, get_feed_last_update(doc, feed_type)}, + {_, {:ok, title}} <- {:title, get_feed_title(doc, feed_type)}, + {_, {:ok, description}} <- {:desc, get_feed_description(doc, feed_type)} do data = %{ :type => feed_type, :title => title, + :description => description, :id => id, :last_update => last_update, :entries => [] diff --git a/test/news_parse_ex_test.exs b/test/news_parse_ex_test.exs @@ -12,6 +12,7 @@ defmodule NewsParseExTest do {:ok, parsed} = NewsParseEx.parse(feed) assert(parsed.title == "Test Title") + assert(parsed.description == nil) assert(parsed.id == "https://example.org/feed/") assert(parsed.last_update == ~U[2021-11-01 16:09:55Z]) end @@ -21,6 +22,7 @@ defmodule NewsParseExTest do {:ok, parsed} = NewsParseEx.parse(feed) assert(parsed.title == "wlroots tags") + assert(parsed.description == nil) assert(parsed.id == "https://gitlab.freedesktop.org/wlroots/wlroots/-/tags") assert(parsed.last_update == ~U[2021-11-01T16:09:55Z]) end @@ -30,6 +32,7 @@ defmodule NewsParseExTest do {:ok, parsed} = NewsParseEx.parse(feed) assert(parsed.title == "~kaniini/pkgconf refs") + assert(parsed.description == "Git refs for ~kaniini/pkgconf") assert(parsed.id == "https://git.sr.ht/~kaniini/pkgconf/refs") assert(parsed.last_update == nil) end